2016-08-18 33 views
7

我使用i18n模块(https://github.com/AlexanderZaytsev/react-native-i18n)。它效果很好,但我想将翻译分成单独的文件。我遇到了一篇不错的博客文章(https://blog.redradix.com/6-essential-libraries-to-use-on-your-next-react-native-app/),其中显示了我想要做的用例。React-Native - i18n单独文件

// 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> 
    ) 
    } 

我得到一个错误:“无法读取未定义的属性'。一般来说,我是一名新手。我错了什么?

回答

6

我的问题的代码就像一个魅力。我只是在我错了进口支架上从顶部

变化:

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

到:

import I18n from '../config/i18n'