import { Directive, Input } from '@angular/core'; import { isObservable } from 'rxjs'; import { equals, isDefined } from './util'; import * as i0 from "@angular/core"; import * as i1 from "./translate.service"; class TranslateDirective { translateService; element; _ref; key; lastParams; currentParams; onLangChangeSub; onDefaultLangChangeSub; onTranslationChangeSub; set translate(key) { if (key) { this.key = key; this.checkNodes(); } } set translateParams(params) { if (!equals(this.currentParams, params)) { this.currentParams = params; this.checkNodes(true); } } constructor(translateService, element, _ref) { this.translateService = translateService; this.element = element; this._ref = _ref; // subscribe to onTranslationChange event, in case the translations of the current lang change if (!this.onTranslationChangeSub) { this.onTranslationChangeSub = this.translateService.onTranslationChange.subscribe((event) => { if (event.lang === this.translateService.currentLang) { this.checkNodes(true, event.translations); } }); } // subscribe to onLangChange event, in case the language changes if (!this.onLangChangeSub) { this.onLangChangeSub = this.translateService.onLangChange.subscribe((event) => { this.checkNodes(true, event.translations); }); } // subscribe to onDefaultLangChange event, in case the default language changes if (!this.onDefaultLangChangeSub) { this.onDefaultLangChangeSub = this.translateService.onDefaultLangChange.subscribe((event) => { this.checkNodes(true); }); } } ngAfterViewChecked() { this.checkNodes(); } checkNodes(forceUpdate = false, translations) { let nodes = this.element.nativeElement.childNodes; // if the element is empty if (!nodes.length) { // we add the key as content this.setContent(this.element.nativeElement, this.key); nodes = this.element.nativeElement.childNodes; } for (let i = 0; i < nodes.length; ++i) { let node = nodes[i]; if (node.nodeType === 3) { // node type 3 is a text node let key; if (forceUpdate) { node.lastKey = null; } if (isDefined(node.lookupKey)) { key = node.lookupKey; } else if (this.key) { key = this.key; } else { let content = this.getContent(node); let trimmedContent = content.trim(); if (trimmedContent.length) { node.lookupKey = trimmedContent; // we want to use the content as a key, not the translation value if (content !== node.currentValue) { key = trimmedContent; // the content was changed from the user, we'll use it as a reference if needed node.originalContent = content || node.originalContent; } else if (node.originalContent) { // the content seems ok, but the lang has changed // the current content is the translation, not the key, use the last real content as key key = node.originalContent.trim(); } else if (content !== node.currentValue) { // we want to use the content as a key, not the translation value key = trimmedContent; // the content was changed from the user, we'll use it as a reference if needed node.originalContent = content || node.originalContent; } } } this.updateValue(key, node, translations); } } } updateValue(key, node, translations) { if (key) { if (node.lastKey === key && this.lastParams === this.currentParams) { return; } this.lastParams = this.currentParams; let onTranslation = (res) => { if (res !== key) { node.lastKey = key; } if (!node.originalContent) { node.originalContent = this.getContent(node); } node.currentValue = isDefined(res) ? res : (node.originalContent || key); // we replace in the original content to preserve spaces that we might have trimmed this.setContent(node, this.key ? node.currentValue : node.originalContent.replace(key, node.currentValue)); this._ref.markForCheck(); }; if (isDefined(translations)) { let res = this.translateService.getParsedResult(translations, key, this.currentParams); if (isObservable(res)) { res.subscribe({ next: onTranslation }); } else { onTranslation(res); } } else { this.translateService.get(key, this.currentParams).subscribe(onTranslation); } } } getContent(node) { return isDefined(node.textContent) ? node.textContent : node.data; } setContent(node, content) { if (isDefined(node.textContent)) { node.textContent = content; } else { node.data = content; } } ngOnDestroy() { if (this.onLangChangeSub) { this.onLangChangeSub.unsubscribe(); } if (this.onDefaultLangChangeSub) { this.onDefaultLangChangeSub.unsubscribe(); } if (this.onTranslationChangeSub) { this.onTranslationChangeSub.unsubscribe(); } } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: TranslateDirective, deps: [{ token: i1.TranslateService }, { token: i0.ElementRef }, { token: i0.ChangeDetectorRef }], target: i0.ɵɵFactoryTarget.Directive }); static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "16.0.0", type: TranslateDirective, selector: "[translate],[ngx-translate]", inputs: { translate: "translate", translateParams: "translateParams" }, ngImport: i0 }); } export { TranslateDirective }; i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: TranslateDirective, decorators: [{ type: Directive, args: [{ selector: '[translate],[ngx-translate]' }] }], ctorParameters: function () { return [{ type: i1.TranslateService }, { type: i0.ElementRef }, { type: i0.ChangeDetectorRef }]; }, propDecorators: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LZXkgPSBudWxsO1xyXG4gICAgICAgIH1cclxuICAgICAgICBpZihpc0RlZmluZWQobm9kZS5sb29rdXBLZXkpKSB7XHJcbiAgICAgICAgICBrZXkgPSBub2RlLmxvb2t1cEtleTtcclxuICAgICAgICB9IGVsc2UgaWYgKHRoaXMua2V5KSB7XHJcbiAgICAgICAgICBrZXkgPSB0aGlzLmtleTtcclxuICAgICAgICB9IGVsc2Uge1xyXG4gICAgICAgICAgbGV0IGNvbnRlbnQgPSB0aGlzLmdldENvbnRlbnQobm9kZSk7XHJcbiAgICAgICAgICBsZXQgdHJpbW1lZENvbnRlbnQgPSBjb250ZW50LnRyaW0oKTtcclxuICAgICAgICAgIGlmICh0cmltbWVkQ29udGVudC5sZW5ndGgpIHtcclxuICAgICAgICAgICAgbm9kZS5sb29rdXBLZXkgPSB0cmltbWVkQ29udGVudDtcclxuICAgICAgICAgICAgLy8gd2Ugd2FudCB0byB1c2UgdGhlIGNvbnRlbnQgYXMgYSBrZXksIG5vdCB0aGUgdHJhbnNsYXRpb24gdmFsdWVcclxuICAgICAgICAgICAgaWYgKGNvbnRlbnQgIT09IG5vZGUuY3VycmVudFZhbHVlKSB7XHJcbiAgICAgICAgICAgICAga2V5ID0gdHJpbW1lZENvbnRlbnQ7XHJcbiAgICAgICAgICAgICAgLy8gdGhlIGNvbnRlbnQgd2FzIGNoYW5nZWQgZnJvbSB0aGUgdXNlciwgd2UnbGwgdXNlIGl0IGFzIGEgcmVmZXJlbmNlIGlmIG5lZWRlZFxyXG4gICAgICAgICAgICAgIG5vZGUub3JpZ2luYWxDb250ZW50ID0gY29udGVudCB8fCBub2RlLm9yaWdpbmFsQ29udGVudDtcclxuICAgICAgICAgICAgfSBlbHNlIGlmIChub2RlLm9yaWdpbmFsQ29udGVudCkgeyAvLyB0aGUgY29udGVudCBzZWVtcyBvaywgYnV0IHRoZSBsYW5nIGhhcyBjaGFuZ2VkXHJcbiAgICAgICAgICAgICAgLy8gdGhlIGN1cnJlbnQgY29udG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