-
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
Open
mapmeld
wants to merge
7
commits into
mapbox:master
Choose a base branch
from
mapmeld:zh
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2e85685
use synonyms to accept zh-HK
mapmeld 5c1f5d9
include HK example
mapmeld a9b8f5e
rename ALT_LOCALES
mapmeld 055f411
use recursion to reduce long locales by piece
mapmeld 64fd04c
add locales, change recursive calls to a while loop
mapmeld 930ae1b
add tests
mapmeld 1664478
remove change to browserLanguagefn
mapmeld File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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', | ||
| }; | ||
|
|
||
| /** | ||
| * 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 +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; | ||
|
|
||
|
|
@@ -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) { | ||
|
||
| 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) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.