React-Native - i18n 独立文件
·
问题:React-Native - i18n 独立文件
我使用 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'”。我是一般反应的新手。我做错了什么?
解答
我的问题中的代码就像一个魅力。我只是在顶部的导入中有错误的括号
更改自:
import { I18n } from '../config/i18n'
至:
import I18n from '../config/i18n'
更多推荐
所有评论(0)