Nagging Penguin Matriarchs

    @selfage/closest_locale_finder
    TypeScript icon, indicating that this package has built-in type declarations

    1.0.1 • Public • Published

    @selfage/closest_locale_finder

    Install

    npm install @selfage/closest_locale_finder

    Overview

    Written in TypeScript and compiled to ES6 with inline source map & source. See @selfage/tsconfig for full compiler options. Provides helper functions to find the closest locale out of a list of locales you want to support.

    Lookup algorithm is based on lookup-closest-locale.

    E.g., given a list of supported locales, ['en', 'en-US'], if you look for en-GB, the closest matching locale is en.

    Find from a map of localized texts

    type Delimiter = "-" | "_";
    function findClosestLocalizedText<T>(targetLocales: Array<string> /* From most preferred to least preferred. */, localizedTexts: Map<string, T>, defaultText: T, delimiter: Delimiter = '-'): T;
    import { findClosestLocalizedText } from '@selfage/closest_locale_finder';
    
    let text = findClosestLocalizedText(
      [navigator.language],
      new Map([
        ['en-US', {'welcome': 'Hello'}],
        ['zh-CN', {'welcome': '欢迎'}]
      ]),
      {'welcome': 'Hello'});
    console.log(text.welcome);

    Find from a set of available locales

    type Delimiter = "-" | "_";
    function findClosestLocale(targetLocales: Array<string> /* From most preferred to least preferred. */, availableLocales: Set<string>, defaultLocale: string, delimiter: Delimiter  = '-'): string;
    import { findClosestLocale } from '@selfage/closest_locale_finder';
    
    let locale = findClosestLocale(
      [navigator.language],
      new Set(['en-US', 'zh-CN']),
      'en-US');
    console.log(locale);

    Customize finding

    type Delimiter = "-" | "_";
    function forEachLocaleCandidate<T>(targetLocales: Array<string> /* From most preferred to least preferred. */, tryMatch: (localeCandidate: string) => {
        matched: boolean;
        res: T;
    }, defaultValue: T, delimiter: Delimiter = '-'): T;

    forEachLocaleCandidate is used internally by both findClosestLocalizedText and findClosestLocale, which match localeCandidate from the map and set respectively.

    Install

    npm i @selfage/closest_locale_finder

    DownloadsWeekly Downloads

    1

    Version

    1.0.1

    License

    GPL-3.0-or-later

    Unpacked Size

    44 kB

    Total Files

    5

    Last publish

    Collaborators

    • teststaybaka