Learn how to localize
Use the combobox to change the basemap language.
The
In this tutorial, you will create a Calcite combobox to switch between different languages to update the Basemap style and app language.
Prerequisites
Steps
Create a new pen
- To get started, either complete the Display a map tutorial or
.
Get an access token
You need an
- Go to the Create an API key tutorial and create an
API key with the followingprivilege(s) :
- Privileges
- Location services > Basemaps
- Privileges
- In CodePen, set the
apiKeyproperty on the globalesriConfigvariable to your access token.var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};
To learn about other ways to get an access token, go to Types of authentication.
Update the map
- Update the
centerandzoomattributes to focus the display on Europe.31 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Display a map</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body lang="es"><arcgis-map basemap="arcgis/topographic" center="13, 46" zoom="4"><arcgis-zoom slot="top-left"></arcgis-zoom></arcgis-map>5 collapsed lines</body></html>
Add modules
The request module can be used to request data from a remote server or file. We’ll use this module to fetch the available languages from the basemap style API endpoint.
- In a new
<script>at the bottom of the<body>, use$arcgis.import()to add the request module.The
ArcGIS Maps SDK for JavaScript is available via CDN and npm, but this tutorial is based on CDN. The$arcgis.importglobal function accepts a module path or array of module paths, and returns a promise that resolves with the requested modules. This function can only be used when working with the CDN; otherwise, use the standard import syntax. To learn more about the SDK’s different modules, visit the References page.37 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Display a map</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body lang="es"><arcgis-map basemap="arcgis/topographic" center="13, 46" zoom="4"><arcgis-zoom slot="top-left"></arcgis-zoom></arcgis-map><script type="module">const esriRequest = await $arcgis.import("@arcgis/core/request.js");</script>5 collapsed lines</body></html>
Set the current language
Set the current language of the basemap and the lang attribute on the body to Spanish (es).
Setting the lang of the application will update the components to use the given language and formatting.
For instance, when the locale is set to Spanish, the zoom component tooltip will say Acercar instead of Zoom in.
-
Set the app’s locale to Spanish (
es) by setting thelangattribute on the documentbody.28 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Display a map</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body lang="es"><arcgis-map basemap="arcgis/topographic" center="13, 46" zoom="4"><arcgis-zoom slot="top-left"></arcgis-zoom></arcgis-map></body>3 collapsed lines</html> -
Set the basemap from a
styleto thearcgis/outdoorbasemap style with thelanguageproperty set to"es". This will display the basemap’s place labels in Spanish.37 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Display a map</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body lang="es"><arcgis-map basemap="arcgis/topographic" center="13, 46" zoom="4"><arcgis-zoom slot="top-left"></arcgis-zoom></arcgis-map><script type="module">const esriRequest = await $arcgis.import("@arcgis/core/request.js");const viewElement = document.querySelector("arcgis-map");viewElement.basemap = {style: {id: "arcgis/outdoor",language: "es", // Spanish language basemap},};</script>5 collapsed lines</body></html>
Create a language dropdown
Get the list of languages available from the basemap styles service and add them to a calcite-combobox component to allow users to select the language of their basemap.
-
Create a div called
basemapLanguagewith acalcite-comboboxcomponent, placed in thetop-rightslot of the map. Setselection-modeto single, so only one language can be selected at a time. Add theclear-disabledattribute so that a language is always selected.31 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Display a map</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body lang="es"><arcgis-map basemap="arcgis/topographic" center="13, 46" zoom="4"><arcgis-zoom slot="top-left"></arcgis-zoom><div id="basemapLanguage" slot="top-right"><calcite-label>Basemap language</calcite-label><calcite-combobox id="languageCombobox" selection-mode="single" clear-disabled></calcite-combobox></div></arcgis-map>19 collapsed lines<script type="module">const esriRequest = await $arcgis.import("@arcgis/core/request.js");const viewElement = document.querySelector("arcgis-map");viewElement.basemap = {style: {id: "arcgis/outdoor",language: "es", // Spanish language basemap},};</script></body></html> -
Within the
<style>tags, apply some CSS properties to thebasemapLanguagediv.15 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Display a map</title><style>html,body {height: 100%;margin: 0;}#basemapLanguage {width: 200px;padding: 10px;background-color: white;}46 collapsed lines</style><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body lang="es"><arcgis-map basemap="arcgis/topographic" center="13, 46" zoom="4"><arcgis-zoom slot="top-left"></arcgis-zoom><div id="basemapLanguage" slot="top-right"><calcite-label>Basemap language</calcite-label><calcite-combobox id="languageCombobox" selection-mode="single" clear-disabled></calcite-combobox></div></arcgis-map><script type="module">const esriRequest = await $arcgis.import("@arcgis/core/request.js");const viewElement = document.querySelector("arcgis-map");viewElement.basemap = {style: {id: "arcgis/outdoor",language: "es", // Spanish language basemap},};</script></body></html> -
Fetch the list of languages available from the
/selfendpoint for the basemap styles service using theesriRequestmethod. Specifyjsonas the response type. This will return a json response with alanguagesproperty.61 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Display a map</title><style>html,body {height: 100%;margin: 0;}#basemapLanguage {width: 200px;padding: 10px;background-color: white;}</style><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body lang="es"><arcgis-map basemap="arcgis/topographic" center="13, 46" zoom="4"><arcgis-zoom slot="top-left"></arcgis-zoom><div id="basemapLanguage" slot="top-right"><calcite-label>Basemap language</calcite-label><calcite-combobox id="languageCombobox" selection-mode="single" clear-disabled></calcite-combobox></div></arcgis-map><script type="module">const esriRequest = await $arcgis.import("@arcgis/core/request.js");const viewElement = document.querySelector("arcgis-map");viewElement.basemap = {style: {id: "arcgis/outdoor",language: "es", // Spanish language basemap},};// update combobox values with languages from rest endpointconst basemapStylesEndpoint ="https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles/self";esriRequest(basemapStylesEndpoint, {responseType: "json",}).then((response) => {});7 collapsed lines</script></body></html> -
From the response data, use the JavaScript forEach method to iterate through each of the languages and add it as a
calcite-combobox-item. Set the value to the language code, and the label to the language name. If the language code matches the language of the current basemap, set the item as selected.61 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Display a map</title><style>html,body {height: 100%;margin: 0;}#basemapLanguage {width: 200px;padding: 10px;background-color: white;}</style><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body lang="es"><arcgis-map basemap="arcgis/topographic" center="13, 46" zoom="4"><arcgis-zoom slot="top-left"></arcgis-zoom><div id="basemapLanguage" slot="top-right"><calcite-label>Basemap language</calcite-label><calcite-combobox id="languageCombobox" selection-mode="single" clear-disabled></calcite-combobox></div></arcgis-map><script type="module">const esriRequest = await $arcgis.import("@arcgis/core/request.js");const viewElement = document.querySelector("arcgis-map");viewElement.basemap = {style: {id: "arcgis/outdoor",language: "es", // Spanish language basemap},};// update combobox values with languages from rest endpointconst basemapStylesEndpoint ="https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles/self";esriRequest(basemapStylesEndpoint, {responseType: "json",}).then((response) => {const json = response.data;// add each language as a combobox itemjson.languages.forEach((language, index) => {const comboboxItem = document.createElement("calcite-combobox-item");comboboxItem.value = language.code;comboboxItem.heading = language.name;// if the current basemap language is the same as the combobox item, select itcomboboxItem.selected = viewElement.basemap.style.language === language.code;languageCombobox.appendChild(comboboxItem);});});7 collapsed lines</script></body></html>
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
updateBasemapLanguagethat accepts a language code.81 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Display a map</title><style>html,body {height: 100%;margin: 0;}#basemapLanguage {width: 200px;padding: 10px;background-color: white;}</style><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body lang="es"><arcgis-map basemap="arcgis/topographic" center="13, 46" zoom="4"><arcgis-zoom slot="top-left"></arcgis-zoom><div id="basemapLanguage" slot="top-right"><calcite-label>Basemap language</calcite-label><calcite-combobox id="languageCombobox" selection-mode="single" clear-disabled></calcite-combobox></div></arcgis-map><script type="module">const esriRequest = await $arcgis.import("@arcgis/core/request.js");const viewElement = document.querySelector("arcgis-map");viewElement.basemap = {style: {id: "arcgis/outdoor",language: "es", // Spanish language basemap},};// update combobox values with languages from rest endpointconst basemapStylesEndpoint ="https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles/self";esriRequest(basemapStylesEndpoint, {responseType: "json",}).then((response) => {const json = response.data;// add each language as a combobox itemjson.languages.forEach((language, index) => {const comboboxItem = document.createElement("calcite-combobox-item");comboboxItem.value = language.code;comboboxItem.heading = language.name;// if the current basemap language is the same as the combobox item, select itcomboboxItem.selected = viewElement.basemap.style.language === language.code;languageCombobox.appendChild(comboboxItem);});});const updateLanguage = (languageCode) => {};6 collapsed lines</script></body></html> -
Use the
languageCodevariable to update the application’s language and the language of the basemap style.81 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Display a map</title><style>html,body {height: 100%;margin: 0;}#basemapLanguage {width: 200px;padding: 10px;background-color: white;}</style><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body lang="es"><arcgis-map basemap="arcgis/topographic" center="13, 46" zoom="4"><arcgis-zoom slot="top-left"></arcgis-zoom><div id="basemapLanguage" slot="top-right"><calcite-label>Basemap language</calcite-label><calcite-combobox id="languageCombobox" selection-mode="single" clear-disabled></calcite-combobox></div></arcgis-map><script type="module">const esriRequest = await $arcgis.import("@arcgis/core/request.js");const viewElement = document.querySelector("arcgis-map");viewElement.basemap = {style: {id: "arcgis/outdoor",language: "es", // Spanish language basemap},};// update combobox values with languages from rest endpointconst basemapStylesEndpoint ="https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles/self";esriRequest(basemapStylesEndpoint, {responseType: "json",}).then((response) => {const json = response.data;// add each language as a combobox itemjson.languages.forEach((language, index) => {const comboboxItem = document.createElement("calcite-combobox-item");comboboxItem.value = language.code;comboboxItem.heading = language.name;// if the current basemap language is the same as the combobox item, select itcomboboxItem.selected = viewElement.basemap.style.language === language.code;languageCombobox.appendChild(comboboxItem);});});const updateLanguage = (languageCode) => {// update language in html bodydocument.body.setAttribute("lang", languageCode);// update the basemap languageviewElement.basemap = {style: {id: "arcgis/outdoor",language: languageCode,},};};6 collapsed lines</script></body></html>
Update the language on user input
Use the calciteComboboxChange event to recognize when the user has selected a new language.
- Add an event listener to watch for changes on the combobox. When the combobox value has changed, call the
updateBasemapLanguagefunction to update the basemap language and app locale.81 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Display a map</title><style>html,body {height: 100%;margin: 0;}#basemapLanguage {width: 200px;padding: 10px;background-color: white;}</style><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body lang="es"><arcgis-map basemap="arcgis/topographic" center="13, 46" zoom="4"><arcgis-zoom slot="top-left"></arcgis-zoom><div id="basemapLanguage" slot="top-right"><calcite-label>Basemap language</calcite-label><calcite-combobox id="languageCombobox" selection-mode="single" clear-disabled></calcite-combobox></div></arcgis-map><script type="module">const esriRequest = await $arcgis.import("@arcgis/core/request.js");const viewElement = document.querySelector("arcgis-map");viewElement.basemap = {style: {id: "arcgis/outdoor",language: "es", // Spanish language basemap},};// update combobox values with languages from rest endpointconst basemapStylesEndpoint ="https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles/self";esriRequest(basemapStylesEndpoint, {responseType: "json",}).then((response) => {const json = response.data;// add each language as a combobox itemjson.languages.forEach((language, index) => {const comboboxItem = document.createElement("calcite-combobox-item");comboboxItem.value = language.code;comboboxItem.heading = language.name;// if the current basemap language is the same as the combobox item, select itcomboboxItem.selected = viewElement.basemap.style.language === language.code;languageCombobox.appendChild(comboboxItem);});});const updateLanguage = (languageCode) => {// update language in html bodydocument.body.setAttribute("lang", languageCode);// update the basemap languageviewElement.basemap = {style: {id: "arcgis/outdoor",language: languageCode,},};};const languageCombobox = document.getElementById("languageCombobox");// when the combobox value changes, update the languagelanguageCombobox.addEventListener("calciteComboboxChange", () => {updateLanguage(languageCombobox.value);});7 collapsed lines</script></body></html>
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 language.
What’s Next?
Learn how to use additional SDK features and ArcGIS services in these tutorials: