react-localization
Advanced tools
Comparing version 0.1.5 to 0.1.6
declare module 'react-localization' { | ||
type Formatted = number | string | JSX.Element; | ||
type FormatObject<U extends Formatted> = { [key: string]: U }; | ||
export interface GlobalStrings<T> { | ||
[language: string]: T; | ||
} | ||
@@ -10,3 +14,3 @@ interface LocalizedStringsMethods { | ||
getInterfaceLanguage(): string; | ||
formatString(str: string, ...values: any[]): string; | ||
formatString<T extends Formatted>(str: string, ...values: Array<T | FormatObject<T>>): Array<string | T>; | ||
getAvailableLanguages(): string[]; | ||
@@ -23,2 +27,2 @@ getString(key: string, language: string): string; | ||
export default LocalizedStrings; | ||
} | ||
} |
@@ -211,4 +211,11 @@ 'use strict'; | ||
if (!valueForPlaceholder) { | ||
valueForPlaceholder = valuesForPlaceholders[0][matchedKey]; | ||
// If no value found, check if working with an object instead | ||
if (valueForPlaceholder == undefined) { | ||
var valueFromObjectPlaceholder = valuesForPlaceholders[0][matchedKey]; | ||
if (valueFromObjectPlaceholder !== undefined) { | ||
valueForPlaceholder = valueFromObjectPlaceholder; | ||
} else { | ||
// If value still isn't found, then it must have been undefined/null | ||
return valueForPlaceholder; | ||
} | ||
} | ||
@@ -215,0 +222,0 @@ |
{ | ||
"name": "react-localization", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"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": { |
@@ -7,28 +7,37 @@ import React from 'react'; | ||
global.navigator = {}; | ||
let strings = new LocalizedStrings({ | ||
en: { | ||
language:"english", | ||
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", | ||
missingComplex:"missing value" | ||
let strings; | ||
beforeEach(() => { | ||
strings = new LocalizedStrings({ | ||
en: { | ||
language:"english", | ||
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", | ||
missingComplex:"missing value" | ||
}, | ||
missing:"missing value", | ||
currentDate: "The current date is {month} {day}, {year}!", | ||
falsy: "{0} {1} {2} {3} {4} {5}" | ||
}, | ||
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}", | ||
} | ||
it: { | ||
language: "italian", | ||
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}", | ||
currentDate: "La data corrente รจ {month} {day}, {year}!", | ||
falsy: "{0} {1} {2} {3} {4} {5}" | ||
} | ||
}); | ||
}); | ||
@@ -68,2 +77,3 @@ | ||
it('Extract simple value from other language', function () { | ||
strings.setLanguage("it"); | ||
expect(strings.how).toEqual('Come vuoi il tuo uovo oggi?'); | ||
@@ -73,2 +83,3 @@ }); | ||
it('Extract complex value from other language', function () { | ||
strings.setLanguage("it"); | ||
expect(strings.ratings.good).toEqual('buono'); | ||
@@ -78,2 +89,3 @@ }); | ||
it('Get missing key from other language', function () { | ||
strings.setLanguage("it"); | ||
expect(strings.missing).toEqual('missing value'); | ||
@@ -83,5 +95,8 @@ }); | ||
it('Get complex missing key from other language', function () { | ||
strings.setLanguage("it"); | ||
expect(strings.ratings.missingComplex).toEqual('missing value'); | ||
}); | ||
it('Format string in other language', function () { | ||
strings.setLanguage("it"); | ||
expect(strings.formatString(strings.formattedValue, "torta", "gelato")) | ||
@@ -92,2 +107,3 @@ .toEqual(["Vorrei un po' di ", "torta", " e ", "gelato", ", o solo ", "torta"]); | ||
it('Get string in a different language', function () { | ||
strings.setLanguage("it"); | ||
expect(strings.getString("choice", "en")).toBe("How to choose the egg"); | ||
@@ -133,2 +149,17 @@ }); | ||
it('Handles named tokens as part of the format string', () => { | ||
const formatTokens = { | ||
month: "January", | ||
day: "12", | ||
year: "2018" | ||
}; | ||
expect(strings.formatString(strings.currentDate, formatTokens)) | ||
.toEqual(["The current date is ", "January", " ", "12", ", ", "2018", "!"]); | ||
}); | ||
it('Handles falsy values', () => { | ||
expect(strings.formatString(strings.falsy, 0, false, '', null, undefined, NaN)) | ||
.toEqual([0, " ", false, " ", '', " ", null, " ", undefined, " ", NaN]); | ||
}); | ||
describe('formatString with React components', () => { | ||
@@ -140,3 +171,2 @@ const reactStrings = new LocalizedStrings({ | ||
helloThere: "Hello {0}! Are you sure {0} is your name?", | ||
currentDate: "The current date is {month} {day}, {year}!", | ||
boldText: "Some {bold} text" | ||
@@ -166,12 +196,2 @@ }, | ||
it('Handles named tokens as part of the format string', () => { | ||
const formatTokens = { | ||
month: "January", | ||
day: "12", | ||
year: "2018" | ||
}; | ||
expect(reactStrings.formatString(reactStrings.currentDate, formatTokens)) | ||
.toEqual(["The current date is ", "January", " ", "12", ", ", "2018", "!"]); | ||
}); | ||
it('Handles named tokens with components', () => { | ||
@@ -178,0 +198,0 @@ expect(reactStrings.formatString(reactStrings.boldText, { |
@@ -139,4 +139,11 @@ 'use strict'; | ||
if(!valueForPlaceholder) { | ||
valueForPlaceholder = valuesForPlaceholders[0][matchedKey]; | ||
// If no value found, check if working with an object instead | ||
if(valueForPlaceholder == undefined) { | ||
const valueFromObjectPlaceholder = valuesForPlaceholders[0][matchedKey]; | ||
if(valueFromObjectPlaceholder !== undefined) { | ||
valueForPlaceholder = valueFromObjectPlaceholder; | ||
} else { | ||
// If value still isn't found, then it must have been undefined/null | ||
return valueForPlaceholder; | ||
} | ||
} | ||
@@ -143,0 +150,0 @@ |
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
34333
658