Skip to content

Commit

Permalink
Merge pull request #52 from alibaba/mutiple-locale-data
Browse files Browse the repository at this point in the history
Load Multiple Locale Data
  • Loading branch information
cwtuan authored Apr 3, 2018
2 parents 95c8408 + e866916 commit c8e1990
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 1 deletion.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,14 @@ require('intl/locale-data/jsonp/ja.js');
*/
init(options)


/**
* Load more locales after init
* @param {Object} locales App locale data
*/
export function load(locales): void;


/**
* Get the formatted message by key
* @param {string} key The string representing key in locale data file
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.8.4",
"version": "1.9.2",
"description": "Internationalize React apps. Not only for React.Component but also for Vanilla JS.",
"keywords": [
"intl",
Expand Down
8 changes: 8 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const COMMON_LOCALE_DATA_URLS = {
ko: "https://g.alicdn.com/react-intl-universal/locale-data/1.0.0/ko.js",
pt: "https://g.alicdn.com/react-intl-universal/locale-data/1.0.0/pt.js",
it: "https://g.alicdn.com/react-intl-universal/locale-data/1.0.0/it.js",
ru: "https://g.alicdn.com/react-intl-universal/locale-data/1.0.0/ru.js",
};


Expand Down Expand Up @@ -202,6 +203,13 @@ class ReactIntlUniversal {
return this.options;
}

/**
* Load more locales after init
*/
load(locales) {
Object.assign(this.options.locales, locales);
}

getLocaleFromCookie(options) {
const { cookieLocaleKey } = options;
if (cookieLocaleKey) {
Expand Down
10 changes: 10 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import cookie from "cookie";
import intl from "../src/index";
import zhCN from "./locales/zh-CN";
import enUS from "./locales/en-US";
import enUSMore from "./locales/en-US-more";

const locales = {
"en-US": enUS,
Expand Down Expand Up @@ -258,3 +259,12 @@ test("Get init options", () => {
expect(currentLocale).toBe("en-US");
});

test("load mutiple locale data", () => {
intl.init({ locales, currentLocale: "en-US" });
const localesMore = {
"en-US": enUSMore,
};
intl.load(localesMore);
expect(intl.get("MORE")).toBe("More data");
});

3 changes: 3 additions & 0 deletions test/locales/en-US-more.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = ({
"MORE": "More data"
});
6 changes: 6 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ declare module "react-intl-universal" {
*/
export function init(options: ReactIntlUniversalOptions): Promise<void>;

/**
* Load more locales after init
* @param {Object} locales App locale data
*/
export function load(locales: { [key: string]: any }): void;

export interface ReactIntlUniversalOptions {
currentLocale?: string;
locales?: { [key: string]: any };
Expand Down

0 comments on commit c8e1990

Please sign in to comment.