DEV Community

Discussion on: Creating a Multi-Language App in React Native

Collapse
 
anytram profile image
Martyna

Hello, I have problem with rewrite your code using Typescript, can you help me with types?

Collapse
 
omatrot profile image
Olivier MATROT • Edited

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 };
Collapse
 
vikrantnegi profile image
Vikrant Negi

Sorry, I'm not very good at the typescript.