### File

`projects/angular-l10n/src/lib/services/l10n-translation-fallback.ts`

### Description

Implement this class-interface to create a translation fallback.

### Index

###### Methods

- Public Abstract get

### Methods

Public Abstract get
`get(language: string, provider: L10nProvider)`
Defined in projects/angular-l10n/src/lib/services/l10n-translation-fallback.ts:20

This method must contain the logic to get the ordered loaders.

Parameters :

Name | Type | Optional | Description
language | `string` | No | The current language
provider | `L10nProvider` | No | The provider of the translations data

Returns : `Observable[]`

An array of loaders

    import { Injectable, Inject } from '@angular/core';
    import { Observable } from 'rxjs';

    import { L10nProvider } from '../models/types';
    import { L10N_CONFIG, L10nConfig } from '../models/l10n-config';
    import { L10nCache } from './l10n-cache';
    import { L10nTranslationLoader } from './l10n-translation-loader';

    /**
     * Implement this class-interface to create a translation fallback.
     */
    @Injectable() export abstract class L10nTranslationFallback {

        /**
         * This method must contain the logic to get the ordered loaders.
         * @param language The current language
         * @param provider The provider of the translations data
         * @return An array of loaders
         */
        public abstract get(language: string, provider: L10nProvider): Observable<any>[];

    }

    @Injectable() export class L10nDefaultTranslationFallback implements L10nTranslationFallback {

        constructor(
            @Inject(L10N_CONFIG) private config: L10nConfig,
            private cache: L10nCache,
            private translationLoader: L10nTranslationLoader
        ) { }

        /**
         * Translation data will be merged in the following order:
         * 'language'
         * 'language[-script]'
         * 'language[-script][-region]'
         */
        public get(language: string, provider: L10nProvider): Observable<any>[] {
            const loaders: Observable<any>[] = [];
            const keywords = language.match(/-?[a-zA-z]+/g) || [];
            let fallbackLanguage = '';
            for (const keyword of keywords) {
                fallbackLanguage += keyword;
                if (this.config.cache) {
                    loaders.push(
                        this.cache.read(`${provider.name}-${fallbackLanguage}`,
                            this.translationLoader.get(fallbackLanguage, provider))
                    );
                } else {
                    loaders.push(this.translationLoader.get(fallbackLanguage, provider));
                }
            }
            return loaders;
        }

    }

# result-matching ""

# No results matching ""