Skip to content

Commit 584c5aa

Browse files
committed
update i18nHelper
1 parent 51af623 commit 584c5aa

File tree

1 file changed

+104
-15
lines changed

1 file changed

+104
-15
lines changed
Lines changed: 104 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,113 @@
11
import React from 'react';
2-
import I18n from 'i18n-js';
2+
import { addLocaleData, defineMessages } from 'react-intl';
3+
import en from 'react-intl/locale-data/en';
4+
import de from 'react-intl/locale-data/de';
5+
import ja from 'react-intl/locale-data/ja';
6+
import zh from 'react-intl/locale-data/zh';
7+
import _ from 'lodash';
38

4-
const InitI18n = (railsContext) => {
5-
I18n.translations = JSON.parse(railsContext.translations);
6-
I18n.defaultLocale = railsContext.i18nDefaultLocale;
7-
I18n.fallbacks = true;
9+
const InitI18nLocale = () => {
10+
addLocaleData([...en, ...de, ...ja, ...zh]);
811
};
912

10-
const SelectLanguage = (onChange) => (
11-
<select onChange={(e) => onChange(e.target.value)} >
12-
<option value="en">English</option>
13-
<option value="de">Deutsch</option>
14-
<option value="ja">日本語</option>
15-
<option value="zh-CN">简体中文</option>
16-
<option value="zh-TW">正體中文</option>
13+
const SelectLanguage = (onChange, locale = defaultLocale) => (
14+
<select onChange={(e) => onChange(e.target.value)} value={locale} >
15+
<option value='en'>English</option>
16+
<option value='de'>Deutsch</option>
17+
<option value='ja'>日本語</option>
18+
<option value='zh-CN'>简体中文</option>
19+
<option value='zh-TW'>正體中文</option>
1720
</select>
1821
);
1922

20-
const SetI18nLocale = (locale) => {
21-
I18n.locale = locale;
23+
const defaultLocale = 'en';
24+
25+
const defaultMessages = defineMessages({
26+
type: {
27+
id: 'type',
28+
defaultMessage: 'English',
29+
},
30+
comments: {
31+
id: 'comments',
32+
defaultMessage: 'Comments',
33+
},
34+
descriptionSupportMarkdown: {
35+
id: 'description.support_markdown',
36+
defaultMessage: 'Text supports Github Flavored Markdown.',
37+
},
38+
descriptionDeleteRule: {
39+
id: 'description.delete_rule',
40+
defaultMessage: 'Comments older than 24 hours are deleted.',
41+
},
42+
descriptionSubmitRule: {
43+
id: 'description.submit_rule',
44+
defaultMessage: 'Name is preserved. Text is reset, between submits.',
45+
},
46+
formHorizontal: {
47+
id: 'form.horizontal',
48+
defaultMessage: 'Horizontal Form',
49+
},
50+
formStacked: {
51+
id: 'form.stacked',
52+
defaultMessage: 'Stacked Form',
53+
},
54+
formInline: {
55+
id: 'form.inline',
56+
defaultMessage: 'Inline Form',
57+
},
58+
inputNameLabel: {
59+
id: 'input.name.label',
60+
defaultMessage: 'Name',
61+
},
62+
inputNamePlaceholder: {
63+
id: 'input.name.placeholder',
64+
defaultMessage: 'Your Name',
65+
},
66+
inputTextLabel: {
67+
id: 'input.text.label',
68+
defaultMessage: 'Text',
69+
},
70+
inputTextPlaceholder: {
71+
id: 'input.text.placeholder',
72+
defaultMessage: 'Say something using markdown...',
73+
},
74+
inputSaving: {
75+
id: 'input.saving',
76+
defaultMessage: 'Saving',
77+
},
78+
inputPost: {
79+
id: 'input.post',
80+
defaultMessage: 'Post',
81+
},
82+
});
83+
84+
const convertTranslations = (trans, locale) => {
85+
const translations = JSON.parse(trans);
86+
const flatMsg = {};
87+
for (const key in translations) {
88+
const tmp = {};
89+
tmp[key] = flattenMessages(translations[key]);
90+
Object.assign(flatMsg, tmp);
91+
}
92+
return _.isEmpty(flatMsg) ? null : flatMsg[`${locale}`];
93+
};
94+
95+
const flattenMessages = (msg, prefix = '') => {
96+
if (!_.isEmpty(msg)) {
97+
return Object.keys(msg).reduce((messages, key) => {
98+
let value = msg[key];
99+
let prefixedKey = prefix ? `${prefix}.${key}` : key;
100+
101+
if (typeof value === 'string') {
102+
messages[prefixedKey] = value;
103+
} else {
104+
Object.assign(messages, flattenMessages(value, prefixedKey));
105+
}
106+
107+
return messages;
108+
}, {});
109+
};
22110
};
23111

24-
export { InitI18n, SelectLanguage, SetI18nLocale };
112+
export { InitI18nLocale, SelectLanguage, convertTranslations,
113+
defaultMessages, defaultLocale };

0 commit comments

Comments
 (0)