Skip to content

Commit

Permalink
update language switching
Browse files Browse the repository at this point in the history
  • Loading branch information
zdila committed Feb 5, 2025
1 parent 79ac1ff commit fd2e562
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
32 changes: 15 additions & 17 deletions app/assets/javascripts/leaflet.map.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,24 @@ if (OSM.MAPTILER_KEY) {
style: "https://api.maptiler.com/maps/openstreetmap/style.json?key=" + OSM.MAPTILER_KEY
},
onAdd: function (map) {
var supportedLanguages = "am,ar,az,be,bg,br,bs,ca,co,cs,cy,da,de,el,en,eo,es,et,eu,fi,fr,fy,ga,gd,he,hi,hr,hu,hy,id,is,it,ja,ja_kana,ja_rm,ja-Latn,ja-Hira,ka,kk,kn,ko,ko-Latn,ku,la,lb,lt,lv,mk,mt,ml,nl,no,oc,pl,pt,rm,ro,ru,sk,sl,sq,sr,sr-Latn,sv,ta,te,th,tr,uk,zh".split(",");
L.MaplibreGL.prototype.onAdd.call(this, map);
var m = this.getMaplibreMap();
m.on("load", function () {
var locale;
if (supportedLanguages.includes(I18n.locale)) {
locale = I18n.locale;
} else {
var mainLocale = I18n.locale.slice(0, 2);
var localeIndex = supportedLanguages.findIndex(function (locale) { return locale.slice(0, 2) === mainLocale; });
if (localeIndex > -1) {
locale = supportedLanguages[localeIndex];
}
}

if (locale) {
m.setLanguage(locale);
var supportedLanguages = maplibregl.Map.prototype.supportedLanguages;
var language;
if (supportedLanguages.includes(I18n.locale)) {
language = I18n.locale;
} else {
var mainLocale = I18n.locale.slice(0, 2);
var localeIndex = supportedLanguages.findIndex(function (locale) {
return locale.slice(0, 2) === mainLocale;
});
if (localeIndex > -1) {
language = supportedLanguages[localeIndex];
}
});
L.MaplibreGL.prototype._update.call(this, map);
}
if (language) {
m.setLanguage(language);
}
},
onRemove: function (map) {
L.MaplibreGL.prototype.onRemove.call(this, map);
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
"name": "openstreetmap",
"private": true,
"dependencies": {
"@maplibre/maplibre-gl-leaflet": "^0.0.17",
"@maptiler/maplibre-gl-omt-language": "^0.0.1",
"@maplibre/maplibre-gl-leaflet": "^0.0.22",
"@maptiler/maplibre-gl-omt-language": "^0.0.2",
"jquery-simulate": "^1.0.2",
"js-cookie": "^3.0.0",
"leaflet": "^1.8.0",
"leaflet.locatecontrol": "^0.83.0",
"maplibre-gl": "^2.4.0",
"maplibre-gl": "^4.7.1",
"osm-community-index": "^5.2.0"
},
"devDependencies": {
Expand Down

0 comments on commit fd2e562

Please sign in to comment.