Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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;
};
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';
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;
};
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)
}
{Platform.OS === "android" && (
<>
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
}
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;
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
function getLanguage() {
const fallback = { languageTag: 'en', isRTL: false }
const { languageTag } =
RNLocalize.findBestAvailableLanguage(Object.keys(availableResources)) || fallback
return languageTag
}