Options
All
  • Public
  • Public/Protected
  • All
Menu

Hierarchy

  • LfLocalizationService

Implements

Index

Constructors

  • example
    const resource = new Map([
    ['jp', { "LOADING": "読み込み中..." }],
    ['ir', { "LOADING": "ag lódáil..." }],
    ['en', { "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

    Parameters

    • Optional resources: Map<string, object>

    Returns LfLocalizationService

Properties

debugMode: boolean = false

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

example
const resource = new Map([
['jp', { "LOADING": "読み込み中..." }],
['ir', { "LOADING": "ag lódáil..." }],
['en', { "LOADING": "Loading..." }]
]);
localizationService = new LfLocalizationService(resource);
localizationService.debugMode = true;
localizationService.getString('LOADING'); // '_ŀǿȧḓīƞɠ..._'

Accessors

  • Returns the current language resource in use

    example
    const resource = new Map([
    ['jp', { "LOADING": "読み込み中..." }],
    ['ir', { "LOADING": "ag lódáil..." }],
    ['en', { "LOADING": "Loading..." }]
    ]);
    localizationService = new LfLocalizationService(resource);
    localizationService.currentResource // {'language': 'en', 'resource':{ "LOADING": "Loading..." }}

    Returns undefined | resourceType

Methods

  • getString(key: string, params?: string[]): string
  • 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

    example
    const resource = new Map([
    ['jp', { "LOADING": "読み込み中..." }],
    ['ir', { "LOADING": "ag lódáil..." }],
    ['en', { "LOADING": "Loading..." }]
    ]);
    localizationService = new LfLocalizationService(resource);
    localizationService.getString('LOADING'); // 'Loading...'
    localizationService.setLanguage('ir');
    localizationService.getString('LOADING'); // 'ag lódáil...'

    Parameters

    • key: string

      the string to translate

    • Optional params: string[]

      the tokens to replace in translated string if exist

    Returns string

    the translated string with tokens

  • initResourcesFromUrlAsync(url: string): Promise<void>
  • 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)

    example
    const localizationService = new LfLocalizationService();
    const resourcesFolder = 'https://cdn.jsdelivr.net/npm/@laserfiche/lf-resource-library@2.0.0/resources/laserfiche-base';
    localizationService.setLanguage('fr-CA');
    await localizationService.initResourcesFromUrlAsync(resourcesFolder); // loads en.json and fr.json

    Parameters

    • url: string

      the url to the language file's folder

    Returns Promise<void>

  • setLanguage(language: string): void
  • Sets currentResource given the language code, fall back to available resource if necessary.

    example
    const localizationService = new LfLocalizationService();
    const resourcesFolder = 'https://cdn.jsdelivr.net/npm/@laserfiche/lf-resource-library@2.0.0/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': { ... }}
    example
    const resource = new Map([
    ['jp', { "LOADING": "読み込み中..." }],
    ['ir', { "LOADING": "ag lódáil..." }],
    ['en', { "LOADING": "Loading..." }]
    ]);
    localizationService = new LfLocalizationService(resource);
    localizationService.setLanguage('ir'); // set currentResource to ir
    localizationService.currentLanguage // {'language': 'ir', 'resource':{ "LOADING": "ag lódáil..." }}

    Parameters

    • language: string

    Returns void

Generated using TypeDoc