UNPKG

@mongez/localization

Version:

A simple i18n localization handler for browsers and nodejs

101 lines 3.4 kB
import { Converter, GroupedTranslations, Keywords, Translatable, TranslationsList } from "./types"; /** * Get current locale code */ export declare function getCurrentLocaleCode(): string; /** * Get fallback locale code */ export declare function getFallbackLocaleCode(): string; /** * Set current converter */ export declare function setConverter(converter: Converter): void; /** * Get the locale code used in translation, this allows to get the locale code on the fly */ export declare function getTranslationLocaleCode(): string; /** * Set current locale code */ export declare function setCurrentLocaleCode(localeCode: string): void; /** * Add keywords */ export declare function extend(localeCode: string, keywords: Keywords): void; /** * Create a grouped translations based on keyword, each keyword contains list of locale codes and beside it its corresponding translation * * @example * { * home: { * en: "Home", * ar: "الرئيسية" * } * } * * Also it could have nested grouped translations * * @example * { * general: { * home: { * en: "Home", * ar: "الرئيسية" * } * } */ export declare function groupedTranslations(groupKey?: string | GroupedTranslations, groupedTranslations?: GroupedTranslations): void; /** * Override the entire translations list */ export declare function setTranslationsList(translations: TranslationsList): void; /** * Get the entire translations list */ export declare function getTranslationsList(): TranslationsList; /** * Get the keywords list of the given locale code */ export declare function getKeywordsListOf(localeCode: string): Keywords | null; /** * Set fallback locale code, if the keyword does not exist on current locale code, * then check it in the faLLBACK locale code instead */ export declare function setFallbackLocaleCode(fallbackLocale: string): void; /** * Translate the given keyword in current locale code */ export declare function trans(keyword: Translatable, placeholders?: any, converter?: Converter): any; /** * Translate using the default converter */ export declare function plainTrans(keyword: string, placeholders?: any): any; /** * Translate the given keyword for the given locale code * Please note this method accepts dot notation syntax */ export declare function transFrom(localeCode: string, keyword: Translatable, placeholders?: any, converter?: Converter): any; export type WithPlaceholder<T> = { p: (keyword: keyof T, placeholders?: any) => string; plain: (keyword: keyof T, placeholders?: any) => string; }; /** * Get a translation object with automatic translation using object syntax * Please note this does not support nested objects, only keywords and their translations * i.e * const translations = transObject({ * name: { * en: 'name', * ar: 'الاسم' * } * }); * * Usage: translations.name // returns the name in current locale code * If the keyword does not exist on current locale code, then it will check it in the fallback locale code * * If keyword is "p", then it will return a function that accepts keyword and its placeholders * If keyword is "plain", then the converter used in translation will be the plain converter */ export declare function transObject<T extends Keywords>(translations: T): WithPlaceholder<T> & { [key in keyof T]: string; }; //# sourceMappingURL=translator.d.ts.map