Learn how to change language labels on a basemap.
The ArcGIS Basemap Styles service provides a number of different styles that you can use in your MapLibre GL JS applications. All of these styles support a language
parameter, which allows you to localize place labels into more than 30 different languages.
In this tutorial, set the basemap style preferences to change between different place label languages using the MapLibre ArcGIS plugin.
Prerequisites
You need an ArcGIS Location Platform or ArcGIS Online account.
Steps
Get the starter app
Select a type of authentication and follow the steps to create a new app.
Choose API key authentication if you:
- Want the easiest way to get started.
- Want to build public applications that access ArcGIS Location Services and secure items.
- Have an ArcGIS Location Platform or ArcGIS Online account.
Choose user authentication if you:
- Want to build private applications.
- Require application users to sign in with their own ArcGIS account and access resources their behalf.
- Have an ArcGIS Online account.
To learn more about both types of authentication, go to Authentication.
Set up authentication
Set developer credentials
Use the API key or OAuth developer credentials so your application can access ArcGIS services.
Add a language selector
Add a menu that allows users to change display language of your map. To accomplish this, use a standard html <select
element.
-
Create a
<div
tag to contain the> <select
dropdown menu, which you will populate in a later step.> Use dark colors for code blocks </style> <!-- Load MapLibre GL JS from CDN --> <script src="https://unpkg.com/maplibre-gl@5.9.0/dist/maplibre-gl.js"></script> <link href="https://unpkg.com/maplibre-gl@5.9.0/dist/maplibre-gl.css" rel="stylesheet"> <!-- Load MapLibre ArcGIS from CDN --> <script src="https://unpkg.com/@esri/maplibre-arcgis@1.0.0/dist/umd/maplibre-arcgis.min.js"></script> </head> <body> <div id="map"></div> <div id="languages-wrapper"> <select id="languages"></select> </div>
-
In the
<style
element, add CSS styling to the> languages-wrapper
andlanguages
.Use dark colors for code blocks <style> html, body, #map { padding: 0; margin: 0; height: 100%; width: 100%; font-family: Arial, Helvetica, sans-serif; font-size: 14px; color: #323232; } #languages-wrapper { position: absolute; top: 20px; right: 20px; background: rgba(255, 255, 255, 0); } #languages { font-size: 16px; padding: 4px 8px; } </style> <!-- Load MapLibre GL JS from CDN --> <script src="https://unpkg.com/maplibre-gl@5.9.0/dist/maplibre-gl.js"></script> <link href="https://unpkg.com/maplibre-gl@5.9.0/dist/maplibre-gl.css" rel="stylesheet"> <!-- Load MapLibre ArcGIS from CDN --> <script src="https://unpkg.com/@esri/maplibre-arcgis@1.0.0/dist/umd/maplibre-arcgis.min.js"></script>
Set the basemap
Using the MapLibre ArcGIS plugin, reference the Basemap Styles service and a language enumeration to update the map. This will be used when a selection is made.
-
Define a function to update the
style
language to the selected language.Use dark colors for code blocks const setLanguage = (selection) => { // Instantiate the given basemap layer. basemapStyle.updateStyle({ preferences: { language: selection } }); };
-
Select the DOM element used for the language selector.
Use dark colors for code blocks const languageSelectElement = document.querySelector("#languages");
-
Use the MapLibre ArcGIS plugin to make a request to the Basemap Styles service. This dynamically fetches the available languages and populates the
select
element.Use dark colors for code blocks const languageSelectElement = document.querySelector("#languages"); maplibreArcGIS.BasemapStyle.getSelf().then((response) => { response.languages.forEach((language) => { const option = document.createElement("option"); option.value = language.code; option.textContent = language.name; languageSelectElement.appendChild(option); }); });
-
Call
set
to initialize the language labels to the first language in theLanguage select
element.Use dark colors for code blocks maplibreArcGIS.BasemapStyle.getSelf().then((response) => { response.languages.forEach((language) => { const option = document.createElement("option"); option.value = language.code; option.textContent = language.name; languageSelectElement.appendChild(option); }); setLanguage(response.languages[0].code); });
Add an event listener
Use an event listener to register a basemap change in the <select
element and to update the map.
-
Add an event listener to update the map when a new language is selected.
Use dark colors for code blocks const languageSelectElement = document.querySelector("#languages"); maplibreArcGIS.BasemapStyle.getSelf().then((response) => { response.languages.forEach((language) => { const option = document.createElement("option"); option.value = language.code; option.textContent = language.name; languageSelectElement.appendChild(option); }); setLanguage(response.languages[0].code); }); languageSelectElement.addEventListener("change", (e) => { setLanguage(e.target.value); });
Add RTL language support
MapLibre GL JS does not automatically display right-to-left languages such as Arabic and Hebrew correctly. Use the Mapbox GL RTL Text plugin to configure settings for RTL languages.
-
Call the
maplibregl.set
with the URL of the Mapbox GL RTL Text plugin to configure support for RTL languages.RTL Text Plugin Use dark colors for code blocks languageSelectElement.addEventListener("change", (e) => { setLanguage(e.target.value); }); maplibregl.setRTLTextPlugin("https://unpkg.com/@mapbox/mapbox-gl-rtl-text@0.2.3/mapbox-gl-rtl-text.js");
Run the app
Run the app.
You should be able to use the<select >
element to switch between language labels on your basemap.
What's next?
Learn how to use additional location services in these tutorials: