Socket
Socket
Sign inDemoInstall

react-localization

Package Overview
Dependencies
Maintainers
1
Versions
47
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

spec/LocalizedStrings.spec.js

9

package.json
{
"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)

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc