Skip to content

Commit

Permalink
Merge pull request #29 from alibaba/nest
Browse files Browse the repository at this point in the history
Nested json
  • Loading branch information
cwtuan authored Sep 25, 2017
2 parents 8cfc08a + 0f8f50d commit f47a16d
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion examples/browser-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"react-scripts": "0.8.4"
},
"dependencies": {
"react-intl-universal": "^1.3.4",
"react-intl-universal": "^1.4.3",
"axios": "^0.16.1",
"lodash": "^4.17.4",
"react": "^15.5.4",
Expand Down
2 changes: 1 addition & 1 deletion examples/node-js-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"react-scripts": "^0.9.0"
},
"dependencies": {
"react-intl-universal": "^1.3.4",
"react-intl-universal": "^1.4.3",
"react": "^15.5.4",
"react-dom": "^15.5.4"
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-intl-universal",
"version": "1.3.4",
"version": "1.4.3",
"description": "Internationalize React apps. Not only for React.Component but also for Vanilla JS.",
"keywords": [
"intl",
Expand Down
10 changes: 9 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class ReactIntlUniversal {
if (!locales || !locales[currentLocale]) {
return "";
}
let msg = locales[currentLocale][key];
let msg = this.getDescendantProp(locales[currentLocale], key);
if (msg == null) {
console.warn(
`react-intl-universal key "${key}" not defined in ${currentLocale}`
Expand Down Expand Up @@ -221,6 +221,14 @@ class ReactIntlUniversal {
}
}

getDescendantProp(locale, key) {
const msg = key.split(".").reduce(function(a, b) {
return (a != undefined) ? a[b] : a ;
}, locale);

return msg;
}

getLocaleFromBrowser() {
return navigator.language || navigator.userLanguage;
}
Expand Down
11 changes: 11 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ test("Message with variables", () => {
expect(intl.get("HELLO", { name: "Tony" })).toBe("Hello, Tony");
});

test("Set specific locale with nested notation", () => {
intl.init({ locales, currentLocale: "en-US" });
expect(intl.get("NESTED.HELLO")).toBe("Hello World");
expect(intl.get("NESTED.HELLO_NAME", { name: "World" })).toBe("Hello, World");
});

test("react-intl mirror API formatMessage:variables", () => {
intl.init({ locales, currentLocale: "en-US" });
const name = "Tony";
Expand Down Expand Up @@ -196,6 +202,11 @@ test("Default message", () => {
);
});

test("Default message with nested key", () => {
intl.init({ locales, currentLocale: "en-US" });
expect(intl.get("NOT_EXIST_KEY.HELLO").defaultMessage("Hello World")).toBe("Hello World");
});

test("Default message", () => {
intl.init({ locales, currentLocale: "en-US" });
expect(intl.get("not-exist-key").defaultMessage("this is default msg")).toBe(
Expand Down
6 changes: 5 additions & 1 deletion test/locales/en-US.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@ module.exports = ({
"SALE_END": "Sale begins {start, date, long}",
"COUPON": "Coupon expires at {expires, time, medium}",
"SALE_PRICE": "The price is {price, number, USD}",
"PHOTO": "You have {num, plural, =0 {no photos.} =1 {one photo.} other {# photos.}}"
"PHOTO": "You have {num, plural, =0 {no photos.} =1 {one photo.} other {# photos.}}",
"NESTED": {
"HELLO": "Hello World",
"HELLO_NAME": "Hello, {name}"
}
});

0 comments on commit f47a16d

Please sign in to comment.