【问题标题】:React-Native - i18n separate filesReact-Native - i18n 独立文件
【发布时间】:2016-12-25 17:01:03
【问题描述】:

我使用 i18n 模块 (https://github.com/AlexanderZaytsev/react-native-i18n)。它工作得很好,但我想将翻译分成单独的文件。我偶然发现了一篇不错的博文 (https://blog.redradix.com/6-essential-libraries-to-use-on-your-next-react-native-app/),它展示了我想做的用例。

// src/config/locales/en.js
const en = {  
  welcome: 'welcome',
};
export default en;


// src/config/locales/es.js
const es = {  
  welcome: 'bienvenido',
};
export default es;  


//src/config/i18n.js
import I18n from 'react-native-i18n';

import es from './locales/es';  
import en from './locales/en';

I18n.fallbacks = true;

I18n.translations = {  
  en: en,
  es: es,
};

export default I18n; 


//usage in components
import I18n from '../config/i18n';

  render() {
    return (
      <Text>{I18n.t('welcome')}</Text>
    )
  }

我收到一个错误:“无法读取未定义的属性 't'”。我是一般反应的新手。我做错了什么?

【问题讨论】:

    标签: reactjs internationalization react-native


    【解决方案1】:

    我的问题中的代码就像一个魅力。我只是在顶部的导入中有错误的括号

    更改自:

    import { I18n } from '../config/i18n'
    

    到:

    import I18n from '../config/i18n'
    

    【讨论】:

    • 嗨,希望你一切都好。我知道自从您发布此答案以来已经有一段时间了,但是如果您可以看一下,我有一个类似的问题。 stackoverflow.com/questions/65561657/…
    猜你喜欢
    • 1970-01-01
    • 2018-09-22
    • 2017-12-09
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 2019-02-17
    • 1970-01-01
    • 2016-08-08
    相关资源
    最近更新 更多