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?
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With