forked from react-boilerplate/react-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathi18n.js
51 lines (44 loc) · 1.62 KB
/
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* i18n.js
*
* This will setup the i18n language files and locale data for your app.
*
* IMPORTANT: This file is used by the internal build
* script `extract-intl`, and must use CommonJS module syntax
* You CANNOT use import/export in this file.
*/
const addLocaleData = require('react-intl').addLocaleData; //eslint-disable-line
const enLocaleData = require('react-intl/locale-data/en');
const deLocaleData = require('react-intl/locale-data/de');
const enTranslationMessages = require('./translations/en.json');
const deTranslationMessages = require('./translations/de.json');
addLocaleData(enLocaleData);
addLocaleData(deLocaleData);
const DEFAULT_LOCALE = 'en';
// prettier-ignore
const appLocales = [
'en',
'de',
];
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, {});
};
const translationMessages = {
en: formatTranslationMessages('en', enTranslationMessages),
de: formatTranslationMessages('de', deTranslationMessages),
};
exports.appLocales = appLocales;
exports.formatTranslationMessages = formatTranslationMessages;
exports.translationMessages = translationMessages;
exports.DEFAULT_LOCALE = DEFAULT_LOCALE;