Created
July 8, 2019 10:38
-
-
Save vikrantnegi/67952226369d72d882d4d2c25d54ad4c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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