Skip to content

Commit 93be335

Browse files
committed
Replaced mapbox-gl-language with Diplomat
1 parent d5e7744 commit 93be335

File tree

4 files changed

+29
-241
lines changed

4 files changed

+29
-241
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ The map can optionally animate if you specify the following parameters:
4949

5050
### Language
5151

52-
By default, map labels appear in your preferred language according to [your browser preferences](https://www.w3.org/International/questions/qa-lang-priorities#changing). You can also override this preference by setting the `language` parameter to an ISO 639 language code. For example, add `&language=grc&date=-0999` to see [Middle Babylon labeled in Ancient Greek](https://embed.openhistoricalmap.org/#map=14/32.5423/44.42123&language=grc&date=-0999) or `&language=la&date=-0999` to see it [in Latin](https://embed.openhistoricalmap.org/#map=14/32.5423/44.42123&date=-0999&language=la) instead of the contemporary cuneiform. If OHM doesn’t have the name of a place in this preferred language, the label appears in the contemporary local language as a last resort. To force the display of names in contemporary local languages, set the `language` parameter to `mul` (the ISO 639 code for multilingual content).
52+
By default, map labels appear in your preferred language according to [your browser preferences](https://www.w3.org/International/questions/qa-lang-priorities#changing). You can also override this preference by setting the `language` parameter to an [IETF language tag](https://en.wikipedia.org/wiki/IETF_language_tag) (such as an ISO 639 language code). For example, add `&language=grc&date=-0999` to see [Middle Babylon labeled in Ancient Greek](https://embed.openhistoricalmap.org/#map=14/32.5423/44.42123&language=grc&date=-0999) or `&language=la&date=-0999` to see it [in Latin](https://embed.openhistoricalmap.org/#map=14/32.5423/44.42123&date=-0999&language=la) instead of the contemporary cuneiform. If OHM doesn’t have the name of a place in this preferred language, the label appears in the contemporary local language as a last resort. To force the display of names in contemporary local languages, set the `language` parameter to `mul` (the ISO 639 code for multilingual content).
5353

5454
## Embedding
5555

index.js

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { filterByDate, dateRangeFromISODate } from '@openhistoricalmap/maplibre-gl-dates';
22
import maplibregl from 'maplibre-gl';
33
import 'maplibre-gl/dist/maplibre-gl.css';
4-
import MapboxLanguage from '@mapbox/mapbox-gl-language';
4+
import { localizeStyle, getLocales } from '@americana/diplomat';
55

66
var attribution = '<a href="https://www.openhistoricalmap.org/copyright">OpenHistoricalMap</a>';
77
var stylesByLayer = {
@@ -45,23 +45,6 @@ addEventListener('load', function () {
4545
map.addControl(new maplibregl.GlobeControl(), 'top-left');
4646
map.addControl(new maplibregl.FullscreenControl(), 'top-left');
4747

48-
let languageCode = params.get('language');
49-
let language = new MapboxLanguage({
50-
defaultLanguage: languageCode,
51-
supportedLanguages: languageCode ? [languageCode] : undefined,
52-
languageSource: 'osm',
53-
getLanguageField: (languageCode) => {
54-
if (languageCode === 'mul') {
55-
return 'name';
56-
} else {
57-
// Optimistically follow the pattern in the tiler tag mapping without hard-coding the specific table columns.
58-
// https://github.com/OpenHistoricalMap/ohm-deploy/blob/main/images/tiler-server/config/languages.sql
59-
return 'name_' + languageCode.replace('-', '_').toLowerCase();
60-
}
61-
},
62-
});
63-
map.addControl(language);
64-
6548
let
6649
markerLongitude = parseFloat(params.get('mlon')),
6750
markerLatitude = parseFloat(params.get('mlat')),
@@ -82,6 +65,10 @@ addEventListener('load', function () {
8265
.addTo(map);
8366
}
8467

68+
let localizationOptions = {
69+
localizedNamePropertyFormat: "name_$1",
70+
};
71+
8572
map.once('styledata', function (event) {
8673
if (params.get('projection')) {
8774
map.setProjection({
@@ -96,6 +83,13 @@ addEventListener('load', function () {
9683

9784
let date = params.get('date') || new Date();
9885
filterByDate(map, date);
86+
87+
localizeStyle(map, getLocales(), {
88+
...localizationOptions,
89+
sourceLayers: ["place_points_centroids"],
90+
glossLocalNames: true,
91+
});
92+
localizeStyle(map, getLocales(), localizationOptions);
9993
});
10094

10195
addEventListener('hashchange', function (event) {
@@ -106,13 +100,7 @@ addEventListener('load', function () {
106100
let oldLanguageCode = oldParams.get('language');
107101
let newLanguageCode = newParams.get('language');
108102
if (oldLanguageCode !== newLanguageCode) {
109-
if (!language.supportedLanguages.includes(newLanguageCode)) {
110-
// mapbox-gl-language assumes a limited set of language fields that is known in advance, as is the case with the Mapbox Streets source. But OHM tiles support hundreds of sparsely populated fields.
111-
language.supportedLanguages.push(newLanguageCode);
112-
}
113-
let newStyle = language.setLanguage(map.getStyle(), newLanguageCode);
114-
// Style diffing seems to miss changes to expression variable values for some reason.
115-
map.setStyle(newStyle, { diff: false });
103+
localizeStyle(map, getLocales(), localizationOptions);
116104
}
117105

118106
if (oldParams.get('projection') !== newParams.get('projection')) {
@@ -137,6 +125,10 @@ addEventListener('load', function () {
137125
});
138126
});
139127

128+
addEventListener("languagechange", function (event) {
129+
localizeStyle(map, getLocales(), localizationOptions);
130+
});
131+
140132
function upgradeLegacyHash() {
141133
var hash = location.hash.substring(1);
142134
if (!hash.includes('=')) {

0 commit comments

Comments
 (0)