forked from wednesday-solutions/react-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
i18n.js
33 lines (28 loc) · 1006 Bytes
/
i18n.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
/**
* i18n.js
*
* This will setup the i18n language files and locale data for your app.
*
*/
const enTranslationMessages =
process.env.NODE_ENV === 'production'
? require('@app/translations/en').messages
: require('@app/translations/en.json');
export const DEFAULT_LOCALE = 'en';
// prettier-ignore
export const appLocales = [
'en',
];
export const formatTranslationMessages = (locale, messages) => {
const defaultFormattedMessages =
locale !== DEFAULT_LOCALE ? formatTranslationMessages(DEFAULT_LOCALE, enTranslationMessages) : {};
const flattenFormattedMessages = (formattedMessages, key) => {
const formattedMessage =
!messages[key] && locale !== DEFAULT_LOCALE ? defaultFormattedMessages[key] : messages[key];
return Object.assign(formattedMessages, { [key]: formattedMessage });
};
return Object.keys(messages).reduce(flattenFormattedMessages, {});
};
export const translationMessages = {
en: formatTranslationMessages('en', enTranslationMessages)
};