Skip to content

Function: fallbackChain() ​

fallbackChain(locale, fallbacks, defaultLocale): string[]

List the languages a message of a language is looked up in, in their order, each once: the language, then each of its configured fallbacks (followed by their own fallbacks and less specific tags), then its less specific tags (zh-Hant-TW, zh-Hant, zh), then the default language (and its own fallbacks and less specific tags). The tags are not normalized: give canonical ones.

Parameters ​

locale ​

string

The language tag of the message asked.

fallbacks ​

Record<string, string[]>

The configured fallbacks, by language tag.

defaultLocale ​

string

The default language tag.

Returns ​

string[]

The language tags, in lookup order.

Example ​

typescript
import {fallbackChain} from 'lakutata/com/i18n'

console.log(fallbackChain('zh-TW', {'zh-TW': ['zh-CN']}, 'en')) //['zh-TW', 'zh-CN', 'zh', 'en']
console.log(fallbackChain('zh-Hant-TW', {}, 'en')) //['zh-Hant-TW', 'zh-Hant', 'zh', 'en']

Released under the MIT License.