CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-react-native-localize

A toolbox for your React Native app localization.

Pending
Overview
Eval results
Files

language-matching.mddocs/

Language Matching

Intelligent language tag matching to find the best supported language from your app's available translations based on device locale preferences.

Capabilities

Find Best Language Tag

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);
    }
  }
}

Matching Algorithm

The findBestLanguageTag function uses a sophisticated matching algorithm:

  1. Exact Match: First tries to match the complete language tag (e.g., "en-US")
  2. Language-Script Match: Matches language with script code (e.g., "zh-Hans")
  3. Language-Country Match: Matches language with country (e.g., "en-US" matches "en-GB")
  4. Language-Only Match: Falls back to language code only (e.g., "en")

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

docs

device-preferences.md

device-settings.md

index.md

language-matching.md

number-currency-formatting.md

platform-utilities.md

tile.json