Launch Week Day 5: Introducing Reachability for PHP.Learn More
Socket
Book a DemoSign in
Socket

react-localization

Package Overview
Dependencies
Maintainers
1
Versions
53
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-localization - npm Package Compare versions

Comparing version
0.0.13
to
0.0.14
+84
spec/LocalizedStrings.spec.js
import LocalizedStrings from './../src/LocalizedStrings';
let strings = new LocalizedStrings({
en:{
how:"How do you want your egg today?",
boiledEgg:"Boiled egg",
softBoiledEgg:"Soft-boiled egg",
choice:"How to choose the egg",
formattedValue:"I'd like some {0} and {1}, or just {0}",
ratings:{
excellent:"excellent",
good:"good",
missing:"missing value"
},
missing:"missing value"
},
it: {
how:"Come vuoi il tuo uovo oggi?",
boiledEgg:"Uovo sodo",
softBoiledEgg:"Uovo alla coque",
choice:"Come scegliere l'uovo",
ratings:{
excellent:"eccellente",
good:"buono"
},
formattedValue:"Vorrei un po' di {0} e {1}, o solo {0}",
}
});
describe('Main Library Functions', function () {
it("Set default language to en", function(){
expect(strings.language).toEqual("en");
});
it("List available languages", function(){
expect(strings.getAvailableLanguages()).toEqual(["en", "it"]);
});
//Default language
it('Extract simple value from default language', function () {
expect(strings.how).toEqual('How do you want your egg today?');
});
it('Extract complex value from default language', function () {
expect(strings.ratings.good).toEqual('good');
});
it('Get complex missing key from default language', function () {
expect(strings.ratings.missing).toEqual('missing value');
});
it('Get missing key from default language', function () {
expect(strings.ratings.notfound).toBe(undefined);
});
it('Format string in default languate', function () {
expect(strings.formatString(strings.formattedValue, "cake", "ice-cream")).toBe("I'd like some cake and ice-cream, or just cake");
});
//Switch language
it("Switch language to italian", function(){
strings.setLanguage("it");
expect(strings.language).toEqual("it");
});
it('Extract simple value from other language', function () {
expect(strings.how).toEqual('Come vuoi il tuo uovo oggi?');
});
it('Extract complex value from other language', function () {
expect(strings.ratings.good).toEqual('buono');
});
it('Get missing key from other language', function () {
expect(strings.missing).toEqual('missing value');
});
it('Get complex missing key from other language', function () {
expect(strings.ratings.missing).toEqual('missing value');
});
it('Format string in other language', function () {
expect(strings.formatString(strings.formattedValue, "torta", "gelato")).toBe("Vorrei un po' di torta e gelato, o solo torta");
});
it('Get string in a different language', function () {
expect(strings.getString("choice", "en")).toBe("How to choose the egg");
});
});
{
"spec_dir": "spec",
"spec_files": [
"**/*[sS]pec.js"
],
"helpers": [
"helpers/**/*.js"
],
"stopSpecOnExpectationFailure": false,
"random": false
}
+6
-3
{
"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)