Skip to content

Commit

Permalink
add option to exclude layers from translation
Browse files Browse the repository at this point in the history
Implements #7
  • Loading branch information
Knut Hühne authored and lukasmartinelli committed Jan 18, 2018
1 parent a1f1be0 commit 9410fd1
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 9 deletions.
9 changes: 6 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* @param {Function} [options.getLanguageField] - Given a language choose the field in the vector tiles
* @param {string} [options.languageSource] - Name of the source that contains the different languages.
* @param {string} [options.defaultLanguage] - Name of the default language to initialize style after loading.
* @param {string[]} [options.excludedLayerIds] - Name of the layers that should be excluded from translation.
*/
function MapboxLanguage(options) {
options = Object.assign({}, options);
Expand All @@ -32,6 +33,7 @@ function MapboxLanguage(options) {
return standardSpacing(style);
}
};
this._excludedLayerIds = options.excludedLayerIds || [];
this.supportedLanguages = options.supportedLanguages || ['en', 'es', 'fr', 'de', 'ru', 'zh', 'ar', 'pt'];
}

Expand Down Expand Up @@ -137,8 +139,8 @@ function adaptPropertyLanguage(isLangField, property, languageFieldName) {
return property;
}

function changeLayerTextProperty(isLangField, layer, languageFieldName) {
if (layer.layout && layer.layout['text-field']) {
function changeLayerTextProperty(isLangField, layer, languageFieldName, excludedLayerIds) {
if (layer.layout && layer.layout['text-field'] && excludedLayerIds.indexOf(layer.id) === -1) {
return Object.assign({}, layer, {
layout: Object.assign({}, layer.layout, {
'text-field': adaptPropertyLanguage(isLangField, layer.layout['text-field'], languageFieldName)
Expand Down Expand Up @@ -169,8 +171,9 @@ MapboxLanguage.prototype.setLanguage = function (style, language) {

var field = this._getLanguageField(language);
var isLangField = this._isLanguageField;
var excludedLayerIds = this._excludedLayerIds;
var changedLayers = style.layers.map(function (layer) {
if (layer.source === streetsSource) return changeLayerTextProperty(isLangField, layer, field);
if (layer.source === streetsSource) return changeLayerTextProperty(isLangField, layer, field, excludedLayerIds);
return layer;
});

Expand Down
67 changes: 61 additions & 6 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
var test = require('tape');
var MapboxLanguage = require('../index');

function makeStyle(layer) {
function makeStyle(layers) {
return {
sources: {
composite: {
type: 'vector',
url: 'mapbox://mapbox-streets-v7'
}
},
layers: [layer]
layers: layers
};


}

test('setLanguage for different text fields', (assert) => {
var language = new MapboxLanguage();
var style = makeStyle({
var style = makeStyle([{
'id': 'state-label-sm',
'source': 'composite',
'source-layer': 'state_label',
Expand All @@ -31,7 +29,7 @@ test('setLanguage for different text fields', (assert) => {
]
}
}
});
}]);

var enStyle = language.setLanguage(style, 'es');
assert.deepEqual(enStyle.layers[0].layout, {
Expand Down Expand Up @@ -59,3 +57,60 @@ test('setLanguage for different text fields', (assert) => {

assert.end();
});

test('setLanguage with excluded layers', (assert) => {
var language = new MapboxLanguage({excludedLayerIds: ['state-label-lg']});
var style = makeStyle([{
'id': 'state-label-sm',
'source': 'composite',
'source-layer': 'state_label',
'layout': {
'text-letter-spacing': 0.15,
'text-field': {
'base': 1,
'stops': [
[0, '{abbr}'],
[6, '{name}']
]
}
}
}, {
'id': 'state-label-lg',
'source': 'composite',
'source-layer': 'state_label',
'layout': {
'text-letter-spacing': 0.15,
'text-field': {
'base': 1,
'stops': [
[0, '{abbr}'],
[6, '{name}']
]
}
}
}]);

var esStyle = language.setLanguage(style, 'es');
assert.deepEqual(esStyle.layers[0].layout, {
'text-letter-spacing': 0.15,
'text-field': {
'base': 1,
'stops': [
[0, '{abbr}'],
[6, '{name_es}']
]
}
}, 'switch style to on regular field');

assert.deepEqual(esStyle.layers[1].layout, {
'text-letter-spacing': 0.15,
'text-field': {
'base': 1,
'stops': [
[0, '{abbr}'],
[6, '{name}']
]
}
}, 'do not switch style on excluded field');
assert.end();
});

0 comments on commit 9410fd1

Please sign in to comment.