generated from wednesday-solutions/react-template
-
Notifications
You must be signed in to change notification settings - Fork 13
/
i18n.ts
36 lines (30 loc) · 1.19 KB
/
i18n.ts
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
/**
* 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 enTranslationMessages = require('./translations/en.json');
export const DEFAULT_LOCALE = 'en';
// prettier-ignore
export const appLocales = [
'en',
];
type Locale = string;
type MessageRecord = Record<string, string>;
export const formatTranslationMessages = (locale: Locale, messages: MessageRecord) => {
const defaultFormattedMessages: MessageRecord =
locale !== DEFAULT_LOCALE ? formatTranslationMessages(DEFAULT_LOCALE, enTranslationMessages) : {};
const flattenFormattedMessages = (formattedMessages: MessageRecord, key: string) => {
const formattedMessage: string =
!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)
};