A toolbox for your React Native app localization.
—
Intelligent language tag matching to find the best supported language from your app's available translations based on device locale preferences.
Finds the best matching language tag from a provided list based on the device's locale preferences. Uses intelligent matching that considers language-country combinations, language-script combinations, and language-only fallbacks.
/**
* Find the best matching language tag from provided options
* @param languageTags - Array of supported language tags in your app
* @returns Object with matched language tag and RTL flag, or undefined if no match
*/
function findBestLanguageTag<T extends string>(
languageTags: ReadonlyArray<T>
): { languageTag: T; isRTL: boolean } | undefined;Usage Examples:
import { findBestLanguageTag } from "react-native-localize";
// Basic language matching
const supportedLanguages = ["en-US", "fr-FR", "es-ES", "de-DE"];
const bestMatch = findBestLanguageTag(supportedLanguages);
if (bestMatch) {
console.log(`Selected language: ${bestMatch.languageTag}`);
console.log(`Is RTL: ${bestMatch.isRTL}`);
// Use this language for your app's localization
} else {
console.log("No matching language found, using default");
// Fall back to default language
}Advanced Usage:
import { findBestLanguageTag } from "react-native-localize";
// App with multiple language variants
const supportedLanguages = [
"en-US", // American English
"en-GB", // British English
"fr-FR", // French (France)
"fr-CA", // French (Canada)
"es-ES", // Spanish (Spain)
"es-MX", // Spanish (Mexico)
"ar-SA", // Arabic (Saudi Arabia)
"he-IL" // Hebrew (Israel)
];
const match = findBestLanguageTag(supportedLanguages);
if (match) {
// Configure app language
setAppLanguage(match.languageTag);
// Configure layout direction
if (match.isRTL) {
setLayoutDirection('rtl');
} else {
setLayoutDirection('ltr');
}
}
// Usage with fallback strategy
function selectAppLanguage(supportedLanguages: string[]) {
// Try exact matching first
let match = findBestLanguageTag(supportedLanguages);
if (!match && supportedLanguages.includes("en")) {
// Fall back to English if available
match = { languageTag: "en" as any, isRTL: false };
}
return match?.languageTag || "en";
}Integration with i18n Libraries:
import { findBestLanguageTag } from "react-native-localize";
import i18n from "i18next";
// Configure i18next based on device preferences
const supportedLanguages = Object.keys(i18n.options.resources || {});
const bestLanguage = findBestLanguageTag(supportedLanguages);
if (bestLanguage) {
i18n.changeLanguage(bestLanguage.languageTag);
}
// React Native usage with React Navigation
import { findBestLanguageTag } from "react-native-localize";
function configureApp() {
const supportedLanguages = ["en", "es", "fr", "de", "ar", "he"];
const match = findBestLanguageTag(supportedLanguages);
if (match) {
// Set app language
setI18nLanguage(match.languageTag);
// Configure navigation for RTL languages
if (match.isRTL) {
// Enable RTL support in navigation
NavigationContainer.setRTL(true);
}
}
}The findBestLanguageTag function uses a sophisticated matching algorithm:
The function processes device locales in order of preference, ensuring the best possible match for the user's language preferences.
Install with Tessl CLI
npx tessl i tessl/npm-react-native-localize