react-native-i18n icon indicating copy to clipboard operation
react-native-i18n copied to clipboard

Updating I18n.translations with 'live' data: is it possible?

Open marco-solari opened this issue 5 years ago • 6 comments

I would like to 'live' translate my app... I.e.: after I load static json translations files, I want to update it with possible live updates from a public url. But this does not seem too update translations...
This is my code:

import I18n from 'react-native-i18n';

import en from '../Assets/Locales/en.json';
import it from '../Assets/Locales/it.json';
import fr from '../Assets/Locales/fr.json';

// use static languages, initially
I18n.translations = { en, it, fr };

I18n.fallbacks = true;

// 'live' update
let language = 'it'; // (just update italian language, to make things simple...)
let url = `https://my.public.server/i18n/${language}.json`;
fetch(url)
  .then(response => response.json())
  .then(newTranslations => {
    const mergedContents = Object.assign(I18n.translations[language], newTranslations);
    I18n.translations[language] = mergedContents;
    // add new json language merged with current JSON language to I18n.translations
    /**
     * NOTE: code reaches here, and I18n.translations[language] is updated with live
     *       contents, but new translations are not displayed by the app... :-(
     *       Probably I should call some I18n.translationsRefresh() method, here...
     */
  })
;  

export default I18n;

Any clue?

marco-solari avatar Jun 19 '19 15:06 marco-solari

Please leave a comment here if you find a solution. I am looking for the same solution. So far the one option I can think of is to possibly register app for codePush and push your translation files as and when updates are there. I am interested to know if there is another easier option.

getavinashm avatar Jun 20 '19 06:06 getavinashm

Have you given a shot at 'react-native-localization' ? They seem to support update with strings.setContent()

getavinashm avatar Jun 20 '19 06:06 getavinashm

@getavinashm: I gave a shot to react-native-localization. I see they do update contents 'live', but I want to stay with i18n... I'm looking for some sort of I18m.translationsRefresh() method...

marco-solari avatar Jun 20 '19 07:06 marco-solari

Hey guys, any solution?

Nitingadhiya avatar Jun 21 '19 09:06 Nitingadhiya

Hey you guys, Do you guys have any solution?

hvlong avatar Nov 28 '19 03:11 hvlong