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

feat: Support changing the current language without re-importing i18n… #256

Merged
merged 1 commit into from
Dec 2, 2024
Merged
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
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,12 @@ You could make it as [peerDependency](https://github.com/alibaba/react-intl-univ
*/
determineLocale(options)

/**
* Change current locale
* @param {string} newLocale Current locale such as 'en-US'
*/
changeCurrentLocale(newCurrentLocale)

/**
* Get the inital options
* @returns {Object} options includes currentLocale and locales
Expand Down
16 changes: 16 additions & 0 deletions packages/react-intl-universal/src/ReactIntlUniversal.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,22 @@ class ReactIntlUniversal {
this.getLocaleFromBrowser()
);
}

/**
* Change current locale
* @param {string} newLocale Current locale such as 'en-US'
*/
changeCurrentLocale(newLocale) {
if (!this.options.locales || !this.options.locales[newLocale]) {
let errorMsg = `react-intl-universal locales data "${newLocale}" not exists.`;
if (!this.options.locales) {
errorMsg += 'You should call init function first.'
}
this.options.warningHandler(errorMsg);
return;
}
this.options.currentLocale = newLocale;
}

/**
* Initialize properties and load CLDR locale data according to currentLocale
Expand Down
2 changes: 2 additions & 0 deletions packages/react-intl-universal/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const getHTML = defaultInstance.getHTML.bind(defaultInstance);
const formatMessage = defaultInstance.formatMessage.bind(defaultInstance);
const formatHTMLMessage = defaultInstance.formatHTMLMessage.bind(defaultInstance);
const determineLocale = defaultInstance.determineLocale.bind(defaultInstance);
const changeCurrentLocale = defaultInstance.changeCurrentLocale.bind(defaultInstance);
const init = defaultInstance.init.bind(defaultInstance);
const getInitOptions = defaultInstance.getInitOptions.bind(defaultInstance);
const load = defaultInstance.load.bind(defaultInstance);
Expand All @@ -25,6 +26,7 @@ export {
formatMessage,
formatHTMLMessage,
determineLocale,
changeCurrentLocale,
init,
getInitOptions,
load,
Expand Down
7 changes: 7 additions & 0 deletions packages/react-intl-universal/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ test("Set specific locale", () => {
expect(intl.get("SIMPLE")).toBe("Simple");
});

test("Change specific locale", () => {
intl.init({ locales, currentLocale: "en-US" });
expect(intl.get("SIMPLE")).toBe("Simple");
intl.changeCurrentLocale("zh-CN");
expect(intl.get("SIMPLE")).toBe("简单");
});

test("Message with variables", () => {
intl.init({ locales, currentLocale: "en-US" });
expect(intl.get("HELLO", { name: "Tony" })).toBe("Hello, Tony");
Expand Down