react-localization
Advanced tools
Comparing version 0.0.13 to 0.0.14
{ | ||
"name": "react-localization", | ||
"version": "0.0.13", | ||
"version": "0.0.14", | ||
"description": "Simple module to localize the React interface using the same syntax used in the ReactNativeLocalization module, use 'npm run build' before publishing", | ||
"scripts": { | ||
"babel-version": "babel --version", | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"test": "jasmine", | ||
"build": "babel ./src/LocalizedStrings.js --presets babel-preset-es2015,babel-preset-stage-2 --out-file ./lib/LocalizedStrings.js" | ||
@@ -31,5 +31,8 @@ }, | ||
"babel-preset-es2015": "^6.22.0", | ||
"babel-preset-stage-2": "^6.22.0" | ||
"babel-preset-stage-2": "^6.22.0", | ||
"jasmine": "^2.6.0", | ||
"jasmine-core": "^2.6.2", | ||
"jasmine-es6": "^0.4.0" | ||
}, | ||
"dependencies": {} | ||
} |
@@ -57,23 +57,41 @@ 'use strict'; | ||
//Can be used from ouside the class to force a particular language | ||
//independently from the interface one | ||
//indipendently from the interface one | ||
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); | ||
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 = {...this.props[this.defaultLanguage], ...this.props[this.language] }; | ||
var localizedStrings = Object.assign({}, this.props[defaultLanguage], 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 | ||
_fallbackValues(defaultStrings, strings) { | ||
for (var key in defaultStrings) { | ||
if (defaultStrings.hasOwnProperty(key) && !strings[key]) { | ||
strings[key] = defaultStrings[key]; | ||
console.log("Missing localization for language '" + this.language + "' and key '" + key + "'."); | ||
} else { | ||
if (typeof strings[key] != "string") { | ||
//Si tratta di un oggetto | ||
this._fallbackValues(defaultStrings[key], strings[key]); | ||
} | ||
} | ||
} | ||
} | ||
//The current language displayed (could differ from the interface language | ||
@@ -80,0 +98,0 @@ // if it has been forced manually and a matching translation has been found) |
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
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
20903
9
363
1
6