Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add defaultVariables options to initialize properties #187

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/react-intl-universal/src/ReactIntlUniversal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
}

Expand All @@ -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);
Expand Down
15 changes: 15 additions & 0 deletions packages/react-intl-universal/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
2 changes: 2 additions & 0 deletions packages/react-intl-universal/test/locales/en-US.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
4 changes: 3 additions & 1 deletion packages/react-intl-universal/test/locales/zh-CN.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}处理国际性事务"
});
1 change: 1 addition & 0 deletions packages/react-intl-universal/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down