Here is what I have in a typescript file for the helpers:
import memoize from "lodash.memoize"; import * as RNLocalize from "react-native-localize"; import i18n from "i18n-js"; import { I18nManager } from "react-native"; const translationGetters = { // lazy requires (metro bundler does not support symlinks) en: () => require("../../src/translations/en.json"), fr: () => require("../../src/translations/fr.json") }; const translate = memoize( (key, config?) => { return 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 as any)[languageTag]() }; i18n.locale = languageTag; }; export { setI18nConfig, translate };
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Here is what I have in a typescript file for the helpers: