非组件翻译.(我已经看过Intl)



应用程序结构是否具有直接支持数据翻译的功能(注意:我不想要React的Intl,因为它是用于在数据呈现之前对其进行本地化的(。我需要本地化状态中存在的数据,并使用翻译后的数据更新状态。

React的intl旨在与组件一起使用,我需要对组件外部的数据进行翻译。我不想使用createIntl模块,因为这需要手动提供locale&消息。

这就是我正在处理的代码。它存在于组件外部。

export function func () {
client.get (url, {}).then (
response => {
// handle error
}).then(data => {
// This is where i want to translate the data based on the locale i am in
// so that the data this translated data is present in the state
}
// error-handling, more code ...
}

您可以使用i18next在纯js中转换数据。

基本示例:

import i18next from 'i18next';
i18next.init({
lng: 'en', // if you're using a language detector, do not define the lng option
debug: true,
resources: {
en: {
translation: {
"key": "hello world"
}
}
}
});
// initialized and ready to go!
// i18next is already initialized, because the translation resources where passed via init function
document.getElementById('output').innerHTML = i18next.t('key');

顺便说一下,为什么不在服务器端翻译数据呢?您可能会遇到一些类似export excel的情况,这些情况也可能需要翻译。

相关内容

最新更新