UNPKG

@kwaeri/i18n

Version:

The internationalization component module of the @kwaeri platform.

119 lines (118 loc) 4.93 kB
/** * SPDX-PackageName: kwaeri/i18n * SPDX-PackageVersion: 0.5.0 * SPDX-FileCopyrightText: © 2014 - 2022 Richard Winters <kirvedx@gmail.com> and contributors * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR MIT */ export declare namespace kwaeri { namespace Internationalization { namespace Locale { type PlaceholderBits = { content: string; example?: string; }; type MessagePlaceholder = { [key: string]: PlaceholderBits; }; type MessageBits = { message: string; description?: string; placeholders?: MessagePlaceholder; }; type Messages = { [key: string]: MessageBits; }; type Map = { [key: string]: Messages; }; type ResultMap = { [key: string]: boolean; }; } class i18n { /** * The default locale */ defaultLocale?: string; /** * The default locale's base locale */ defaultLocaleBase?: string; /** * The currently configured locale */ locale?: string; /** * The currently configured locale's base locale. It may seem redundant, but * for a logical message resolution process, we search for a message from * the configured locale - should it not be found we'd then check the base * locale for a given locale (i.e. "en" for "en_US"), if it is still not found * (either because it was already a base locale, messages for a base locale * aren't provided for a locale), we'd finally check the default locale. * before we decide that a message translation just doesn't exist. */ localeBase?: string; /** * A [list of] provided locale(s) */ locales?: string[]; /** * A map of loaded messages per locale that has been provided */ map: Locale.Map; /** * A convenience flag that denotes whether translations have been * mapped or not. */ mapped: boolean; /** * IL8N constructor * * @param { string[] } locales A provided locale, or list of locales.Can be provided as individual arguments or as an array of strings. */ constructor(...locales: any[]); /** * Initializes IL8N; Sets default and configured locales and loads translations * * @returns { Promise<Locale.ResultMap|null> } A promise of a {@link Locale.ResultMap } or null */ init(): Promise<Locale.ResultMap | null>; /** * Method to fetch translations per locale provided * * @param { string[] } locales An array of strings, each a locale requested * * @returns {} */ loadLocales(locales: string[]): Promise<Locale.ResultMap>; /** * Returns the translation of a message for a configured locale. Up to 8 placeholder/template arguments may be passed in after the message name. * * @param { string } name The name of the internationalized message * @params Up to 8 placeholder/template values * * @returns { string } The requested message translated for a configured locale */ getMessage(name: string, ...placeholders: any[]): string; /** * Similar to {@link getMessage}, returns the translation of a message for a provided locale, allowing up to 8 placeholder/template arguments. * * Unlike {@link getMessage}, should a message not be found for the provided locale no additional searhing will be done. * * @param { string } name The name of the internationalized message * @param { string } locale The localization for the provided message * @params Up to 8 placeholder/template values * * @returns { string } The requested message for a provided locale. */ getLocalisedMessage(name: string, locale: string, ...placeholders: any[]): string; /** * Strips placeholders from a message where placeholder content is provided to replace them, * * @param { Locale.MessageBits } bits * @params placeholders */ rasterizeString(bits: Locale.MessageBits, ...placeholders: any[]): string; } } }