From d8bda5bed8e35babd0aefa42b5e27971d7be4ced Mon Sep 17 00:00:00 2001 From: Scott Farley Date: Wed, 13 Mar 2019 14:32:35 -0700 Subject: [PATCH 1/4] localize placeholder based on first language, #150 --- lib/index.js | 23 +++++++++++++++++++++-- lib/localization.js | 33 +++++++++++++++++++++++++++++++++ package-lock.json | 7 ++++++- package.json | 1 + test/test.geocoder.js | 9 +++++++++ test/test.ui.js | 24 ++++++++++++++++++++++++ 6 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 lib/localization.js diff --git a/lib/index.js b/lib/index.js index 3c62ebd1..ffc2a471 100644 --- a/lib/index.js +++ b/lib/index.js @@ -8,6 +8,8 @@ var exceptions = require('./exceptions'); var MapboxClient = require('@mapbox/mapbox-sdk'); var mbxGeocoder = require('@mapbox/mapbox-sdk/services/geocoding'); var MapboxEventManager = require('./events'); +var localization = require('./localization'); +var subtag = require('subtag'); var geocoderService; /** @@ -56,7 +58,6 @@ function MapboxGeocoder(options) { MapboxGeocoder.prototype = { options: { - placeholder: 'Search', zoom: 16, flyTo: true, trackProximity: false, @@ -92,7 +93,7 @@ MapboxGeocoder.prototype = { this._inputEl = document.createElement('input'); this._inputEl.type = 'text'; - this._inputEl.placeholder = this.options.placeholder; + this._inputEl.placeholder = this.getPlaceholderText(); this._inputEl.addEventListener('keydown', this._onKeyDown); this._inputEl.addEventListener('change', this._onChange); @@ -388,6 +389,24 @@ MapboxGeocoder.prototype = { return this.options.proximity; }, + /** + * Get the text to use as the search bar placeholder + * + * If placeholder is provided in options, then use options.placeholder + * Otherwise, if language is provided in options, then use the localized string of the first language if available + * Otherwise use the default + */ + getPlaceholderText: function(){ + if (this.options.placeholder) return this.options.placeholder; + if (this.options.language){ + var firstLanguage = this.options.language.split(",")[0]; + var language = subtag.language(firstLanguage); + var localizedValue = localization.placeholder[language]; + if (localizedValue) return localizedValue; + } + return 'Search'; + }, + /** * Subscribe to events that happen within the plugin. * @param {String} type name of event. Available events and the data passed into their respective event objects are: diff --git a/lib/localization.js b/lib/localization.js new file mode 100644 index 00000000..0e9422dc --- /dev/null +++ b/lib/localization.js @@ -0,0 +1,33 @@ +'use strict'; + +/** + * Localized values for the placeholder string + */ +var placeholder = { + // list drawn from https://docs.mapbox.com/api/search/#language-coverage + 'de': 'Suche', // german + 'it': 'Ricerca', //italian + 'en': 'Search', // english + 'nl': 'Zoeken', //dutch + 'fr': 'Chercher', //french + 'ca': 'Cerca', //catalan + 'he': 'לחפש', //hebrew + 'ja': 'サーチ', //japanese + 'lv': 'Meklēt', //latvian + 'pt': 'Procurar', //portuguese + 'sr': 'Претрага', //serbian + 'zh': '搜索', //chinese-simplified + 'cs': 'Vyhledávání', //czech + 'hu': 'Keresés', //hungarian + 'ka': 'ძიება', // georgian + 'nb': 'Søke', //norwegian + 'sk': 'Vyhľadávanie', //slovak + 'th': 'ค้นหา', //thai + 'fi': 'Hae',//finnish + 'is': 'Leita',//icelandic + 'ko': '수색',//korean + 'pl': 'Szukaj', //polish + 'sl': 'Iskanje' //slovenian +} + +module.exports = {placeholder: placeholder}; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index ce7baa39..092a6566 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@mapbox/mapbox-gl-geocoder", - "version": "3.1.1", + "version": "3.1.4", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -9961,6 +9961,11 @@ "minimist": "^1.1.0" } }, + "subtag": { + "version": "0.5.0", + "resolved": "https://registry.npmjs.org/subtag/-/subtag-0.5.0.tgz", + "integrity": "sha512-CaIBcTSb/nyk4xiiSOtZYz1B+F12ZxW8NEp54CdT+84vmh/h4sUnHGC6+KQXUfED8u22PQjCYWfZny8d2ELXwg==" + }, "suggestions": { "version": "1.3.3", "resolved": "https://registry.npmjs.org/suggestions/-/suggestions-1.3.3.tgz", diff --git a/package.json b/package.json index cc6d854d..26cbbe10 100644 --- a/package.json +++ b/package.json @@ -55,6 +55,7 @@ "lodash.debounce": "^4.0.6", "request": "^2.88.0", "sinon": "^7.2.3", + "subtag": "^0.5.0", "suggestions": "^1.3.2", "xtend": "^4.0.1" } diff --git a/test/test.geocoder.js b/test/test.geocoder.js index 19c885b1..b93c5f87 100644 --- a/test/test.geocoder.js +++ b/test/test.geocoder.js @@ -6,6 +6,7 @@ var mapboxgl = require('mapbox-gl'); var once = require('lodash.once'); var mapboxEvents = require('./../lib/events'); var sinon = require('sinon'); +var localization = require('./../lib/localization'); mapboxgl.accessToken = process.env.MapboxAccessToken; @@ -431,5 +432,13 @@ test('geocoder', function(tt) { ); }); + tt.test('placeholder localization', (t)=>{ + var ensureLanguages = ['de', 'en', 'fr', 'it', 'nl', 'ca', 'cs', 'fr', 'he', 'hu', 'is', 'ja', 'ka', 'ko', 'lv', 'ka', 'ko', 'lv', 'nb', 'pl', 'pt', 'sk', 'sl', 'sr', 'th', 'zh']; + ensureLanguages.forEach(function(languageTag){ + t.equals(typeof(localization.placeholder[languageTag]), 'string', 'localized placeholder value is present for language=' + languageTag); + }); + t.end(); + }); + tt.end(); }); diff --git a/test/test.ui.js b/test/test.ui.js index 2e6ac241..2444ddbc 100644 --- a/test/test.ui.js +++ b/test/test.ui.js @@ -84,5 +84,29 @@ test('Geocoder#inputControl', function(tt) { t.end(); }); + tt.test('placeholder language localization', function(t){ + t.plan(1); + setup({language: 'de-DE'}); + t.equal( + map.getContainer().querySelector('.mapboxgl-ctrl-geocoder input') + .placeholder, + 'Suche', + 'placeholder is localized based on language' + ); + t.end(); + }); + + tt.test('placeholder language localization with more than one language specified', function(t){ + t.plan(1); + setup({language: 'de-DE,lv'}); + t.equal( + map.getContainer().querySelector('.mapboxgl-ctrl-geocoder input') + .placeholder, + 'Suche', + 'placeholder is localized based on language' + ); + t.end(); + }) + tt.end(); }); From 4e1b2a69c7831b40dfbca13ff832c4f022880986 Mon Sep 17 00:00:00 2001 From: Scott Farley Date: Thu, 14 Mar 2019 13:42:46 -0700 Subject: [PATCH 2/4] get placeholder is a private method --- lib/index.js | 5 ++++- lib/localization.js | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index ffc2a471..95f1df3f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -395,8 +395,11 @@ MapboxGeocoder.prototype = { * If placeholder is provided in options, then use options.placeholder * Otherwise, if language is provided in options, then use the localized string of the first language if available * Otherwise use the default + * + * @returns {String} the value to use as the search bar placeholder + * @private */ - getPlaceholderText: function(){ + _getPlaceholderText: function(){ if (this.options.placeholder) return this.options.placeholder; if (this.options.language){ var firstLanguage = this.options.language.split(",")[0]; diff --git a/lib/localization.js b/lib/localization.js index 0e9422dc..71b7f13e 100644 --- a/lib/localization.js +++ b/lib/localization.js @@ -2,6 +2,8 @@ /** * Localized values for the placeholder string + * + * @private */ var placeholder = { // list drawn from https://docs.mapbox.com/api/search/#language-coverage From 7ac2a10e19d8989756fcaa9c4d27dfab17f08ead Mon Sep 17 00:00:00 2001 From: Scott Farley Date: Thu, 14 Mar 2019 13:46:47 -0700 Subject: [PATCH 3/4] fix tests --- lib/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 95f1df3f..ac454455 100644 --- a/lib/index.js +++ b/lib/index.js @@ -93,7 +93,7 @@ MapboxGeocoder.prototype = { this._inputEl = document.createElement('input'); this._inputEl.type = 'text'; - this._inputEl.placeholder = this.getPlaceholderText(); + this._inputEl.placeholder = this._getPlaceholderText(); this._inputEl.addEventListener('keydown', this._onKeyDown); this._inputEl.addEventListener('change', this._onChange); From 8ba23eaf95c51c17daf811b781c88b576796081f Mon Sep 17 00:00:00 2001 From: Scott Farley Date: Thu, 14 Mar 2019 13:47:03 -0700 Subject: [PATCH 4/4] update changelog with new changes --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a170145..c77b3ceb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## Unreleased +- Localize placeholder based on language set in constructor options [#150](https://github.com/mapbox/mapbox-gl-geocoder/issues/150) + ## v3.1.4 - Emit a `clear` event when the user backspaces into an empty search bar or selects all existing text and deletes it.