Skip to content

Commit 4957791

Browse files
committed
feat(slim/i18n): add i18n hoc
1 parent 70c74e8 commit 4957791

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
function getSourceCode(appName, { sourceDir }) {
2+
const langUrl = `${sourceDir.locales}/` + '${lang}.json'
3+
4+
return `import React, { useState, useEffect } from 'react'
5+
import { IntlProvider } from 'react-intl'
6+
import axios from 'axios'
7+
8+
const fetchTranslations = async (lang) => {
9+
return await axios.get(\`${langUrl}\`)
10+
}
11+
12+
const withI18n = (Component) => (props) => {
13+
const [messages, setMessages] = useState({})
14+
const locale = 'en'
15+
16+
useEffect(() => {
17+
fetchTranslations(locale).then((response) => {
18+
setMessages(response.data)
19+
})
20+
}, [])
21+
22+
return (
23+
<IntlProvider
24+
locale={locale}
25+
defaultLocale={locale}
26+
messages={messages}
27+
onError={() => {}}
28+
>
29+
<Component {...props} />
30+
</IntlProvider>
31+
)
32+
}
33+
34+
export default withI18n
35+
`
36+
}
37+
38+
module.exports = {
39+
getSourceCode,
40+
};

0 commit comments

Comments
 (0)