Skip to content
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

Add local language support #17

Merged
merged 4 commits into from
Jul 6, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ Showcasing the languages supported by Mapbox Streets.
- [German](https://mapbox.github.io/mapbox-gl-language/examples/de.html)
- [Japanese](https://mapbox.github.io/mapbox-gl-language/examples/ja.html)
- [Korean](https://mapbox.github.io/mapbox-gl-language/examples/ko.html)
- [Multilingual](https://mapbox.github.io/mapbox-gl-language/examples/multilingual.html)
- [Portuguese](https://mapbox.github.io/mapbox-gl-language/examples/pt.html)
- [Russian](https://mapbox.github.io/mapbox-gl-language/examples/ru.html)
- [Spanish](https://mapbox.github.io/mapbox-gl-language/examples/es.html)
Expand Down
34 changes: 34 additions & 0 deletions examples/multilingual.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html>
<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/v0.36.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.36.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>

<div id='map'></div>
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoibHVrYXNtYXJ0aW5lbGxpIiwiYSI6ImNpem85dmhwazAyajIyd284dGxhN2VxYnYifQ.HQCmyhEXZUTz3S98FMrVAQ';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v10',
center: [-98, 38.88],
minZoom: 2,
zoom: 3
});
mapboxgl.setRTLTextPlugin('https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-rtl-text/v0.1.0/mapbox-gl-rtl-text.js');
map.addControl(new MapboxLanguage({
defaultLanguage: 'mul'
}));
</script>

</body>
</html>
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function MapboxLanguage(options) {
this._defaultLanguage = options.defaultLanguage;
this._isLanguageField = options.languageField || /^\{name/;
this._getLanguageField = options.getLanguageField || function nameField(language) {
return '{name_' + language + '}';
return language === 'mul' ? '{name}' : '{name_' + language + '}';
};
this._languageSource = options.languageSource || null;
this._languageTransform = options.languageTransform || function (style, language) {
Expand All @@ -34,7 +34,7 @@ function MapboxLanguage(options) {
}
};
this._excludedLayerIds = options.excludedLayerIds || [];
this.supportedLanguages = options.supportedLanguages || ['ar', 'en', 'es', 'fr', 'de', 'ja', 'ko', 'pt', 'ru', 'zh'];
this.supportedLanguages = options.supportedLanguages || ['ar', 'en', 'es', 'fr', 'de', 'ja', 'ko', 'mul', 'pt', 'ru', 'zh'];
}

function standardSpacing(style) {
Expand Down
16 changes: 14 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ test('setLanguage for different text fields', (assert) => {
[6, '{name_es}']
]
}
}, 'switch style to spanish name field');
}, 'switch style to english name field');

var arStyle = language.setLanguage(style, 'ar');
assert.deepEqual(arStyle.layers[0].layout, {
Expand All @@ -53,7 +53,19 @@ test('setLanguage for different text fields', (assert) => {
[6, '{name_ar}']
]
}
}, 'switch style to spanish arabic field');
}, 'switch style to arabic name field');

var mulStyle = language.setLanguage(style, 'mul');
assert.deepEqual(mulStyle.layers[0].layout, {
'text-letter-spacing': 0.15,
'text-field': {
'base': 1,
'stops': [
[0, '{abbr}'],
[6, '{name}']
]
}
}, 'switch style to multilingual name field');

assert.end();
});
Expand Down