Skip to content

Commit

Permalink
Merge branch 'feature/translate-countries' of https://github.com/elon…
Browse files Browse the repository at this point in the history
…mir/intl-tel-input into elonmir-translate-countries
  • Loading branch information
jackocnr committed Jun 24, 2018
2 parents f6ecc7d + 0a8010f commit 86ead8e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@ Display the country dial code next to the selected flag so it's not part of the
Type: `String` Default: `""` Example: `"build/js/utils.js"`
Enable formatting/validation etc. by specifying the URL of the included utils.js script (or alternatively just point it to the file on [cdnjs.com](https://cdnjs.com/libraries/intl-tel-input)). The script is fetched using Ajax when the page has finished loading (on the `window.load` event) to prevent blocking (the script is ~215KB). When instantiating the plugin, we return a [deferred object](https://api.jquery.com/category/deferred-object/), so you can use `.done(callback)` to know when initialisation requests like this have finished. See [Utilities Script](#utilities-script) for more information. _Note that if you're lazy loading the plugin script itself (intlTelInput.js) this will not work and you will need to use the `loadUtils` method instead._

**localizedCountries**
Type: `Object` Default: `{}`
Allows to translate the countries by its given iso code e.g.:

```js
{'de':'Deutschland'}
```


## Public Methods
**destroy**
Expand Down
17 changes: 16 additions & 1 deletion build/js/intlTelInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@
// display the country dial code next to the selected flag so it's not part of the typed number
separateDialCode: false,
// specify the path to the libphonenumber script to enable validation/formatting
utilsScript: ""
utilsScript: "",
// default locale eg.: {'de':'Deutschland'}
localizedCountries: null
}, keys = {
UP: 38,
DOWN: 40,
Expand Down Expand Up @@ -130,6 +132,10 @@
this._processCountryCodes();
// process the preferredCountries
this._processPreferredCountries();
// translate countries according to locale object literal
if (this.options.localizedCountries) {
this._translateCountriesByLocale();
}
},
// add a country code to this.countryCodes
_addCountryCode: function(iso2, dialCode, priority) {
Expand Down Expand Up @@ -160,6 +166,15 @@
this.countries = allCountries;
}
},
// Translate Countries by object literal provided on config
_translateCountriesByLocale: function() {
for (var i = 0; i < this.countries.length; i++) {
var iso = this.countries[i].iso2.toLowerCase();
if (iso in this.options.localizedCountries) {
this.countries[i].name = this.options.localizedCountries[iso];
}
}
},
// sort by country name
_countryNameSort: function(a, b) {
return a.name.localeCompare(b.name);
Expand Down
Loading

0 comments on commit 86ead8e

Please sign in to comment.