From 9fed1290eca30eca777e6da190b275b07052375d Mon Sep 17 00:00:00 2001 From: Elga Date: Fri, 22 Sep 2017 02:37:03 +0700 Subject: [PATCH 1/4] Support nested JSON --- src/index.js | 10 +++++++++- test/index.js | 6 ++++++ test/locales/en-US.js | 6 +++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 72f5765..8f03f90 100644 --- a/src/index.js +++ b/src/index.js @@ -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}` @@ -221,6 +221,14 @@ class ReactIntlUniversal { } } + getDescendantProp(locale, key) { + const msg = key.split('.').reduce(function(a, b) { + return a[b]; + }, locale); + + return msg; + } + getLocaleFromBrowser() { return navigator.language || navigator.userLanguage; } diff --git a/test/index.js b/test/index.js index 43e6288..50fada5 100644 --- a/test/index.js +++ b/test/index.js @@ -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_TONY", { name: "Meta" })).toBe("Hello, Meta"); +}); + test("react-intl mirror API formatMessage:variables", () => { intl.init({ locales, currentLocale: "en-US" }); const name = "Tony"; diff --git a/test/locales/en-US.js b/test/locales/en-US.js index dc31f0a..315a98c 100644 --- a/test/locales/en-US.js +++ b/test/locales/en-US.js @@ -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_TONY": "Hello, {name}" + } }); From 027a9e19b949674aa487ce0f2eebbb939d53e1bc Mon Sep 17 00:00:00 2001 From: Elga Date: Fri, 22 Sep 2017 02:41:57 +0700 Subject: [PATCH 2/4] update wording text --- test/index.js | 2 +- test/locales/en-US.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/index.js b/test/index.js index 50fada5..5cdab78 100644 --- a/test/index.js +++ b/test/index.js @@ -24,7 +24,7 @@ test("Message with variables", () => { 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_TONY", { name: "Meta" })).toBe("Hello, Meta"); + expect(intl.get("NESTED.HELLO_NAME", { name: "Meta" })).toBe("Hello, Meta"); }); test("react-intl mirror API formatMessage:variables", () => { diff --git a/test/locales/en-US.js b/test/locales/en-US.js index 315a98c..377c7fc 100644 --- a/test/locales/en-US.js +++ b/test/locales/en-US.js @@ -10,6 +10,6 @@ module.exports = ({ "PHOTO": "You have {num, plural, =0 {no photos.} =1 {one photo.} other {# photos.}}", "NESTED": { "HELLO": "Hello World", - "HELLO_TONY": "Hello, {name}" + "HELLO_NAME": "Hello, {name}" } }); From a89b1d56e5ea92109c19fd276d1dd08ed69164e5 Mon Sep 17 00:00:00 2001 From: Elga Date: Mon, 25 Sep 2017 10:54:43 +0700 Subject: [PATCH 3/4] handle error if key not exists of nested json --- src/index.js | 4 ++-- test/index.js | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 8f03f90..63886c6 100644 --- a/src/index.js +++ b/src/index.js @@ -222,8 +222,8 @@ class ReactIntlUniversal { } getDescendantProp(locale, key) { - const msg = key.split('.').reduce(function(a, b) { - return a[b]; + const msg = key.split(".").reduce(function(a, b) { + return (a != undefined) ? a[b] : a ; }, locale); return msg; diff --git a/test/index.js b/test/index.js index 5cdab78..a788d2b 100644 --- a/test/index.js +++ b/test/index.js @@ -24,7 +24,7 @@ test("Message with variables", () => { 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: "Meta" })).toBe("Hello, Meta"); + expect(intl.get("NESTED.HELLO_NAME", { name: "World" })).toBe("Hello, World"); }); test("react-intl mirror API formatMessage:variables", () => { @@ -202,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( From 0f8f50dc81ff3914a6643601e550014d923544b3 Mon Sep 17 00:00:00 2001 From: "chiawei.tcw" Date: Mon, 25 Sep 2017 12:59:29 +0800 Subject: [PATCH 4/4] bump version --- examples/browser-example/package.json | 2 +- examples/node-js-example/package.json | 2 +- package.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/browser-example/package.json b/examples/browser-example/package.json index 70bb85b..75a507d 100644 --- a/examples/browser-example/package.json +++ b/examples/browser-example/package.json @@ -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", diff --git a/examples/node-js-example/package.json b/examples/node-js-example/package.json index 3ff953a..12a9cc6 100644 --- a/examples/node-js-example/package.json +++ b/examples/node-js-example/package.json @@ -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" }, diff --git a/package.json b/package.json index 54d3762..aab22dd 100644 --- a/package.json +++ b/package.json @@ -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",