Implements

Constructors

  • Parameters

    • Optionalresources: Map<string, object>

    Returns LfJsUtils.LfLocalizationService

    const resource = new Map([
    ['jp-JP', { "LOADING": "読み込み中..." }],
    ['en-US', { "LOADING": "Loading..." }] // have to provide 'en' in map
    ]);
    const localizationService = new LfLocalizationService(resource);

    // or

    const localizationService = new LfLocalizationService();
    // have to call initResourcesFromUrlAsync later to load resources dynamically

Properties

debugMode: boolean

When true, returns pseudo language string. Defaults to false.

const resource = new Map([
['jp-JP', { "LOADING": "読み込み中..." }],
['en-US', { "LOADING": "Loading..." }]
]);
localizationService = new LfLocalizationService(resource);
localizationService.debugMode = true;
localizationService.getString('LOADING'); // '_ŀǿȧḓīƞɠ..._'

Accessors

  • get currentResource(): undefined | LfJsUtils.resourceType
  • Returns the current language resource in use

    Returns undefined | LfJsUtils.resourceType

    const resource = new Map([
    ['jp', { "LOADING": "読み込み中..." }],
    ['en', { "LOADING": "Loading..." }]
    ]);
    localizationService = new LfLocalizationService(resource);
    localizationService.currentResource // {'language': 'en', 'resource':{ "LOADING": "Loading..." }}

Methods

  • Returns the translated formatted string if exists, falls back to default resource if translated string doesn't exist in current resource throws an error if current resource does not exist

    Parameters

    • key: string

      the string to translate

    • Optionalparams: string[]

      the tokens to replace in translated string if exist

    Returns string

    the translated string with tokens

    const resource = new Map([
    ['jp-JP', { "LOADING": "読み込み中..." }],
    ['en-US', { "LOADING": "Loading..." }]
    ]);
    localizationService = new LfLocalizationService(resource);
    localizationService.getString('LOADING'); // 'Loading...'
    localizationService.setLanguage('jp-JP');
    localizationService.getString('LOADING'); // '読み込み中...'
  • Resets the resource map to include remote language resource files: en by default, and the closest selected language (e.g.: if selected language is fr-CA, and fr-CA doesn't exists but fr exists, it loads fr)

    Parameters

    • url: string

      the url to the language file's folder

    Returns Promise<void>

    const localizationService = new LfLocalizationService();
    const resourcesFolder = 'https://lfxstatic.com/npm/@laserfiche/lf-resource-library@4/resources/laserfiche-base';
    localizationService.setLanguage('fr-CA');
    await localizationService.initResourcesFromUrlAsync(resourcesFolder); // loads en-US.json and fr-FR.json
  • Sets currentResource given the language code, fall back to available resource if necessary.

    Parameters

    • language: string

    Returns void

    const localizationService = new LfLocalizationService();
    const resourcesFolder = 'https://lfxstatic.com/npm/@laserfiche/lf-resource-library@4/resources/laserfiche-base';
    localizationService.setLanguage('fr-CA'); // gives a warning since no resource exists at this point
    await localizationService.initResourcesFromUrlAsync(resourcesFolder); // loads en.json and fr.json because language has been set to be fr-CA
    localizationService.currentLanguage // {'language': 'fr', 'resource': { ... }}
    const resource = new Map([
    ['jp-JP', { "LOADING": "読み込み中..." }],
    ['en-US', { "LOADING": "Loading..." }]
    ]);
    localizationService = new LfLocalizationService(resource);
    localizationService.setLanguage('jp-JP'); // set currentResource to ir
    localizationService.currentLanguage // {'language': 'jp-JP', 'resource':{ "LOADING": "読み込み中..." }}