-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocales.js
executable file
·41 lines (33 loc) · 1.14 KB
/
locales.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* i18n.js
*
* This will setup the i18n language files and locale data for your app.
*
*/
import { addLocaleData } from 'react-intl';
import enLocaleData from 'react-intl/locale-data/en';
import zhLocaleData from 'react-intl/locale-data/zh';
import enTranslationMessages from './i18n/en.json';
import zhTranslationMessages from './i18n/zh.json';
addLocaleData(enLocaleData);
addLocaleData(zhLocaleData);
export const DEFAULT_LOCALE = 'en';
export const appLocales = [
'en',
'zh',
];
export const formatTranslationMessages = (locale, messages) => {
const defaultFormattedMessages = locale !== DEFAULT_LOCALE
? formatTranslationMessages(DEFAULT_LOCALE, enTranslationMessages)
: {};
return Object.keys(messages).reduce((formattedMessages, key) => {
const formattedMessage = !messages[key] && locale !== DEFAULT_LOCALE
? defaultFormattedMessages[key]
: messages[key];
return Object.assign(formattedMessages, { [key]: formattedMessage });
}, {});
};
export const translationMessages = {
en: formatTranslationMessages('en', enTranslationMessages),
zh: formatTranslationMessages('zh', zhTranslationMessages),
};