UNPKG

@ngx-translate/http-loader

Version:

http loader for dynamically loading translation files for @ngx-translate/core

93 lines (89 loc) 3.66 kB
import { HttpClient, HttpBackend } from '@angular/common/http'; import * as i0 from '@angular/core'; import { InjectionToken, inject, Injectable } from '@angular/core'; import { mergeDeep, TranslateLoader } from '@ngx-translate/core'; import { catchError, of, forkJoin, map } from 'rxjs'; const TRANSLATE_HTTP_LOADER_CONFIG = new InjectionToken("TRANSLATE_HTTP_LOADER_CONFIG"); class TranslateHttpLoader { http; config; constructor() { this.config = { resources: [], enforceLoading: false, useHttpBackend: false, ...inject(TRANSLATE_HTTP_LOADER_CONFIG), }; this.http = this.config.useHttpBackend ? new HttpClient(inject(HttpBackend)) : inject(HttpClient); } /** * Gets the translations from the server */ getTranslation(lang) { const cacheBuster = this.config.enforceLoading ? `?enforceLoading=${Date.now()}` : ""; const requests = this.config.resources.map((resource) => { const path = typeof resource === "string" ? `${resource}${lang}.json` : `${resource.prefix}${lang}${resource.suffix ?? ".json"}`; const request$ = this.http.get(`${path}${cacheBuster}`); if (this.config.failOnError) { return request$; } return request$.pipe(catchError((err) => { console.warn(`@ngx-translate/http-loader: error loading translation for ${lang}:`, err); return of({}); })); }); if (requests.length === 0) { return of({}); } return forkJoin(requests).pipe(map((response) => response.reduce((acc, curr) => mergeDeep(acc, curr), {}))); } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.0", ngImport: i0, type: TranslateHttpLoader, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.0.0", ngImport: i0, type: TranslateHttpLoader }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.0", ngImport: i0, type: TranslateHttpLoader, decorators: [{ type: Injectable }], ctorParameters: () => [] }); function provideTranslateHttpLoader(config = {}) { // If config already has resources, it's a multi-config, pass it through if ("resources" in config && config.resources) { return provideTranslateMultiHttpLoader(config); } // Otherwise, convert single config to multi-config const singleConfig = config; const multiConfig = { enforceLoading: singleConfig.enforceLoading ?? false, useHttpBackend: singleConfig.useHttpBackend ?? false, failOnError: singleConfig.failOnError ?? false, resources: [ { prefix: singleConfig.prefix ?? "/assets/i18n/", suffix: singleConfig.suffix ?? ".json", }, ], }; return provideTranslateMultiHttpLoader(multiConfig); } function provideTranslateMultiHttpLoader(config = {}) { return [ { provide: TRANSLATE_HTTP_LOADER_CONFIG, useValue: { resources: ["/assets/i18n/"], ...config, }, }, { provide: TranslateLoader, useClass: TranslateHttpLoader, }, ]; } /** * Generated bundle index. Do not edit. */ export { TRANSLATE_HTTP_LOADER_CONFIG, TranslateHttpLoader, provideTranslateHttpLoader, provideTranslateMultiHttpLoader }; //# sourceMappingURL=ngx-translate-http-loader.mjs.map