A lightweight translation helper for Leaflet with built-in presets and support for custom locales and translations.
Note
Translations of existing controls will not change, but translations for future calls will use the new locale (like in plugins)
translate(key)a function that accepts a source-text key. If the key is missing from the active dictionary, it returns the key unchanged.setLocale(locale)changes the active locale. The locale must already be registered.getLocale()returns the active locale identifier.addLocale(locale, translations)registers or replaces a locale.translationsis an object whose keys are source text and whose values are translated strings.getLocales()returns the registry of locale dictionaries.
You need to set the active locale and then pass the translate method to the map option translate:
import { translate, setLocale } from 'leaflet.translations';
setLocale('de');
const map = new LeafletMap('map', { translate: translate })Register a custom locale before selecting it:
import { translate, setLocale, addLocale } from 'leaflet.translations';
addLocale('de', {
'Map': 'Karte',
'Zoom in': 'Vergrößern'
});
setLocale('de');You can use the Leaflet built-in translate method on the map or the one from this package:
console.log(map.translate('Map')); // "Karte"
console.log(translate('Map')); // "Karte"addLocale('en', {
...getLocales().en,
'some.plugin.text': 'Search'
});
console.log(map.translate('some.plugin.text')); // "Search"cz: Czechda: Danishde: Germanel: Greeken: English (default)es: Spanishfa: Persian/Farsifi: Finnishfr: Frenchhu: Hungarianid: Indonesianit: Italianja: Japaneseko: Koreanky: Kyrgyznl: Dutchno: Norwegianpl: Polishpt_br: Portuguese (Brazil)pt_pt: Portuguese (Portugal)ro: Romanianru: Russiansv: Swedishtr: Turkishuk: Ukrainian (alternative code)zh: Chinese (Simplified)zh_tw: Chinese (Traditional)