react-localization
Advanced tools
Comparing version 0.0.15 to 0.0.16
@@ -9,3 +9,3 @@ declare module "react-localization" { | ||
getString(key: string, language: string): string; | ||
setProps(props: any): void; | ||
setContent(props: any): void; | ||
} | ||
@@ -12,0 +12,0 @@ |
@@ -22,4 +22,2 @@ 'use strict'; | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
@@ -29,4 +27,21 @@ | ||
var DEFAULT_VALUE = 'en-US'; | ||
var LocalizedStrings = function () { | ||
_createClass(LocalizedStrings, [{ | ||
key: '_getInterfaceLanguage', | ||
value: function _getInterfaceLanguage() { | ||
var lang = null; | ||
if (typeof navigator !== 'undefined' && navigator.languages && typeof navigator.languages !== 'undefined' && navigator.languages[0] && typeof navigator.languages[0] !== 'undefined') { | ||
lang = navigator.languages[0]; | ||
} else if (typeof navigator !== 'undefined' && navigator.language && typeof navigator.language !== 'undefined') { | ||
lang = navigator.language; | ||
} else if (typeof navigator !== 'undefined' && navigator.userLanguage && typeof navigator.userLanguage !== 'undefined') { | ||
lang = navigator.userLanguage; | ||
} else if (typeof navigator !== 'undefined' && navigator.browserLanguage && typeof navigator.browserLanguage !== 'undefined') { | ||
lang = navigator.browserLanguage; | ||
} | ||
return lang || DEFAULT_VALUE; | ||
} | ||
}, { | ||
key: '_getBestMatchingLanguage', | ||
@@ -36,10 +51,7 @@ value: function _getBestMatchingLanguage(language, props) { | ||
if (props[language]) return language; | ||
//if the string is composed try to find a match with only the first language identifiers (en-US --> en) | ||
var idx = language.indexOf("-"); | ||
if (idx >= 0) { | ||
language = language.substring(0, idx); | ||
if (props[language]) return language; | ||
} | ||
//Return the default language (the first coded) | ||
return Object.keys(props)[0]; | ||
var idx = language.indexOf('-'); | ||
var auxLang = idx >= 0 ? language.substring(0, idx) : language; | ||
return props[auxLang] ? auxLang : Object.keys(props)[0]; | ||
} | ||
@@ -51,36 +63,107 @@ }]); | ||
this.interfaceLanguage = typeof navigator !== 'undefined' && navigator.languages && typeof navigator.languages !== 'undefined' && navigator.languages[0] && typeof navigator.languages[0] !== 'undefined' ? navigator.languages[0] : typeof navigator !== 'undefined' && navigator.language && typeof navigator.language !== 'undefined' ? navigator.language : typeof navigator !== 'undefined' && navigator.userLanguage && typeof navigator.userLanguage !== 'undefined' ? navigator.userLanguage : 'en-US'; | ||
//Store locally the passed strings | ||
this.props = props; | ||
this.defaultLanguage = Object.keys(props)[0]; | ||
//Set language to its default value (the interface) | ||
this.setLanguage(this.interfaceLanguage); | ||
//Add property before checking for use of reserved words... | ||
this._interfaceLanguage = this._getInterfaceLanguage(); | ||
this._language = this._interfaceLanguage; | ||
this.setContent(props); | ||
} | ||
//Can be used from ouside the class to force a particular language | ||
//independently from the interface one | ||
_createClass(LocalizedStrings, [{ | ||
key: 'setContent', | ||
value: function setContent(props) { | ||
this._defaultLanguage = Object.keys(props)[0]; | ||
this._defaultLanguageFirstLevelKeys = []; | ||
//Store locally the passed strings | ||
this._props = props; | ||
//Check for use of reserved words | ||
this._validateProps(props[this._defaultLanguage]); | ||
//Store first level keys (for identifying missing translations) | ||
for (var key in this._props[this._defaultLanguage]) { | ||
if (typeof this._props[this._defaultLanguage][key] == "string") { | ||
this._defaultLanguageFirstLevelKeys.push(key); | ||
} | ||
} | ||
//Set language to its default value (the interface) | ||
this.setLanguage(this._interfaceLanguage); | ||
} | ||
}, { | ||
key: '_validateProps', | ||
value: function _validateProps(props) { | ||
var _this = this; | ||
Object.keys(props).map(function (key) { | ||
if (_this.hasOwnProperty(key)) throw new Error(key + ' cannot be used as a key. It is a reserved word.'); | ||
}); | ||
} | ||
_createClass(LocalizedStrings, [{ | ||
//Can be used from ouside the class to force a particular language | ||
//indipendently from the interface one | ||
}, { | ||
key: 'setLanguage', | ||
value: function setLanguage(language) { | ||
//Check if a translation exists for the current language or if the default | ||
//Check if exists a translation for the current language or if the default | ||
//should be used | ||
var bestLanguage = this._getBestMatchingLanguage(language, this.props); | ||
this.language = bestLanguage; | ||
var bestLanguage = this._getBestMatchingLanguage(language, this._props); | ||
var defaultLanguage = Object.keys(this._props)[0]; | ||
this._language = bestLanguage; | ||
//Associate the language object to the this object | ||
if (this.props[bestLanguage]) { | ||
//console.log("There are strings for the language:"+language); | ||
//Merge default | ||
var localizedStrings = _extends({}, this.props[this.defaultLanguage], this.props[this.language]); | ||
if (this._props[bestLanguage]) { | ||
//delete default propery values to identify missing translations | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
try { | ||
for (var _iterator = this._defaultLanguageFirstLevelKeys[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
key = _step.value; | ||
delete this[key]; | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
var localizedStrings = Object.assign({}, this._props[this._language]); | ||
for (var key in localizedStrings) { | ||
//console.log("Checking property:"+key); | ||
if (localizedStrings.hasOwnProperty(key)) { | ||
//console.log("Associating property:"+key); | ||
this[key] = localizedStrings[key]; | ||
} | ||
} | ||
//Now add any string missing from the translation but existing in the default language | ||
if (defaultLanguage !== this._language) { | ||
localizedStrings = this._props[defaultLanguage]; | ||
this._fallbackValues(localizedStrings, this); | ||
} | ||
} | ||
} | ||
//Load fallback values for missing translations | ||
}, { | ||
key: '_fallbackValues', | ||
value: function _fallbackValues(defaultStrings, strings) { | ||
for (var key in defaultStrings) { | ||
if (defaultStrings.hasOwnProperty(key) && !strings[key]) { | ||
strings[key] = defaultStrings[key]; | ||
console.log('\uD83D\uDEA7 \uD83D\uDC77 key \'' + key + '\' not found in localizedStrings for language ' + this._language + ' \uD83D\uDEA7'); | ||
} else { | ||
if (typeof strings[key] != "string") { | ||
//It's an object | ||
this._fallbackValues(defaultStrings[key], strings[key]); | ||
} | ||
} | ||
} | ||
} | ||
//The current language displayed (could differ from the interface language | ||
@@ -92,3 +175,3 @@ // if it has been forced manually and a matching translation has been found) | ||
value: function getLanguage() { | ||
return this.language; | ||
return this._language; | ||
} | ||
@@ -101,3 +184,3 @@ | ||
value: function getInterfaceLanguage() { | ||
return this.interfaceLanguage; | ||
return this._interfaceLanguage; | ||
} | ||
@@ -110,9 +193,9 @@ | ||
value: function getAvailableLanguages() { | ||
if (!this.availableLanguages) { | ||
this.availableLanguages = []; | ||
for (var language in this.props) { | ||
this.availableLanguages.push(language); | ||
if (!this._availableLanguages) { | ||
this._availableLanguages = []; | ||
for (var language in this._props) { | ||
this._availableLanguages.push(language); | ||
} | ||
} | ||
return this.availableLanguages; | ||
return this._availableLanguages; | ||
} | ||
@@ -146,3 +229,3 @@ | ||
try { | ||
return this.props[language][key]; | ||
return this._props[language][key]; | ||
} catch (ex) { | ||
@@ -154,3 +237,3 @@ console.log("No localization found for key " + key + " and language " + language); | ||
//Replace all occorrencies of a string in another using RegExp | ||
//Replace all occurrences of a string in another using RegExp | ||
@@ -157,0 +240,0 @@ }, { |
{ | ||
"name": "react-localization", | ||
"version": "0.0.15", | ||
"version": "0.0.16", | ||
"description": "Simple module to localize the React interface using the same syntax used in the ReactNativeLocalization module, use 'npm run build' before publishing", | ||
@@ -29,3 +29,3 @@ "scripts": { | ||
"devDependencies": { | ||
"babel-cli": "^6.22.2", | ||
"babel-cli": "^6.24.1", | ||
"babel-preset-es2015": "^6.22.0", | ||
@@ -32,0 +32,0 @@ "babel-preset-stage-2": "^6.22.0", |
@@ -52,3 +52,3 @@ # react-localization | ||
* formatString() - to format the passed string replacing its placeholders with the other arguments strings | ||
* setProps(props) - to dynamically load another set of strings | ||
* setContent(props) - to dynamically load another set of strings | ||
```js | ||
@@ -55,0 +55,0 @@ en:{ |
@@ -100,3 +100,3 @@ import LocalizedStrings from './../src/LocalizedStrings'; | ||
it('Switch to different props', function () { | ||
strings.setProps( | ||
strings.setContent( | ||
secondarySet | ||
@@ -103,0 +103,0 @@ ) |
@@ -51,6 +51,6 @@ 'use strict'; | ||
this._language = this._interfaceLanguage; | ||
this.setProps(props); | ||
this.setContent(props); | ||
} | ||
setProps(props){ | ||
setContent(props){ | ||
this._defaultLanguage = Object.keys(props)[0]; | ||
@@ -57,0 +57,0 @@ this._defaultLanguageFirstLevelKeys = []; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25649
481