How to use the react-native-localize.findBestAvailableLanguage function in react-native-localize

To help you get started, we’ve selected a few react-native-localize examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github react-native-community / react-native-localize / example / src / AsyncExample.js View on Github external
const translationsDir = await (Platform.OS === "android"
    ? RNFS.readDirAssets("translations")
    : RNFS.readDir(RNFS.MainBundlePath + "/translations"));

  const translationPaths = translationsDir
    .filter(({ isFile, name }) => isFile() && name.endsWith(".json"))
    .reduce((all, { name, path }) => {
      const languageTag = name.replace(".json", "");
      return { ...all, [languageTag]: path };
    }, {});

  // fallback if no available language fits
  const fallback = { languageTag: "en", isRTL: false };

  const { languageTag, isRTL } =
    RNLocalize.findBestAvailableLanguage(Object.keys(translationPaths)) ||
    fallback;

  const fileContent = await (Platform.OS === "android"
    ? RNFS.readFileAssets(translationPaths[languageTag], "utf8")
    : RNFS.readFile(translationPaths[languageTag], "utf8"));

  // clear translation cache
  translate.cache.clear();
  // update layout direction
  I18nManager.forceRTL(isRTL);

  // set i18n-js config
  i18n.translations = { [languageTag]: JSON.parse(fileContent) };
  i18n.locale = languageTag;
};
github NordicMuseum / Nordic-Museum-Audio-Guide / v2 / src / i18n.js View on Github external
export const setI18nConfig = forceLocale => {
  // fallback if no available language fits
  const tagFallback = { languageTag: 'en', isRTL: false };

  let language = {};
  const languageTags = Object.keys(translationGetters);
  if (forceLocale && languageTags.includes(forceLocale)) {
    language = { languageTag: forceLocale, isRTL: forceLocale === 'ar' };
  } else {
    language =
      RNLocalize.findBestAvailableLanguage(languageTags) || tagFallback;
  }

  let stringFallback;
  i18n.fallbacks = true;
  switch (forceLocale) {
    case 'svKids':
    case 'svSimple':
    case 'seSme':
    case 'seSmj':
    case 'seSma': {
      stringFallback = 'sv';
      break;
    }

    default: {
      stringFallback = 'en';
github tranhoangduong1994 / react-native-boilerplate / src / utils / LocalizationUtils.js View on Github external
const setI18nConfig = () => {
  const fallback = {
    languageTag: 'vi',
    isRTL: false
  };

  const { languageTag, isRTL } = RNLocalize.findBestAvailableLanguage(Object.keys(translationGetters)) || fallback;

  t.cache.clear();
  I18nManager.forceRTL(isRTL);

  i18n.translations = { [languageTag]: translationGetters[languageTag]() };
  i18n.locale = languageTag;
};
github rastapasta / foodsharing / src / common / translation.tsx View on Github external
const setI18nConfig = () => {
  const fallback = { languageTag: "en", isRTL: false }

  const { languageTag, isRTL } =
    RNLocalize.findBestAvailableLanguage(Object.keys(translationGetters)) ||
    fallback

  translate.cache.clear();
  I18nManager.forceRTL(isRTL)

  i18n.translations = { [languageTag]: translationGetters[languageTag]() }
  i18n.locale = languageTag

  moment.locale(languageTag)
}
github OriginProtocol / origin / mobile / src / utils / language.js View on Github external
export function findBestAvailableLanguage() {
  const bestAvailable = RNLocalize.findBestAvailableLanguage(
    Object.keys(TRANSLATIONS).map(x => x.replace('_', '-').substr(0, 2))
  )

  let language = 'en_US'
  if (bestAvailable && bestAvailable.languageTag) {
    language = Object.keys(TRANSLATIONS).find(x =>
      x.startsWith(bestAvailable.languageTag)
    )
  }
  return language
}
github burst-apps-team / phoenix / mobile / src / core / i18n.ts View on Github external
import i18nJS from 'i18n-js';
import { findBestAvailableLanguage } from 'react-native-localize';
import en from '../translations/en.json';
import ru from '../translations/ru.json';

const fallback = { languageTag: 'en', isRTL: false };
const { languageTag } = findBestAvailableLanguage(['en-US', 'en', 'ru']) || fallback;

i18nJS.locale = languageTag;
i18nJS.fallbacks = true;
i18nJS.translations = { en, ru };

export const i18n = i18nJS;
github infinitered / ignite-bowser / boilerplate / app / i18n / i18n.ts View on Github external
import * as RNLocalize from "react-native-localize"
import i18n from "i18n-js"

const en = require("./en")
const ja = require("./ja")

i18n.fallbacks = true
i18n.translations = { en, ja }

const fallback = { languageTag: "en", isRTL: false }
const { languageTag } = RNLocalize.findBestAvailableLanguage(Object.keys(i18n.translations)) || fallback
i18n.locale = languageTag
github celo-org / celo-monorepo / packages / mobile / src / i18n.ts View on Github external
function getLanguage() {
  const fallback = { languageTag: 'en', isRTL: false }
  const { languageTag } =
    RNLocalize.findBestAvailableLanguage(Object.keys(availableResources)) || fallback
  return languageTag
}