-
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 4 commits
2e85685
5c1f5d9
a9b8f5e
055f411
64fd04c
930ae1b
1664478
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,10 @@ | ||
| /* accept lowercased values from common locale selectors */ | ||
| const ALT_LOCALES = { | ||
| 'zh-cn': 'zh-Hans', | ||
| 'zh-hk': 'zh-Hant', | ||
| 'zh-tw': 'zh-Hant', | ||
| }; | ||
|
|
||
| /** | ||
| * 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. | ||
|
|
@@ -115,7 +122,13 @@ 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; | ||
| 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); | ||
| if (!streetsSource) return style; | ||
|
|
||
|
|
@@ -141,15 +154,16 @@ 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]; | ||
| 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.