Skip to main content

How to add translations to the MapLibre UI?

How do translations work in MapLibre

export const defaultLocale = {
  "AttributionControl.ToggleAttribution": "Toggle attribution",
  "AttributionControl.MapFeedback": "Map feedback",
  "FullscreenControl.Enter": "Enter fullscreen",
  "FullscreenControl.Exit": "Exit fullscreen",
  "GeolocateControl.FindMyLocation": "Find my location",
  "GeolocateControl.LocationNotAvailable": "Location not available",
  "LogoControl.Title": "MapLibre logo",
  "Map.Title": "Map",
  "Marker.Title": "Map marker",
  "NavigationControl.ResetBearing": "Reset bearing to north",
  "NavigationControl.ZoomIn": "Zoom in",
  "NavigationControl.ZoomOut": "Zoom out",
  "Popup.Close": "Close popup",
  "ScaleControl.Feet": "ft",
  "ScaleControl.Meters": "m",
  "ScaleControl.Kilometers": "km",
  "ScaleControl.Miles": "mi",
  "ScaleControl.NauticalMiles": "nm",
  "GlobeControl.Enable": "Enable globe",
  "GlobeControl.Disable": "Disable globe",
  "TerrainControl.Enable": "Enable terrain",
  "TerrainControl.Disable": "Disable terrain",
  "CooperativeGesturesHandler.WindowsHelpText":
    "Use Ctrl + scroll to zoom the map",
  "CooperativeGesturesHandler.MacHelpText": "Use ⌘ + scroll to zoom the map",
  "CooperativeGesturesHandler.MobileHelpText":
    "Use two fingers to move the map",
};
import maplibregl from "maplibre-gl";
// fr.ts contains the same content as above, translated
import { fr } from "./locales/fr.ts";

new maplibregl.Map({
  container: "map",
  style: "https://demotiles.maplibre.org/globe.json",
  center: [0, 0],
  zoom: 2,
  locale: fr, // Use the translation here
});

Introducing maplibre-ui-translations

Example:

import maplibregl from "maplibre-gl";
import { defaultLocale } from "maplibre-gl/src/ui/default_locale";
import {
  updateMaplibreLocale,
  maplibreLocales,
} from "maplibre-ui-translations";

const map = new maplibregl.Map({
  container: "map",
  style: "https://demotiles.maplibre.org/globe.json",
  center: [0, 0],
  zoom: 2,
  locale: defaultLocale,
});

document.querySelector("#lang-switcher")?.addEventListener("change", (e) => {
  const selectedCode = (e.target as HTMLSelectElement).value;
  updateMaplibreLocale(map, selectedCode);
});

Your help is needed!