@fabioguelfi/angular-translate
Version:
A lightweight internationalization library for Angular applications
568 lines (556 loc) • 17.4 kB
JavaScript
import { InjectionToken, Inject, Injectable, Directive, ElementRef, Input, Pipe, NgModule } from '@angular/core';
import { HttpClient, HttpClientModule } from '@angular/common/http';
import { __assign } from 'tslib';
import { BehaviorSubject, Subject, of, from, combineLatest } from 'rxjs';
import { filter, switchMap, switchMapTo, take, takeUntil } from 'rxjs/operators';
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
var /** @type {?} */ CONFIG = new InjectionToken('config');
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
var LoaderService = /** @class */ (function () {
function LoaderService(http, config) {
this.http = http;
this.config = config;
this.path = '/assets/languages/';
this.extension = '.json';
this.path = config.path ? config.path : this.path;
}
/**
* @param {?} fileName
* @return {?}
*/
LoaderService.prototype.getFile = /**
* @param {?} fileName
* @return {?}
*/
function (fileName) {
return this.http.get(this.path + fileName + this.extension);
};
LoaderService.decorators = [
{ type: Injectable }
];
/** @nocollapse */
LoaderService.ctorParameters = function () { return [
{ type: HttpClient, },
{ type: undefined, decorators: [{ type: Inject, args: [CONFIG,] },] },
]; };
return LoaderService;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
/** @enum {string} */
var CONSTANTS = {
EXIT: 'EXIT',
};
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
var TranslateService = /** @class */ (function () {
function TranslateService(http, config) {
this.http = http;
this.config = config;
this.translations = {};
this.translationsLoaded = new BehaviorSubject(false);
this.matcher = function (key) { return new RegExp('{{\\s?[\\b' + key + '\\b]*\\s?}}', 'gm'); };
this.loaderService = new LoaderService(http, config);
}
/**
* @return {?}
*/
TranslateService.prototype.getBrowserLanguage = /**
* @return {?}
*/
function () {
var /** @type {?} */ browserLanguage = window.navigator.languages
? window.navigator.languages[0]
: window.navigator.language;
if (browserLanguage.indexOf('-') !== -1) {
browserLanguage = browserLanguage.split('-')[0];
}
if (browserLanguage.indexOf('_') !== -1) {
browserLanguage = browserLanguage.split('_')[0];
}
return browserLanguage;
};
/**
* @param {?} fileName
* @return {?}
*/
TranslateService.prototype.setDefault = /**
* @param {?} fileName
* @return {?}
*/
function (fileName) {
var _this = this;
this.defaultKey = fileName;
this.defaultPrefix = fileName.split('-')[0];
if (this.translations[this.defaultKey]) {
this.translationsLoaded.next(true);
}
else {
this.translationsLoaded.next(false);
this.loaderService.getFile(fileName)
.subscribe(function (translations) {
_this.translations[_this.defaultKey] = translations;
_this.translationsLoaded.next(true);
});
}
};
/**
* @param {?} fileName
* @return {?}
*/
TranslateService.prototype.setOverride = /**
* @param {?} fileName
* @return {?}
*/
function (fileName) {
var _this = this;
this.overrideKey = fileName;
if (this.translations[this.overrideKey]) {
this.translationsLoaded.next(true);
}
else {
this.loaderService.getFile(fileName)
.subscribe(function (translations) {
_this.translations[_this.overrideKey] = translations;
_this.translationsLoaded.next(true);
});
}
};
/**
* @param {?} keyPaths
* @return {?}
*/
TranslateService.prototype.get = /**
* @param {?} keyPaths
* @return {?}
*/
function (keyPaths) {
return this.translationsLoaded.pipe(filter(Boolean), switchMapTo(keyPaths instanceof Array
? this.getAll(keyPaths)
: this.getOne(keyPaths)));
};
/**
* @param {?} keyPaths
* @param {?} fileName
* @return {?}
*/
TranslateService.prototype.getByFileName = /**
* @param {?} keyPaths
* @param {?} fileName
* @return {?}
*/
function (keyPaths, fileName) {
var _this = this;
var /** @type {?} */ translationLoaded = new Subject();
var /** @type {?} */ defaultFileName = this.defaultPrefix + "-" + fileName.split('-')[1];
this.loaderService.getFile(fileName).pipe(function (file) { return combineLatest(file, _this.loaderService.getFile(defaultFileName)); }, filter(function (_a) {
var file = _a[0], defaultFile = _a[1];
return [file, defaultFile].indexOf(undefined) === -1;
}), take(1)).subscribe(function (_a) {
var translations = _a[0], defaultTranslations = _a[1];
_this.translations[fileName] = translations;
_this.translations[defaultFileName] = defaultTranslations;
translationLoaded.next(fileName);
}, function () {
_this.loaderService.getFile(defaultFileName)
.pipe(take(1))
.subscribe(function (translations) {
_this.translations[defaultFileName] = translations;
translationLoaded.next(defaultFileName);
});
});
return translationLoaded.pipe(switchMap(function (overrideFileName) {
return keyPaths instanceof Array
? _this.getAll(keyPaths, overrideFileName, defaultFileName)
: _this.getOne(keyPaths, overrideFileName, defaultFileName);
}));
};
/**
* @param {?} keyPath
* @param {?=} fileName
* @param {?=} defaultKey
* @return {?}
*/
TranslateService.prototype.getOne = /**
* @param {?} keyPath
* @param {?=} fileName
* @param {?=} defaultKey
* @return {?}
*/
function (keyPath, fileName, defaultKey) {
if (fileName === void 0) { fileName = this.overrideKey; }
if (defaultKey === void 0) { defaultKey = this.defaultKey; }
return from([this.read(keyPath, {}, fileName, defaultKey)]);
};
/**
* @param {?} keyPaths
* @param {?=} fileName
* @param {?=} defaultKey
* @return {?}
*/
TranslateService.prototype.getAll = /**
* @param {?} keyPaths
* @param {?=} fileName
* @param {?=} defaultKey
* @return {?}
*/
function (keyPaths, fileName, defaultKey) {
var _this = this;
if (fileName === void 0) { fileName = this.overrideKey; }
if (defaultKey === void 0) { defaultKey = this.defaultKey; }
return of(keyPaths.reduce(function (acc, keyPath) {
return (__assign({}, acc, (_a = {}, _a[keyPath] = _this.read(keyPath, {}, fileName, defaultKey), _a)));
var _a;
}, {}));
};
/**
* @param {?} keyPath
* @param {?=} params
* @param {?=} overrideKey
* @param {?=} defaultKey
* @return {?}
*/
TranslateService.prototype.read = /**
* @param {?} keyPath
* @param {?=} params
* @param {?=} overrideKey
* @param {?=} defaultKey
* @return {?}
*/
function (keyPath, params, overrideKey, defaultKey) {
var _this = this;
if (params === void 0) { params = {}; }
if (overrideKey === void 0) { overrideKey = this.overrideKey; }
if (defaultKey === void 0) { defaultKey = this.defaultKey; }
var /** @type {?} */ value = CONSTANTS.EXIT;
var /** @type {?} */ path = keyPath.split('.');
if (this.translations[overrideKey]) {
value = this.readValue(path, this.translations[overrideKey]);
if (value === CONSTANTS.EXIT) {
value = this.readValue(path, this.translations[defaultKey]);
}
}
else if (this.translations[defaultKey]) {
value = this.readValue(path, this.translations[defaultKey]);
}
if (Boolean(params) && params !== {}) {
value = Object.keys(params)
.reduce(function (final, key) { return final.replace(_this.matcher(key), params[key]); }, value);
}
if (value === CONSTANTS.EXIT) {
console.warn('Unknown Key: ', keyPath);
return keyPath;
}
return value;
};
/**
* @param {?} path
* @param {?} translation
* @return {?}
*/
TranslateService.prototype.readValue = /**
* @param {?} path
* @param {?} translation
* @return {?}
*/
function (path, translation) {
var /** @type {?} */ length = path.length;
for (var /** @type {?} */ i = 0; i < length; i++) {
translation = translation && translation[path[i]] ? translation[path[i]] : CONSTANTS.EXIT;
if (translation === CONSTANTS.EXIT) {
break;
}
}
return translation;
};
/**
* @param {?} a
* @param {?} b
* @return {?}
*/
TranslateService.prototype.isEquivalent = /**
* @param {?} a
* @param {?} b
* @return {?}
*/
function (a, b) {
if (!Boolean(a) || !Boolean(b)) {
return false;
}
var /** @type {?} */ aProps = Object.getOwnPropertyNames(a);
var /** @type {?} */ bProps = Object.getOwnPropertyNames(b);
if (aProps.length !== bProps.length) {
return false;
}
for (var /** @type {?} */ i = 0; i < aProps.length; i++) {
var /** @type {?} */ propName = aProps[i];
if (a[propName] !== b[propName]) {
return false;
}
}
return true;
};
TranslateService.decorators = [
{ type: Injectable }
];
/** @nocollapse */
TranslateService.ctorParameters = function () { return [
{ type: HttpClient, },
{ type: undefined, decorators: [{ type: Inject, args: [CONFIG,] },] },
]; };
return TranslateService;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
var TranslateDirective = /** @class */ (function () {
function TranslateDirective(element, translateService) {
this.element = element;
this.translateService = translateService;
this.unsubscribe = new Subject();
this.translationLoaded$ = this.translateService.translationsLoaded
.pipe(takeUntil(this.unsubscribe));
}
Object.defineProperty(TranslateDirective.prototype, "params", {
set: /**
* @param {?} params
* @return {?}
*/
function (params) {
if (!this.translateService.isEquivalent(this.translateParams, params)) {
this.translateParams = params;
if (this.translateParams) {
this.runOneCheck(this.keyPath, this.translateParams);
}
}
},
enumerable: true,
configurable: true
});
/**
* @return {?}
*/
TranslateDirective.prototype.ngAfterViewInit = /**
* @return {?}
*/
function () {
this.keyPath = this.element.nativeElement.textContent ? this.element.nativeElement.textContent.trim() : '';
this.element.nativeElement.textContent = '';
this.registerKeyChecker(this.keyPath);
};
/**
* @return {?}
*/
TranslateDirective.prototype.ngOnDestroy = /**
* @return {?}
*/
function () {
this.unsubscribe.next();
this.unsubscribe.complete();
};
/**
* @param {?} keyPath
* @return {?}
*/
TranslateDirective.prototype.registerKeyChecker = /**
* @param {?} keyPath
* @return {?}
*/
function (keyPath) {
var _this = this;
this.translationLoaded$
.subscribe(function (isLoaded) { return _this.doCheck(isLoaded, keyPath, _this.translateParams); });
};
/**
* @param {?} keyPath
* @param {?} params
* @return {?}
*/
TranslateDirective.prototype.runOneCheck = /**
* @param {?} keyPath
* @param {?} params
* @return {?}
*/
function (keyPath, params) {
var _this = this;
if (keyPath) {
this.translationLoaded$
.pipe(take(1))
.subscribe(function (isLoaded) { return _this.doCheck(isLoaded, keyPath, params); });
}
};
/**
* @param {?} isLoaded
* @param {?} keyPath
* @param {?} params
* @return {?}
*/
TranslateDirective.prototype.doCheck = /**
* @param {?} isLoaded
* @param {?} keyPath
* @param {?} params
* @return {?}
*/
function (isLoaded, keyPath, params) {
if (isLoaded) {
var /** @type {?} */ readValue = keyPath !== '' ? this.translateService.read(keyPath, params) : '';
this.element.nativeElement.textContent = readValue === keyPath ? '' : readValue;
}
};
TranslateDirective.decorators = [
{ type: Directive, args: [{
selector: '[translate]'
},] }
];
/** @nocollapse */
TranslateDirective.ctorParameters = function () { return [
{ type: ElementRef, },
{ type: TranslateService, },
]; };
TranslateDirective.propDecorators = {
"params": [{ type: Input, args: ['translate',] },],
};
return TranslateDirective;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
var TranslatePipe = /** @class */ (function () {
function TranslatePipe(translateService) {
this.translateService = translateService;
this.translation = '';
this.unsubscribe = new Subject();
this.translationLoaded$ = translateService.translationsLoaded.pipe(filter(Boolean), takeUntil(this.unsubscribe));
}
/**
* @param {?} val
* @param {?} args
* @return {?}
*/
TranslatePipe.prototype.transform = /**
* @param {?} val
* @param {?} args
* @return {?}
*/
function (val, args) {
var _this = this;
this.translationLoaded$.subscribe(function () {
var /** @type {?} */ readValue = val ? _this.translateService.read(val, args) : '';
_this.translation = readValue === val ? _this.translation : readValue;
});
return this.translation;
};
/**
* @return {?}
*/
TranslatePipe.prototype.ngOnDestroy = /**
* @return {?}
*/
function () {
this.unsubscribe.next();
this.unsubscribe.complete();
};
TranslatePipe.decorators = [
{ type: Pipe, args: [{
name: 'translate',
pure: false
},] }
];
/** @nocollapse */
TranslatePipe.ctorParameters = function () { return [
{ type: TranslateService, },
]; };
return TranslatePipe;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
var TranslateModule = /** @class */ (function () {
function TranslateModule() {
}
/**
* @param {?=} config
* @return {?}
*/
TranslateModule.forRoot = /**
* @param {?=} config
* @return {?}
*/
function (config) {
if (config === void 0) { config = {}; }
return {
ngModule: TranslateModule,
providers: [
TranslateService,
LoaderService,
{ provide: CONFIG, useValue: config }
]
};
};
/**
* @return {?}
*/
TranslateModule.forChild = /**
* @return {?}
*/
function () {
return {
ngModule: TranslateModule
};
};
TranslateModule.decorators = [
{ type: NgModule, args: [{
imports: [HttpClientModule],
declarations: [
TranslateDirective,
TranslatePipe
],
exports: [
TranslateDirective,
TranslatePipe
]
},] }
];
return TranslateModule;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
var /** @type {?} */ translations = {
'default-en': {
'BODY': {
'KNOWN_KEY': 'Known key example'
}
}
};
var /** @type {?} */ LoaderServiceMock = {
path: '/assets/language',
extension: '.json',
http: null,
config: null,
getFile: function (key) { return of(translations[key]); }
};
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
export { TranslateModule, TranslateService, TranslateDirective, TranslatePipe, LoaderService, LoaderServiceMock, CONFIG as ɵa };
//# sourceMappingURL=angular-translate.js.map