Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions examples/zh-HK.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!DOCTYPE html>
<html lang='zh-HK'>
<head>
<meta charset='utf-8' />
<title>Mapbox GL Language</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v2.4.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v2.4.0/mapbox-gl.css' rel='stylesheet' />
<script src='../index.js'></script>
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body lang='zh-HK'>

<div id='map'></div>
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoibHVrYXNtYXJ0aW5lbGxpIiwiYSI6ImNpem85dmhwazAyajIyd284dGxhN2VxYnYifQ.HQCmyhEXZUTz3S98FMrVAQ';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v11',
center: [114.3, 22.3],
minZoom: 2,
zoom: 9,
language: 'zh-HK',
});
mapboxgl.setRTLTextPlugin('https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-rtl-text/v0.2.3/mapbox-gl-rtl-text.js');
map.addControl(new MapboxLanguage({
defaultLanguage: 'zh-HK'
}));
</script>

</body>
</html>
41 changes: 30 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/* accept lowercased values from common locale selectors */
const ALT_LOCALES = {
'zh-cn': 'zh-Hans',
'zh-hk': 'zh-Hant',
'zh-mo': 'zh-Hant',
'zh-sg': 'zh-Hans',
'zh-tw': 'zh-Hant',
Comment on lines 3 to 7
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how common it is, but zh-SG and zh-MO normally map to zh-Hans and zh-Hant, respectively, based on official status.

};

/**
* Create a new [Mapbox GL JS plugin](https://www.mapbox.com/blog/build-mapbox-gl-js-plugins/) that
* modifies the layers of the map style to use the `text-field` that matches the browser language.
Expand Down Expand Up @@ -115,7 +124,15 @@ function findStreetsSource(style) {
* @returns {object} the modified style
*/
MapboxLanguage.prototype.setLanguage = function (style, language) {
if (this.supportedLanguages.indexOf(language) < 0) throw new Error(`Language ${ language } is not supported`);
language = ALT_LOCALES[language.toLowerCase()] || language;

while (this.supportedLanguages.indexOf(language) < 0) {
if (language.indexOf('-') > -1) {
language = language.slice(0, language.lastIndexOf('-'));
} else {
throw new Error(`Language ${ language } is not supported`);
}
}
const streetsSource = this._languageSource || findStreetsSource(style);
if (!streetsSource) return style;

Expand All @@ -141,17 +158,19 @@ MapboxLanguage.prototype._initialStyleUpdate = function () {
this._map.setStyle(this.setLanguage(style, language));
};

function browserLanguage(supportedLanguages) {
const language = navigator.languages ? navigator.languages[0] : (navigator.language || navigator.userLanguage);
const parts = language && language.split('-');
let languageCode = language;
if (parts.length > 1) {
languageCode = parts[0];
}
if (supportedLanguages.indexOf(languageCode) > -1) {
return languageCode;
function browserLanguage(supportedLanguages, language) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function isn’t recursive anymore, so nothing ever passes in language anymore. language can go back to being a local constant.

language = language || (navigator.languages ? navigator.languages[0] : (navigator.language || navigator.userLanguage));
language = ALT_LOCALES[language.toLowerCase()] || language;

while (supportedLanguages.indexOf(language) < 0) {
if (language.indexOf('-') > -1) {
// reduce longer locales (en-US, zh-Hant-TW) to shorter forms
language = language.slice(0, language.lastIndexOf('-'));
} else {
return null;
}
}
return null;
return language;
}

MapboxLanguage.prototype.onAdd = function (map) {
Expand Down
35 changes: 35 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,40 @@ test('MapboxLanguage', (t) => {
t.end();
});

test('longer locales', (t) => {
const language = new MapboxLanguage();
const layers = [{
'id': 'state-label-sm',
'source': 'composite',
'source-layer': 'state_label',
'layout': {
'text-letter-spacing': 0.15,
'text-field': ['get', 'name']
}
}];
const style = makeStyle(layers);

const enStyle = language.setLanguage(style, 'en-US');
t.deepEqual(enStyle.layers[0].layout, {
'text-letter-spacing': 0.15,
'text-field': [
'coalesce',
['get', 'name_en'],
['get', 'name']
]
}, 'shorter locale name');

const zhStyle = language.setLanguage(style, 'zh-HK');
t.deepEqual(zhStyle.layers[0].layout, {
'text-letter-spacing': 0.15,
'text-field': [
'coalesce',
['get', 'name_zh-Hant'],
['get', 'name']
]
}, 'zh hans/hant name');
t.end();
});

t.end();
});