Skip to content

Instantly share code, notes, and snippets.

@vikrantnegi
Created July 8, 2019 10:38
Show Gist options
  • Save vikrantnegi/67952226369d72d882d4d2c25d54ad4c to your computer and use it in GitHub Desktop.
Save vikrantnegi/67952226369d72d882d4d2c25d54ad4c to your computer and use it in GitHub Desktop.
const translationGetters = {
// lazy requires (metro bundler does not support symlinks)
ar: () => require("./src/translations/ar.json"),
en: () => require("./src/translations/en.json"),
fr: () => require("./src/translations/fr.json")
};
const translate = memoize(
(key, config) => i18n.t(key, config),
(key, config) => (config ? key + JSON.stringify(config) : key)
);
const setI18nConfig = () => {
// fallback if no available language fits
const fallback = { languageTag: "en", isRTL: false };
const { languageTag, isRTL } =
RNLocalize.findBestAvailableLanguage(Object.keys(translationGetters)) ||
fallback;
// clear translation cache
translate.cache.clear();
// update layout direction
I18nManager.forceRTL(isRTL);
// set i18n-js config
i18n.translations = { [languageTag]: translationGetters[languageTag]() };
i18n.locale = languageTag;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment