-
Notifications
You must be signed in to change notification settings - Fork 181
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
Localize placeholder based on first language #201
Changes from 1 commit
d8bda5b
4e1b2a6
7ac2a10
8ba23ea
5ff3272
c32841e
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 |
---|---|---|
|
@@ -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(){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this method be private? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. related to https://github.com/mapbox/mapbox-gl-geocoder/pull/198/files#r265396617 but I think it's better to re-gen the docs with each commit and include them. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. Moved this to be a private method. |
||
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'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could also lookup 'en' in localization.placeholder, but I don't think it really matters. |
||
}, | ||
|
||
/** | ||
* 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: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
I think we should add a JSDoc
@returns
here for completeness.