Skip to content

Commit

Permalink
Merge pull request #97 from alibaba/fallbackLocale
Browse files Browse the repository at this point in the history
Fallback locale
  • Loading branch information
cwtuan authored Oct 30, 2018
2 parents 71a56eb + 96c9123 commit a5d97fd
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ require('intl/locale-data/jsonp/ja.js');
* @param {Object} options.locales App locale data like {"en-US":{"key1":"value1"},"zh-CN":{"key1":"值1"}}
* @param {Object} options.commonLocaleDataUrls Custom common locale urls. See https://github.com/alibaba/react-intl-universal/releases/tag/1.12.0
* @param {Object} options.warningHandler Ability to accumulate missing messages using third party services. See https://github.com/alibaba/react-intl-universal/releases/tag/1.11.1
* @param {string} options.fallbackLocale Fallback locale such as 'zh-CN' to use if a key is not found in the current locale
* @returns {Promise}
*/
init(options)
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.14.3",
"version": "1.15.0",
"description": "Internationalize React apps. Not only for React.Component but also for Vanilla JS.",
"keywords": [
"intl",
Expand Down
19 changes: 15 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class ReactIntlUniversal {
warningHandler: console.warn, // ability to accumulate missing messages using third party services like Sentry
escapeHtml: true, // disable escape html in variable mode
commonLocaleDataUrls: COMMON_LOCALE_DATA_URLS,
fallbackLocale: null, // Locale to use if a key is not found in the current locale
};
}

Expand All @@ -69,10 +70,20 @@ class ReactIntlUniversal {
}
let msg = this.getDescendantProp(locales[currentLocale], key);
if (msg == null) {
this.options.warningHandler(
`react-intl-universal key "${key}" not defined in ${currentLocale}`
);
return "";
if (this.options.fallbackLocale) {
msg = this.getDescendantProp(locales[this.options.fallbackLocale], key);
if (msg == null) {
this.options.warningHandler(
`react-intl-universal key "${key}" not defined in ${currentLocale} or the fallback locale, ${this.options.fallbackLocale}`
);
return "";
}
} else {
this.options.warningHandler(
`react-intl-universal key "${key}" not defined in ${currentLocale}`
);
return "";
}
}
if (variables) {
variables = Object.assign({}, variables);
Expand Down
17 changes: 14 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ test("react-intl mirror API formatHTMLMessage:defaultMessage", () => {
intl.init({ locales, currentLocale: "en-US" });
let reactEl = intl.formatHTMLMessage({
id: "not-exist-key",
defaultMessage: React.createElement("div", {className: 'test'})
defaultMessage: React.createElement("div", { className: 'test' })
});

expect(reactEl.type).toBe('div');
Expand Down Expand Up @@ -211,7 +211,7 @@ test("Without default message, just return empty string", () => {

test("Should call handler when message is not defined", () => {
const warningHandler = jest.fn();
intl.init({
intl.init({
locales, currentLocale: "en-US",
warningHandler
});
Expand Down Expand Up @@ -290,7 +290,18 @@ test("load mutiple locale data without overriding existing one", () => {
const localesMore = {
"en-US": enUSMore,
};
intl.load(localesMore);
intl.load(localesMore);
expect(intl.get("SIMPLE")).toBe("Simple");
expect(intl.get("MORE")).toBe("More data");
});

test("Uses fallback locale if key not found in currentLocale", () => {
intl.init({ locales, currentLocale: "zh-CN", fallbackLocale: "en-US" });
expect(intl.get("ONLY_IN_ENGLISH")).toBe("ONLY_IN_ENGLISH");
});

test("Uses default message if key not found in fallbackLocale", () => {
intl.init({ locales, currentLocale: "zh-CN", fallbackLocale: "en-US" });
expect(intl.get("not-exist-key").defaultMessage("this is default msg")).toBe("this is default msg");
});

1 change: 1 addition & 0 deletions test/locales/en-US.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ module.exports = ({
"DOT.HELLO": "Hello World",
"BRACE1": "The format is {var}",
"BRACE2": "The format is ${var}",
"ONLY_IN_ENGLISH": "ONLY_IN_ENGLISH"
});
1 change: 1 addition & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ declare module "react-intl-universal" {
* @param {string} options.currentLocale Current locale such as 'en-US'
* @param {Object} options.locales App locale data like {"en-US":{"key1":"value1"},"zh-CN":{"key1":"值1"}}
* @param {Object} options.warningHandler Ability to accumulate missing messages using third party services like Sentry
* @param {string} options.fallbackLocale Fallback locale such as 'zh-CN' to use if a key is not found in the current locale
* @param {boolean} options.escapeHtml To escape html. Default value is true.
* @returns {Promise}
*/
Expand Down

0 comments on commit a5d97fd

Please sign in to comment.