UNPKG

angular-l10n

Version:

An Angular library to translate messages, dates and numbers

110 lines 3.51 kB
/** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ import { Injectable, Inject, Optional } from '@angular/core'; import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http'; import { timeout } from 'rxjs/operators'; import { Caching } from '../models/caching'; import { L10N_CONFIG } from "../models/l10n-config"; import { ProviderType } from '../models/types'; /** * Implement this class-interface to create a custom provider for translation data. * @abstract */ export class TranslationProvider { } TranslationProvider.decorators = [ { type: Injectable } ]; if (false) { /** * This method must contain the logic of data access. * @abstract * @param {?} language The current language * @param {?} args The object set during the configuration of 'providers' * @return {?} An observable of an object of translation data: {key: value} */ TranslationProvider.prototype.getTranslation = function (language, args) { }; } export class L10nTranslationProvider { /** * @param {?} http * @param {?} configuration * @param {?} caching */ constructor(http, configuration, caching) { this.http = http; this.configuration = configuration; this.caching = caching; this.headers = new HttpHeaders({ 'Content-Type': 'application/json' }); this.options = {}; } /** * @param {?} language * @param {?} args * @return {?} */ getTranslation(language, args) { this.setOptions(); /** @type {?} */ let url = ""; switch (args.type) { case ProviderType.WebAPI: url += args.path + language; break; default: url += args.prefix + language + ".json"; } return this.getRequest(url); } /** * @return {?} */ setOptions() { this.options = { headers: this.headers, params: this.configuration.translation.version ? new HttpParams().set('ver', this.configuration.translation.version) : undefined }; } /** * @param {?} url * @return {?} */ getRequest(url) { /** @type {?} */ let request; if (this.configuration.translation.timeout) { request = this.http.get(url, this.options).pipe(timeout(this.configuration.translation.timeout)); } else { request = this.http.get(url, this.options); } if (this.configuration.translation.caching) { return this.caching.read(url, request); } return request; } } L10nTranslationProvider.decorators = [ { type: Injectable } ]; /** @nocollapse */ L10nTranslationProvider.ctorParameters = () => [ { type: HttpClient, decorators: [{ type: Optional }] }, { type: undefined, decorators: [{ type: Inject, args: [L10N_CONFIG,] }] }, { type: Caching } ]; if (false) { /** @type {?} */ L10nTranslationProvider.prototype.headers; /** @type {?} */ L10nTranslationProvider.prototype.options; /** @type {?} */ L10nTranslationProvider.prototype.http; /** @type {?} */ L10nTranslationProvider.prototype.configuration; /** @type {?} */ L10nTranslationProvider.prototype.caching; } //# sourceMappingURL=translation-provider.js.map