首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >非组件的翻译。(我已经看过Intl了)

非组件的翻译。(我已经看过Intl了)
EN

Stack Overflow用户
提问于 2021-11-08 06:10:08
回答 1查看 28关注 0票数 0

app fabric有没有什么东西可以直接支持数据转换(注意:我不想要React的Intl,因为它是为了在渲染之前本地化数据)。我需要本地化状态中存在的数据,并使用转换后的数据更新状态。

React的intl旨在与组件一起使用,我需要对组件外部存在的数据进行转换。我不想使用createIntl模块,因为它需要手动提供区域设置和消息。

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

代码语言:javascript
复制
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 ...
}
EN

回答 1

Stack Overflow用户

发布于 2021-11-08 06:32:30

您可以使用i18next将数据转换为纯js格式。

基本示例:

代码语言:javascript
复制
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这样的情况,它可能也需要翻译。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69879248

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档