2016-08-18 88 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'