Skip to content

Commit ba262ad

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

4 files changed

Lines changed: 27 additions & 241 deletions

File tree

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: 15 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')),
@@ -96,6 +79,15 @@ addEventListener('load', function () {
9679

9780
let date = params.get('date') || new Date();
9881
filterByDate(map, date);
82+
83+
localizeStyle(map, getLocales(), {
84+
sourceLayers: ["place_points_centroids"],
85+
glossLocalNames: true,
86+
localizedNamePropertyFormat: "name_$1",
87+
});
88+
localizeStyle(map, getLocales(), {
89+
localizedNamePropertyFormat: "name_$1",
90+
});
9991
});
10092

10193
addEventListener('hashchange', function (event) {
@@ -106,13 +98,7 @@ addEventListener('load', function () {
10698
let oldLanguageCode = oldParams.get('language');
10799
let newLanguageCode = newParams.get('language');
108100
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 });
101+
localizeStyle(map);
116102
}
117103

118104
if (oldParams.get('projection') !== newParams.get('projection')) {
@@ -137,6 +123,10 @@ addEventListener('load', function () {
137123
});
138124
});
139125

126+
addEventListener("languagechange", function (event) {
127+
localizeStyle(map);
128+
});
129+
140130
function upgradeLegacyHash() {
141131
var hash = location.hash.substring(1);
142132
if (!hash.includes('=')) {

0 commit comments

Comments
 (0)