import Decimal from 'decimal.js'; import type { FSTranslationKeys, I18n, MappedCurrencySymbols, NumberLike, TranslationKey, TranslationKeys, Translations } from './types'; export interface ICurrencyValue { value: Decimal; currencyCode: string; } export default class I18nHelper { protected readonly i18n: I18n; constructor(i18n: I18n); protected localeListeners: Array<(locale: string) => void>; /** * Converts a given number-like value into an actual number * * @param num - number-like value to be converted * @return - Converted number */ protected convertToNumber(num: NumberLike): number; /** * Lets you override the currency symbol by providing mappedCurrencyCodes:{} * in the env file. Ex: mappedCurrencyCodes: { AUD: 'A$'}, * * @param num - Number to be formatted * @param [currency] - ISO 4217 code for the currency you're trying to change * @return - Formatted number */ protected currencyWithCustomSymbol(num: ICurrencyValue | NumberLike, options: Intl.NumberFormatOptions): string; /** * Recursively creates a new object matching the structure of the currently set * translations with each keys' value replaced by its object path. This object can be * used to reference the intended translation key with auto-completion and type checking. * * @param translations - Translations keys to flatten * @param parentTranslations - New object containing the object paths * @param paths - Paths to the current translation keys object from the root level * @return - New object containing the object paths */ protected flatten(translations: TranslationKeys, parentTranslations?: TranslationKeys, paths?: string[]): TranslationKeys; /** * Determines if a given value is a translation key * * @param translationKey - Value to check if it is a translation key * @param translationKey.zero * @param translationKey.one * @param translationKey.other * @return - Whether or not the value is a translation key */ protected isTranslationKey(translationKey: TranslationKey | object | undefined): translationKey is TranslationKey; /** * Recursively merges new string translations into any existing * * @param translations - A locale-indexed object containing * key/value pairs of string translations * @return - All possible string translations */ addTranslations(translations: Translations): FSTranslationKeys & T; /** * Recursively merges new currency symbol overrides into any existing * * @param mappedCurrencyCodes - A locale-indexed object containing * key/value pairs of string translations * @return - All possible string translations * @example FSI18n.addCurrencyOverrides({AUD: 'A$'}); */ addCurrencyOverrides(mappedCurrencyCodes: MappedCurrencySymbols): FSTranslationKeys & T; /** * Change language * * @param locale - Set locale */ setLocale(locale: string): void; addLocaleListener(func: (locale: string) => void): void; removeLocaleListener(func: (locale: string) => void): void; /** * Creates an object matching the same structure as the translation definitions * object, but with the string translation values replaced by their object path * * Used for code completion. * * @return Available string translations * @throws MISSING_TRANSLATIONS_ERROR */ getAvailableStringTranslations(): T; /** * Creates an object matching the same structure as the translation definitions * object, but with the string translation values replaced by their object path * * Used for code completion. * * @return Available string translations * @throws MISSING_TRANSLATIONS_ERROR */ getAvailableCurrencySymbolOverrides(): T; /** * Converts a number-like value into a locale-formatted string * * @param num - Number to be formatted * @param [options] - Options to control how the number is formatted * @return - Formatted number * @example FSI18n.number(1234); // returns "1,234" */ number(num: NumberLike, options?: Intl.NumberFormatOptions): string; /** * Converts a number-like value into a locale-formatted currency string * * @param num - Number to be formatted * @param [currency] - ISO 4217 currency code * @param [options] - Options to control how the number is formatted * @return - Formatted number * @example FSI18n.currency(1.23, 'USD'); // returns "$1.23" */ currency(num: ICurrencyValue | NumberLike, currency?: string, options?: Intl.NumberFormatOptions): string; /** * Converts a number-like value into a locale-formatted percent string * * @param num - Number to be formatted * @param [options] - Options to control how the number is formatted * @return - Formatted number * @example FSI18n.percent(.12); // returns "12%" */ percent(num: NumberLike, options?: Intl.NumberFormatOptions): string; /** * Converts a date object into a locale-formatted string * * @param date - Date object to be formatted * @param [options] - Options to control how the date is formatted * @return - Formatted date string * @example FSI18n.date(new Date()); // returns "6/13/2018" */ date(date: Date, options?: Intl.DateTimeFormatOptions): string; /** * Returns the language appropriate for a given translationKey * * @param translationKey - Path to the appropriate key in a translation object * @param [options] - Optional options object to * allow for interpolation and/or pluralization * @return - Localized string */ string(translationKey?: TranslationKey, options?: I18n.TranslateOptions): string; /** * Returns the current Locale * * @return Current locale */ currentLocale(): string; }