-
Notifications
You must be signed in to change notification settings - Fork 50
Handle alternate locale names, long locale names #67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
2e85685
5c1f5d9
a9b8f5e
055f411
64fd04c
930ae1b
1664478
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| 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', | ||
| }; | ||
|
|
||
| /** | ||
|
|
@@ -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); | ||
|
|
@@ -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) { | ||
|
||
| 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('-'))); | ||
|
||
| } | ||
| return null; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
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-SGandzh-MOnormally map tozh-Hansandzh-Hant, respectively, based on official status.