angular-l10n
Version:
An Angular library to translate messages, dates and numbers
102 lines • 2.7 kB
JavaScript
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
import { Directive, ElementRef, Input, Renderer2 } from '@angular/core';
import { takeUntil } from 'rxjs/operators';
import { TranslationService } from '../services/translation.service';
import { BaseDirective } from '../models/base-directive';
export class TranslateDirective extends BaseDirective {
/**
* @param {?} translation
* @param {?} el
* @param {?} renderer
*/
constructor(translation, el, renderer) {
super(el, renderer);
this.translation = translation;
this.el = el;
this.renderer = renderer;
}
/**
* @param {?} params
* @return {?}
*/
set l10nTranslate(params) {
this.params = params;
}
/**
* @param {?} params
* @return {?}
*/
set translate(params) {
this.params = params;
}
/**
* @return {?}
*/
setup() {
this.translation.translationChanged().pipe(takeUntil(this.destroy)).subscribe(() => { this.replace(); });
}
/**
* @return {?}
*/
replace() {
this.replaceText();
this.replaceAttributes();
}
/**
* @return {?}
*/
replaceText() {
if (!!this.key) {
this.setText(this.getValue(this.key));
}
}
/**
* @return {?}
*/
replaceAttributes() {
if (this.attributes.length > 0) {
/** @type {?} */
const keys = this.getAttributesKeys();
/** @type {?} */
const data = this.translation.translate(keys, this.params);
this.setAttributes(data);
}
}
/**
* @param {?} key
* @return {?}
*/
getValue(key) {
return this.translation.translate(key, this.params);
}
}
TranslateDirective.decorators = [
{ type: Directive, args: [{
selector: '[l10nTranslate],[translate]'
},] }
];
/** @nocollapse */
TranslateDirective.ctorParameters = () => [
{ type: TranslationService },
{ type: ElementRef },
{ type: Renderer2 }
];
TranslateDirective.propDecorators = {
l10nTranslate: [{ type: Input }],
translate: [{ type: Input }],
params: [{ type: Input }]
};
if (false) {
/** @type {?} */
TranslateDirective.prototype.params;
/** @type {?} */
TranslateDirective.prototype.translation;
/** @type {?} */
TranslateDirective.prototype.el;
/** @type {?} */
TranslateDirective.prototype.renderer;
}
//# sourceMappingURL=translate.directive.js.map