react-localization
Advanced tools
Comparing version 0.0.14 to 0.0.15
@@ -8,3 +8,4 @@ declare module "react-localization" { | ||
getAvailableLanguages(): string[]; | ||
getString(key:string, language:string):string; | ||
getString(key: string, language: string): string; | ||
setProps(props: any): void; | ||
} | ||
@@ -11,0 +12,0 @@ |
{ | ||
"name": "react-localization", | ||
"version": "0.0.14", | ||
"version": "0.0.15", | ||
"description": "Simple module to localize the React interface using the same syntax used in the ReactNativeLocalization module, use 'npm run build' before publishing", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -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 | ||
```js | ||
@@ -55,0 +55,0 @@ en:{ |
import LocalizedStrings from './../src/LocalizedStrings'; | ||
let strings = new LocalizedStrings({ | ||
en:{ | ||
language:"english", | ||
how:"How do you want your egg today?", | ||
@@ -12,3 +13,3 @@ boiledEgg:"Boiled egg", | ||
good:"good", | ||
missing:"missing value" | ||
missingComplex:"missing value" | ||
}, | ||
@@ -30,6 +31,20 @@ missing:"missing value" | ||
const secondarySet = { | ||
fr:{ | ||
"hello":"bonjour" | ||
}, | ||
en:{ | ||
"hello":"hello" | ||
}, | ||
it:{ | ||
"hello":"ciao" | ||
} | ||
} | ||
describe('Main Library Functions', function () { | ||
it("Set default language to en", function(){ | ||
expect(strings.language).toEqual("en"); | ||
expect(strings.getLanguage()).toEqual("en"); | ||
}); | ||
@@ -49,3 +64,3 @@ | ||
it('Get complex missing key from default language', function () { | ||
expect(strings.ratings.missing).toEqual('missing value'); | ||
expect(strings.ratings.missingComplex).toEqual('missing value'); | ||
}); | ||
@@ -62,3 +77,3 @@ it('Get missing key from default language', function () { | ||
strings.setLanguage("it"); | ||
expect(strings.language).toEqual("it"); | ||
expect(strings.getLanguage()).toEqual("it"); | ||
}); | ||
@@ -78,3 +93,3 @@ it('Extract simple value from other language', function () { | ||
it('Get complex missing key from other language', function () { | ||
expect(strings.ratings.missing).toEqual('missing value'); | ||
expect(strings.ratings.missingComplex).toEqual('missing value'); | ||
}); | ||
@@ -89,2 +104,10 @@ it('Format string in other language', function () { | ||
it('Switch to different props', function () { | ||
strings.setProps( | ||
secondarySet | ||
) | ||
strings.setLanguage("fr"); | ||
expect(strings.hello).toEqual('bonjour'); | ||
}); | ||
}); |
@@ -48,10 +48,32 @@ 'use strict'; | ||
constructor(props) { | ||
this.interfaceLanguage = this._getInterfaceLanguage(); | ||
//Add property before checking for use of reserved words... | ||
this._interfaceLanguage = this._getInterfaceLanguage(); | ||
this._language = this._interfaceLanguage; | ||
this.setProps(props); | ||
} | ||
setProps(props){ | ||
this._defaultLanguage = Object.keys(props)[0]; | ||
this._defaultLanguageFirstLevelKeys = []; | ||
//Store locally the passed strings | ||
this.props = props; | ||
this.defaultLanguage = Object.keys(props)[0]; | ||
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); | ||
this.setLanguage(this._interfaceLanguage); | ||
} | ||
_validateProps(props) { | ||
Object.keys(props).map(key => { | ||
if (this.hasOwnProperty(key)) throw new Error(`${key} cannot be used as a key. It is a reserved word.`) | ||
}) | ||
} | ||
//Can be used from ouside the class to force a particular language | ||
@@ -62,8 +84,12 @@ //indipendently from the interface one | ||
//should be used | ||
var bestLanguage = this._getBestMatchingLanguage(language, this.props); | ||
var defaultLanguage = Object.keys(this.props)[0]; | ||
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]) { | ||
var localizedStrings = Object.assign({}, this.props[defaultLanguage], this.props[this.language]); | ||
if (this._props[bestLanguage]) { | ||
//delete default propery values to identify missing translations | ||
for (key of this._defaultLanguageFirstLevelKeys){ | ||
delete this[key]; | ||
} | ||
var localizedStrings = Object.assign({}, this._props[this._language]); | ||
for (var key in localizedStrings) { | ||
@@ -75,4 +101,4 @@ if (localizedStrings.hasOwnProperty(key)) { | ||
//Now add any string missing from the translation but existing in the default language | ||
if (defaultLanguage !== this.language) { | ||
localizedStrings = this.props[defaultLanguage]; | ||
if (defaultLanguage !== this._language) { | ||
localizedStrings = this._props[defaultLanguage]; | ||
this._fallbackValues(localizedStrings, this); | ||
@@ -88,6 +114,6 @@ } | ||
strings[key] = defaultStrings[key]; | ||
console.log("Missing localization for language '" + this.language + "' and key '" + key + "'."); | ||
console.log(`🚧 👷 key '${key}' not found in localizedStrings for language ${this._language} 🚧`); | ||
} else { | ||
if (typeof strings[key] != "string") { | ||
//Si tratta di un oggetto | ||
//It's an object | ||
this._fallbackValues(defaultStrings[key], strings[key]); | ||
@@ -103,3 +129,3 @@ } | ||
getLanguage() { | ||
return this.language; | ||
return this._language; | ||
} | ||
@@ -109,3 +135,3 @@ | ||
getInterfaceLanguage() { | ||
return this.interfaceLanguage; | ||
return this._interfaceLanguage; | ||
} | ||
@@ -115,9 +141,9 @@ | ||
getAvailableLanguages() { | ||
if (!this.availableLanguages) { | ||
this.availableLanguages = []; | ||
for (let language in this.props) { | ||
this.availableLanguages.push(language); | ||
if (!this._availableLanguages) { | ||
this._availableLanguages = []; | ||
for (let language in this._props) { | ||
this._availableLanguages.push(language); | ||
} | ||
} | ||
return this.availableLanguages; | ||
return this._availableLanguages; | ||
} | ||
@@ -140,3 +166,3 @@ | ||
try { | ||
return this.props[language][key]; | ||
return this._props[language][key]; | ||
} catch (ex) { | ||
@@ -143,0 +169,0 @@ console.log("No localization found for key " + key + " and language " + language); |
22381
406