React-i18next: Question about resource loading in react-native

Created on 14 Jan 2020  路  6Comments  路  Source: i18next/react-i18next

Hello, how can i load a resource from my project, for example:

Translations are in assets/langs/en/translations.json and my code is:

i18n .use(i18backend) .use(initReactI18next) .init({ lng: 'en', fallbackLng: 'en', resources: {}, preload: ['en'], react: { useSuspense: false }, backend: { loadPath: '../assets/{{lng}}/{{ns}}.json' } });

Should i use the backend plugin for local loading or add them manually?

Most helpful comment

@LaVielle depends...if initial bundlesize is a lot larger because all the translations got bundled (even all languages) that might have a significant impact on loading time -> not relevant for react-native - but for sure in web

All 6 comments

Yea i was wondering if there's a way to read from the filesystem and load all the languages at once because i have multiple languages with about 6-7 namespaces and its a pain to add them manually :/.

as react-native does not use webpack there is no context you could use to get all files in a folder...so...no...there is no magic to import files

@tankpower1 In my experience, it's best to add all translations at once when calling init.

In my project (2, languages, ~25 namespaces/language, ~500 strings/language) I saw no drawbacks to adding all this on init i18n.init({resources: allMyTranslations, ...otherOptions})
I see only benefits because now I don't need to manually add resources when i need them in components, which was super messy.

So to answer your initial question, I'd say export your translations, import then in your i18n file, and then init resources with this. No need for backend solution if you're just loading local strings.

@jamuhl Not sure if my way is best practice though cause the docs don't explicitly say to init this way.

@LaVielle depends...if initial bundlesize is a lot larger because all the translations got bundled (even all languages) that might have a significant impact on loading time -> not relevant for react-native - but for sure in web

@tankpower1 In my experience, it's best to add all translations at once when calling init.

In my project (2, languages, ~25 namespaces/language, ~500 strings/language) I saw no drawbacks to adding all this on init i18n.init({resources: allMyTranslations, ...otherOptions})
I see only benefits because now I don't need to manually add resources when i need them in components, which was super messy.

So to answer your initial question, I'd say export your translations, import then in your i18n file, and then init resources with this. No need for backend solution if you're just loading local strings.

@jamuhl Not sure if my way is best practice though cause the docs don't explicitly say to init this way.

Hi @LaVielle ,

Can you share the structure of the "allMyTranslations" object. How did you configure that?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ok2ju picture ok2ju  路  3Comments

a-barbieri picture a-barbieri  路  4Comments

oyeanuj picture oyeanuj  路  3Comments

jadbox picture jadbox  路  3Comments

flq picture flq  路  4Comments