Skip to content

Commit

Permalink
Merge pull request #118 from alibaba/common-locale
Browse files Browse the repository at this point in the history
Remove network dependency for common locale data  fix #84
  • Loading branch information
cwtuan authored May 23, 2019
2 parents 461ee66 + c63c82f commit e7b5cc4
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 76 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,11 @@ Note that you are not necessary to load all locale data, just load the current l

```js
import intl from 'react-intl-universal';
// common locale data
require('intl/locale-data/jsonp/en.js');
require('intl/locale-data/jsonp/zh.js');

// locale data
// app locale data
const locales = {
"en-US": require('./locales/en-US.js'),
"zh-CN": require('./locales/zh-CN.js'),
Expand Down Expand Up @@ -283,7 +286,6 @@ require('intl/locale-data/jsonp/ja.js');
* @param {string} options.escapeHtml To escape html. Default value is true.
* @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.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}
Expand Down
7 changes: 6 additions & 1 deletion examples/browser-example/src/App.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import intl from "react-intl-universal";
import intl from 'react-intl-universal';
import _ from "lodash";
import http from "axios";
import React, { Component } from "react";
Expand All @@ -10,6 +10,11 @@ import CurrencyComponent from "./Currency";
import MessageNotInComponent from "./MessageNotInComponent";
import "./app.css";

require('intl/locale-data/jsonp/en.js');
require('intl/locale-data/jsonp/zh.js');
require('intl/locale-data/jsonp/fr.js');
require('intl/locale-data/jsonp/ja.js');

const SUPPOER_LOCALES = [
{
name: "English",
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-intl-universal",
"version": "1.16.2",
"version": "2.0.3",
"description": "Internationalize React apps. Not only for React.Component but also for Vanilla JS.",
"keywords": [
"intl",
Expand Down Expand Up @@ -33,8 +33,6 @@
"intl": "^1.2.5",
"intl-messageformat": "^2.2.0",
"invariant": "^2.2.2",
"is-electron": "^2.1.0",
"load-script": "^1.0.0",
"lodash.merge": "^4.6.1",
"object-keys": "^1.0.11",
"querystring": "^0.2.0"
Expand Down
2 changes: 0 additions & 2 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@ export default {
external : [
'invariant',
'intl-messageformat',
'load-script',
'intl',
'escape-html',
'cookie',
'querystring',
'react',
'console-polyfill',
'lodash.merge',
'is-electron'
],
plugins : [babel({exclude: 'node_modules/**', presets: ['es2015-rollup']})]
};
49 changes: 4 additions & 45 deletions src/ReactIntlUniversal.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,10 @@ import IntlMessageFormat from "intl-messageformat";
import escapeHtml from "escape-html";
import cookie from "cookie";
import queryParser from "querystring";
import load from "load-script";
import invariant from "invariant";
import "console-polyfill";
import * as constants from "./constants";
import merge from "lodash.merge";
import isElectron from 'is-electron';

const COMMON_LOCALE_DATA_URLS = {
en: "https://g.alicdn.com/react-intl-universal/locale-data/1.0.0/en.js",
zh: "https://g.alicdn.com/react-intl-universal/locale-data/1.0.0/zh.js",
fr: "https://g.alicdn.com/react-intl-universal/locale-data/1.0.0/fr.js",
fa: "https://g.alicdn.com/react-intl-universal/locale-data/1.0.0/fa.js",
ja: "https://g.alicdn.com/react-intl-universal/locale-data/1.0.0/ja.js",
de: "https://g.alicdn.com/react-intl-universal/locale-data/1.0.0/de.js",
es: "https://g.alicdn.com/react-intl-universal/locale-data/1.0.0/es.js",
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",
pl: "https://g.alicdn.com/react-intl-universal/locale-data/1.0.0/pl.js",
nl: "https://g.alicdn.com/react-intl-universal/locale-data/1.0.0/nl.js",
sv: "https://g.alicdn.com/react-intl-universal/locale-data/1.0.0/sv.js",
tr: "https://g.alicdn.com/react-intl-universal/locale-data/1.0.0/tr.js",
};

const isBrowser = !isElectron() && !!(typeof window !== 'undefined' &&
window.document &&
window.document.createElement);

String.prototype.defaultMessage = String.prototype.d = function (msg) {
return this || msg || "";
Expand All @@ -46,7 +22,7 @@ class ReactIntlUniversal {
locales: {}, // app locale data like {"en-US":{"key1":"value1"},"zh-CN":{"key1":"值1"}}
warningHandler: console.warn.bind(console), // ability to accumulate missing messages using third party services like Sentry
escapeHtml: true, // disable escape html in variable mode
commonLocaleDataUrls: COMMON_LOCALE_DATA_URLS,
// commonLocaleDataUrls: COMMON_LOCALE_DATA_URLS,
fallbackLocale: null, // Locale to use if a key is not found in the current locale
};
}
Expand Down Expand Up @@ -198,26 +174,9 @@ class ReactIntlUniversal {
);

return new Promise((resolve, reject) => {

const lang = this.options.currentLocale.split('-')[0].split('_')[0];
const langUrl = this.options.commonLocaleDataUrls[lang];
if (isBrowser) {
if (langUrl) {
load(langUrl, (err, script) => {
if (err) {
reject(err);
} else {
resolve();
}
});
} else {
this.options.warningHandler(`Language "${lang}" is not supported. Check https://github.com/alibaba/react-intl-universal/releases/tag/1.12.0`);
resolve();
}
} else {
// For Node.js, common locales are added in the application
resolve();
}
// init() will not load external common locale data anymore.
// But, it still return a Promise for abckward compatibility.
resolve();
});
}

Expand Down
23 changes: 0 additions & 23 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,13 +279,6 @@ test("Get dot key variables", () => {
expect(intl.get("DOT.HELLO")).toBe("Hello World");
});

test("Call warningHandler if currentLocale didn't exist in locales", () => {
const warningHandler = jest.fn();
intl.init({ warningHandler, locales, currentLocale: "fake" });
intl.get("DOT.HELLO");
expect(warningHandler).toHaveBeenCalledTimes(2);
});

test("Get init options", () => {
intl.init({ locales, currentLocale: "en-US" });
const { currentLocale } = intl.getInitOptions();
Expand All @@ -312,27 +305,11 @@ test("Uses default message if key not found in fallbackLocale", () => {
expect(intl.get("not-exist-key").defaultMessage("this is default msg")).toBe("this is default msg");
});

test("Call warningHandler if the languages was not supported", () => {
const warningHandler = jest.fn();
intl.init({ locales, currentLocale: "fake", warningHandler });
expect(warningHandler).toHaveBeenCalledTimes(1);
});

test("Resolve language url if currentLocale was matched", async () => {
const result = await intl.init({ locales, currentLocale: "en" });
expect(result).toBe(undefined);
});

test("Reject language url if language loaded was failed", async () => {
let errorOccured = false;
try {
await intl.init({ locales, commonLocaleDataUrls: { pl: 'http://fakeurl/test' }, currentLocale: 'pl' });
} catch (e) {
errorOccured = true;
}
await expect(errorOccured).toBe(true);
});

test("Resolve directly if the environment is not browser", async () => {
const createElement = window.document.createElement;
Object.defineProperty(window.document, 'createElement', {
Expand Down

0 comments on commit e7b5cc4

Please sign in to comment.