From cd7b73e44adeba3c868b762013a22f5587651c04 Mon Sep 17 00:00:00 2001 From: fanmd Date: Mon, 27 Jun 2022 16:06:37 +0800 Subject: [PATCH 1/2] add defaultVariables options to initialize properties --- packages/react-intl-universal/src/ReactIntlUniversal.js | 3 ++- packages/react-intl-universal/typings/index.d.ts | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/react-intl-universal/src/ReactIntlUniversal.js b/packages/react-intl-universal/src/ReactIntlUniversal.js index 8fa9e2d..40e52d0 100644 --- a/packages/react-intl-universal/src/ReactIntlUniversal.js +++ b/packages/react-intl-universal/src/ReactIntlUniversal.js @@ -22,6 +22,7 @@ class ReactIntlUniversal { 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 + defaultVariables: {} // Default variables in message }; } @@ -31,7 +32,7 @@ class ReactIntlUniversal { * @param {Object} variables Variables in message * @returns {string} message */ - get(key, variables) { + get(key, variables = this.options.defaultVariables) { if (this.options.intlGetHook) { try { this.options.intlGetHook(key, this.options.currentLocale); diff --git a/packages/react-intl-universal/typings/index.d.ts b/packages/react-intl-universal/typings/index.d.ts index 1885a2f..82008d6 100644 --- a/packages/react-intl-universal/typings/index.d.ts +++ b/packages/react-intl-universal/typings/index.d.ts @@ -93,6 +93,7 @@ declare module "react-intl-universal" { localStorageLocaleKey?: string; warningHandler?: (message?: any, error?: any) => void; escapeHtml?: boolean; + defaultVariables?: { [key: string]: any } } export interface ReactIntlUniversalMessageDescriptor { From 92cd141d2d2aaf2c102564530478b4ee19860948 Mon Sep 17 00:00:00 2001 From: fanmd Date: Mon, 8 Aug 2022 15:26:20 +0800 Subject: [PATCH 2/2] add some test cases --- packages/react-intl-universal/test/index.test.js | 15 +++++++++++++++ .../react-intl-universal/test/locales/en-US.js | 2 ++ .../react-intl-universal/test/locales/zh-CN.js | 4 +++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/packages/react-intl-universal/test/index.test.js b/packages/react-intl-universal/test/index.test.js index d6209c1..aac8970 100644 --- a/packages/react-intl-universal/test/index.test.js +++ b/packages/react-intl-universal/test/index.test.js @@ -368,3 +368,18 @@ test("Resolve directly if the environment is not browser", async () => { }); expect(result).toBe(undefined); }); + +test("Set default variables locale", () => { + intl.init({ locales, currentLocale: "zh-CN", + defaultVariables:{ + company:"Jivation-Group" + } + }); + expect(intl.get("COMPANY")).toBe("Jivation-Group是一个国际集团公司"); + expect(intl.get("BUSINESS")).toBe("Jivation-Group处理国际性事务"); + intl.init({ locales, currentLocale: "en-US", defaultVariables:{ + company:"Jivation-Group" + } }); + expect(intl.get("COMPANY")).toBe("Jivation-Group is a international group of companies"); + expect(intl.get("BUSINESS")).toBe("Jivation-Group handle international business"); +}); \ No newline at end of file diff --git a/packages/react-intl-universal/test/locales/en-US.js b/packages/react-intl-universal/test/locales/en-US.js index 96b32f3..ec9174a 100644 --- a/packages/react-intl-universal/test/locales/en-US.js +++ b/packages/react-intl-universal/test/locales/en-US.js @@ -8,6 +8,8 @@ module.exports = ({ "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.}}", + "COMPANY":"{company} is a international group of companies", + "BUSINESS":"{company} handle international business", "NESTED": { "HELLO": "Hello World", "HELLO_NAME": "Hello, {name}" diff --git a/packages/react-intl-universal/test/locales/zh-CN.js b/packages/react-intl-universal/test/locales/zh-CN.js index 76cc519..c6d65b2 100644 --- a/packages/react-intl-universal/test/locales/zh-CN.js +++ b/packages/react-intl-universal/test/locales/zh-CN.js @@ -8,5 +8,7 @@ module.exports = ({ "COUPON": "优惠卷将在{expires, time, medium}过期", "TIME": "时间是{theTime, time}", "SALE_PRICE": "售价{price, number, CNY}", - "PHOTO": "你有{num}张照片" + "PHOTO": "你有{num}张照片", + "COMPANY":"{company}是一个国际集团公司", + "BUSINESS":"{company}处理国际性事务" });