[英]React-intl parse json array in locale file
I'm currently working with React-intl v. ^4.7.6
.我目前正在使用
React-intl v. ^4.7.6
。 I have a folder called translations
where all my locale translations are in. I use IntlProvider
to select which file to load based on the user's browser.我有一个名为
translations
的文件夹,我所有的语言环境翻译都在其中。我使用IntlProvider
根据用户的浏览器选择要加载的文件。 It seems like FormatttedMessage
or intl.formatMessage
can't do this.似乎
FormatttedMessage
或intl.formatMessage
不能这样做。
Here is what my translation file looks like.这是我的翻译文件的样子。 Of course, there would be one for every language.
当然,每种语言都会有一个。
transtions/eng.json
{
"header.about": "About",
"header.resume": "Resume",
"Education": [{
"school": "ABC University",
"degree": "(B.Eng)",
"graduated": "2030 - Present",
"location": "Canada",
"description": [
"d1",
"d2"
]
},
{
"school": "College",
"degree": "Science",
"graduated": "2030",
"location": "Canada",
"description": [
"Graduated with honors"
]
}]
[... other translations ...]
}
I have another approach to resolve your problem.我有另一种方法来解决您的问题。 (Though it might not be the best solution and it is not exactly using your en/ german translate, but i think they are similar)
(虽然它可能不是最好的解决方案,也不是完全使用你的英文/德文翻译,但我认为它们是相似的)
First we can define a file with all localization keys:首先,我们可以定义一个包含所有本地化键的文件:
// i18n/keys.js
export const student = {
projects: [
{
name: "student.projects.0.name",
desc: "student.projects.0.desc",
},
{
name: "student.projects.1.name",
desc: "student.projects.1.desc",
},
],
};
export const localizationKeys = {
student: student,
};
In i18n locale files, we can define the translations as below:在 i18n 语言环境文件中,我们可以定义如下翻译:
// i18n/en/student.js
export const student = {
projects: [
{
name: "Camera",
desc: "Photo",
},
{
name: "Foods",
desc: "Apple",
},
],
};
// i18n/zh_tw/student.js
export const student = {
projects: [
{
name: "拍攝",
desc: "照片",
},
{
name: "食物",
desc: "蘋果",
},
],
};
Export the translations in i18n to locale modules for react-i18n将 i18n 中的翻译导出到 react-i18n 的语言环境模块
// i18n/en/index.js
import { student } from "./student";
const en = {
general: general,
};
export default en;
// i18n/zh_tw/index.js
import { student } from "./student";
const zh_tw = {
student: student,
};
export default zh_tw;
In your react components, you can access your localization Keys (keys.js) to get back each of the i18n keys name.在您的 React 组件中,您可以访问您的本地化密钥 (keys.js) 以获取每个 i18n 密钥名称。
import React from "react";
import { useIntl } from "react-intl";
import { localizationKeys } from "./i18n/keys";
import { IntlProvider } from "react-intl";
import zh_tw from "./i18n/zh_tw";
import flatten from "flat";
import en from "./i18n/en";
const messages = {
en: flatten(en),
"zh-Hant-TW": flatten(zh_tw),
};
export default function ProjectShowcase() => {
const intl = useIntl();
const [locale, setLocale] = useState(navigator.language);
const [mergedMessages, setMergedMessages] = useState(messages["en"]);
useEffect(() => {
// Merging english and current locale, avoid showing Text id if cannot look for the translate in locale file
// In this case, it will always show EN translate as default if the text-id not found in a "zh_tw" locale
setMergedMessages(Object.assign({}, messages["en"], messages[locale]));
}, [locale]);
return (
<IntlProvider
messages={mergedMessages}
locale={locale}
key={locale}
defaultLocale="en"
>
{localizationKeys.student.projects.map((p, idx) => {
return (
<ul>
<li>
<span>
{intl.formatMessage({
id: localizationKeys.student.projects[idx].name,
})}
</span>
{` (${intl.formatMessage({
id: localizationKeys.student.projects[idx].desc,
})})`}
</li>
</ul>
);
})
}
</IntlProvider>
)
}
I have developed a little example on the react-intl, you may find more completed code inside: https://github.com/applelok/react-localization我在 react-intl 上开发了一个小例子,你可以在里面找到更完整的代码: https : //github.com/applelok/react-localization
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.