UNPKG

@singleton-i18n/angular-client

Version:

Singleton client code for Angular 10.

89 lines (88 loc) 3.65 kB
import { VIPConfig } from '../config'; import { LocaleService } from './locale.service'; import { MessageFormat } from '../formatters/message.formatter'; import { BaseService } from './base.service'; import { VIPService } from './vip.service'; import { I18nContext } from './i18n.context'; export interface L10NKey { rawKey: string; nameSpace: string; } export declare type sourceBundleObject = { [key: string]: any; }; export declare class L10nService extends BaseService { protected vipService: VIPService; protected localeService: LocaleService; private messageFormat; private i18nContext; constructor(vipService: VIPService, localeService: LocaleService, messageFormat: MessageFormat, i18nContext: I18nContext); /** * Generate long key with namespace. * @param config * @param key raw key */ getLongKey(config: VIPConfig, key: string): string; /** * In the mutiple component situation, the namespace is necessary. * Default workspace the main component in root scope and each lazy scope. * @param key raw key with namespace */ private parseKey; /** * If translation is certainly be loaded before application bootstrap, the locale can be * skipped, otherwise make sure getting available locale from the return of stream API. * @param nameSpace * @param locale */ resolveLocaleData(nameSpace: string, locale?: string): any; /** * Register source bundles to the main configuration on demand. * For the isolated module (separated lib), the namespace is required. * Considering using the last item of the array for the configuration or new API instead. * @param args Source bundle objects from Angular component. */ registerSourceBundles(...args: sourceBundleObject[]): void; /** * Get source string via key with namespace * @param key raw key with namespace */ getSourceString(key: string): string; /** * Determine whether the key already exists in the sourceBundles or translation. * if the locale is source locale, check whether the key exists in sourceBundles. * if the locale is not source locale, check whether the key exists in translation. * @param key raw key with namespace * @param locale */ isExistKey(key: string, locale?: string): boolean; private formatMessage; /** * @param key raw key with namespace * @param source source string for translation * @param args variables for placeholders * @param locale work with steam API */ translate(key: string, source: string, args?: any[] | {}, locale?: string): string; /** * @param key raw key with namespace * @param args variables and comment * @param locale optinal parameter for live update through 'stream' API */ getMessage(key: string, args?: any[] | {}, locale?: string): string; /** * This API is designed to generate scoped translate method for the isolated module. * The key with namespace which is generated by the configuration. * @param config The VIP configuration with product name, component name and version. */ getScopedTranslate(config: VIPConfig): Function; /** * getSplitedMessage * get message array splited by the seperator * @param key raw key with namespace * @param args variables * @param locale optinal parameter for live update through 'stream' API * @param seperator is a regular expressions, default value is /##\d+/ */ getSplitedMessage(key: string, args?: any[], locale?: string, seperator?: RegExp): string[]; }