Skip to content
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
use recursion to reduce long locales by piece
  • Loading branch information
mapmeld committed Jul 26, 2025
commit 055f4115e063499c5f55572170dba9d6bae12957
34 changes: 17 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
/* accept values from common locale selectors */
/* accept lowercased values from common locale selectors */
const ALT_LOCALES = {
'zh-CN': 'zh-Hans',
'zh-Hans-CN': 'zh-Hans',
'zh-HK': 'zh-Hant',
'zh-Hant-HK': 'zh-Hant',
'zh-TW': 'zh-Hant',
'zh-Hant-TW': 'zh-Hant',
'zh-cn': 'zh-Hans',
'zh-hk': 'zh-Hant',
'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.

};

/**
Expand Down Expand Up @@ -125,8 +122,11 @@ function findStreetsSource(style) {
* @returns {object} the modified style
*/
MapboxLanguage.prototype.setLanguage = function (style, language) {
language = ALT_LOCALES[language] || language;
language = ALT_LOCALES[language.toLowerCase()] || language;
if (this.supportedLanguages.indexOf(language) < 0) {
if (language.indexOf('-') > -1) {
return this.setLanguage(style, language.slice(0, language.lastIndexOf('-')));
}
throw new Error(`Language ${ language } is not supported`);
}
const streetsSource = this._languageSource || findStreetsSource(style);
Expand Down Expand Up @@ -154,16 +154,16 @@ MapboxLanguage.prototype._initialStyleUpdate = function () {
this._map.setStyle(this.setLanguage(style, language));
};

function browserLanguage(supportedLanguages) {
let language = navigator.languages ? navigator.languages[0] : (navigator.language || navigator.userLanguage);
language = ALT_LOCALES[language] || language;
const parts = language && language.split('-');
let languageCode = language;
if (parts.length > 1) {
languageCode = parts[0];
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;

if (supportedLanguages.indexOf(language) > -1) {
return language;
}
if (supportedLanguages.indexOf(languageCode) > -1) {
return languageCode;
if (language.indexOf('-') > -1) {
// reduce longer locales (en-US, zh-Hant-TW) to shorter forms
return browserLanguage(supportedLanguages, language.slice(0, language.lastIndexOf('-')));
Copy link
Contributor

Choose a reason for hiding this comment

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

This works, though reduce() would be more succinct, or just a for loop incrementing the end of the range of parts.join('-') to look up.

Copy link
Author

Choose a reason for hiding this comment

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

I changed these to while loops and added tests for "en-US" -> "en" and "zh-HK" to "zh-Hant"
MapBox page showing name_zh-Hant is correct label: https://docs.mapbox.com/data/tilesets/reference/mapbox-streets-v8/

}
return null;
}
Expand Down