Function getAvailableLanguage

  • Checks if the user's locale matches the same language with a different region translated by the app

    Parameters

    • userLocale: string

      The user locale tested against

    • appLocales: string[]

      The locales translated by the app

    • Optional options: PreferredLocaleOptions = {}

      Options for the function

    Returns {
        appLocale: string;
        isAvailable: boolean;
    }

    The user's locale if it matches the app's locale, the app's locale if the languages match but region is or false if no matches

    • appLocale: string
    • isAvailable: boolean

    Example

    import { getAvailableLanguage } from 'preferred-locale'

    getAvailableLanguage('en-US', ['en-US', 'en-GB']) // { isAvailable: true, appLocale: 'en-US' }

    getAvailableLanguage('en-GB', ['en-US']) // { isAvailable: true, appLocale: 'en-US' }

    getAvailableLanguage('es-ES', ['en-US']) // { isAvailable: false, appLocale: 'en-ES' }