Learn how to localize basemap place labels using the basemap styles service.
The basemap styles service provides a number of basemap layer styles such as topography, streets, and imagery that you can use in maps. Each style accepts a language parameter, which allows you to localize place labels. There are currently over 30 different languages available.
In this tutorial, you will create a Calcite combobox to switch between different languages to update the Basemap style and app locale.
Prerequisites
Steps
Create a new pen
- To get started, either complete the Display a map tutorial or .
Get an access token
You need an access token with the correct privileges to access the location services used in this tutorial.
- Go to the Create an API key tutorial and create an API key with the following privilege(s):
- Privileges
- Location services > Basemaps
- Privileges
- In CodePen, set
esri
to your API key..Config.api Key Use dark colors for code blocks esriConfig.apiKey = "YOUR_ACCESS_TOKEN"; const map = new Map({ basemap: "arcgis/topographic" // basemap styles service });
To learn about other ways to get an access token, go to Types of authentication.
Add modules
The intl
module provides utilities for updating the locale of your application. For more information, see the Localization guide page.
- In the
require
statement, add the intl module.The ArcGIS Maps SDK for JavaScript is available as AMD modules and ES modules, but this tutorial is based on AMD. The AMD
require
function uses references to determine which modules will be loaded – for example, you can specify"esri/
for loading the Map module. After the modules are loaded, they are passed as parameters (e.g.Map" Map
) to the callback function where they can be used in your application. It is important to keep the module references and callback parameters in the same order. To learn more about the API's different modules visit the Overview Guide page.Use dark colors for code blocks require([ "esri/config", "esri/Map", "esri/views/MapView", "esri/intl" ], (esriConfig, Map, MapView, intl) => {
Update the map view
- Update the
center
andzoom
properties to focus the display on Europe.Use dark colors for code blocks const view = new MapView({ map: map, center: [13, 46], // Longitude, latitude zoom: 4, // Zoom level container: "viewDiv" // Div element });
Set the current language
Set the current language of the basemap and the locale of the application to Spanish (es
).
Setting the locale of the application will update the widgets to use the given language and formatting.
For instance, when the locale is set to Spanish, the zoom widget tooltip will say Acercar
instead of Zoom in
.
-
Set the app's locale to Spanish (
es
) usingintl.set
.Locale() Use dark colors for code blocks intl.setLocale("es");
-
Set the basemap from a
style
to thearcgis/outdoor
basemap style with thelanguage
property set toes
. This will display the basemap's place labels in Spanish.Use dark colors for code blocks intl.setLocale("es"); const map = new Map({ basemap: { style: { id: "arcgis/outdoor", language: "es" } } });
Create a basemap language function
Create a function that accepts a language code and updates the basemap style formatted with that language.
-
Define a new function
update
that accepts a language code.Basemap Language Use dark colors for code blocks const updateBasemapLanguage = (languageCode) => { };
-
Use the
language
variable to update the application's locale and the language of the basemap style.Code Use dark colors for code blocks const updateBasemapLanguage = (languageCode) => { intl.setLocale(languageCode); view.map.basemap = { style: { id: "arcgis/outdoor", language: languageCode } } };
Add a language Calcite combobox
Use the Calcite combobox component to create a dropdown to change the language of the basemap.
-
Add references to Calcite components in the
<head
tag.> Use dark colors for code blocks <script type="module" src="https://js.arcgis.com/calcite-components/2.11.1/calcite.esm.js"></script> <link rel="stylesheet" type="text/css" href="https://js.arcgis.com/calcite-components/2.11.1/calcite.css" />
-
Create a HTML
div
element with a calcite label and combobox component for all the languages.Use dark colors for code blocks <div id="basemapLanguage" class="esri-widget"> <calcite-label>Basemap language</calcite-label> <calcite-combobox id="languageCombobox" selection-mode="single" clear-disabled="true"> <calcite-combobox-item value="global" text-label="Global"></calcite-combobox-item> <calcite-combobox-item value="local" text-label="Local"></calcite-combobox-item> <calcite-combobox-item value="ar" text-label="Arabic"></calcite-combobox-item> <calcite-combobox-item value="bs" text-label="Bosnian"></calcite-combobox-item> <calcite-combobox-item value="ca" text-label="Catalan"></calcite-combobox-item> <calcite-combobox-item value="zh-HK" text-label="Chinese (Hong Kong)"></calcite-combobox-item> <calcite-combobox-item value="zh-CN" text-label="Chinese (Simplified)"></calcite-combobox-item> <calcite-combobox-item value="zh-TW" text-label="Chinese (Taiwan)"></calcite-combobox-item> <calcite-combobox-item value="hr" text-label="Croatian"></calcite-combobox-item> <calcite-combobox-item value="cs" text-label="Czech"></calcite-combobox-item> <calcite-combobox-item value="da" text-label="Danish"></calcite-combobox-item> <calcite-combobox-item value="nl" text-label="Dutch"></calcite-combobox-item> <calcite-combobox-item value="en" text-label="English"></calcite-combobox-item> <calcite-combobox-item value="et" text-label="Estonian"></calcite-combobox-item> <calcite-combobox-item value="fi" text-label="Finnish"></calcite-combobox-item> <calcite-combobox-item value="fr" text-label="French"></calcite-combobox-item> <calcite-combobox-item value="de" text-label="German"></calcite-combobox-item> <calcite-combobox-item value="el" text-label="Greek"></calcite-combobox-item> <calcite-combobox-item value="he" text-label="Hebrew"></calcite-combobox-item> <calcite-combobox-item value="hu" text-label="Hungarian"></calcite-combobox-item> <calcite-combobox-item value="id" text-label="Indonesian"></calcite-combobox-item> <calcite-combobox-item value="it" text-label="Italian"></calcite-combobox-item> <calcite-combobox-item value="ja" text-label="Japanese"></calcite-combobox-item> <calcite-combobox-item value="ko" text-label="Korean"></calcite-combobox-item> <calcite-combobox-item value="lv" text-label="Latvian"></calcite-combobox-item> <calcite-combobox-item value="lt" text-label="Lithuanian"></calcite-combobox-item> <calcite-combobox-item value="nb" text-label="Norwegian"></calcite-combobox-item> <calcite-combobox-item value="pl" text-label="Polish"></calcite-combobox-item> <calcite-combobox-item value="pt-BR" text-label="Portuguese (Brazil)"></calcite-combobox-item> <calcite-combobox-item value="pt-PT" text-label="Portuguese (Portugal)"></calcite-combobox-item> <calcite-combobox-item value="ro" text-label="Romanian"></calcite-combobox-item> <calcite-combobox-item value="ru" text-label="Russian"></calcite-combobox-item> <calcite-combobox-item value="sr" text-label="Serbian"></calcite-combobox-item> <calcite-combobox-item value="es" text-label="Spanish" selected></calcite-combobox-item> <calcite-combobox-item value="sv" text-label="Swedish"></calcite-combobox-item> <calcite-combobox-item value="th" text-label="Thai"></calcite-combobox-item> <calcite-combobox-item value="tr" text-label="Turkish"></calcite-combobox-item> <calcite-combobox-item value="uk" text-label="Ukrainian"></calcite-combobox-item> <calcite-combobox-item value="vi" text-label="Vietnamese"></calcite-combobox-item> </calcite-combobox> </div>
-
Add the
basemap
div to the view UI.Language Use dark colors for code blocks const basemapLanguage = document.getElementById("basemapLanguage"); // add to the view UI view.ui.add(basemapLanguage, "top-right");
-
Apply some CSS styling to the
basemap
div within theLanguage <style
tags.> Use dark colors for code blocks #basemapLanguage { width: 200px; padding: 10px; }
-
Add an event listener to watch for changes on the combobox. When the combobox value has changed, call the
update
function to update the basemap language and app locale.Basemap Language Use dark colors for code blocks const basemapLanguage = document.getElementById("basemapLanguage"); // add to the view UI view.ui.add(basemapLanguage, "top-right"); const languageCombobox = document.getElementById("languageCombobox"); // when the combobox value changes, update the basemap language languageCombobox.addEventListener("calciteComboboxChange", (event) => { updateBasemapLanguage(event.target.value); });
Run the App
In CodePen, run your code to display the map.
You should be able to use the calcite combobox to update the basemap language and app locale.
What's Next?
Learn how to use additional API features and ArcGIS services in these tutorials: