Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-Native - i18n separate files

I use the i18n module (https://github.com/AlexanderZaytsev/react-native-i18n). It works great but I want to split the translations into seperate files. I came accross a nice blog post (https://blog.redradix.com/6-essential-libraries-to-use-on-your-next-react-native-app/) which shows the usecase like I want to do.

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

I got an error: "Cannot read property 't' of undefined". I am a newbie in react in general. What did I wrong?

like image 331
Yetispapa Avatar asked Aug 18 '16 16:08

Yetispapa


1 Answers

The code from my question works like a charm. I just have wrong brackets in my import on the top

change from:

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

to:

import I18n from '../config/i18n'
like image 163
Yetispapa Avatar answered Nov 16 '22 07:11

Yetispapa