diff --git a/ui/src/app/app.component.html b/ui/src/app/app.component.html index 7567b06f7..51cf460d0 100644 --- a/ui/src/app/app.component.html +++ b/ui/src/app/app.component.html @@ -20,7 +20,7 @@ aria-expanded="false" ngbDropdownToggle> - Add + Add
` +}) +export class TranslateComponent implements OnDestroy { + private _key: string; + lastParams: any; + currentParams: any; + messages: Messages = {}; + sub: Subscription; + default: string; + + @Input() set key(key: string) { + if (key) { + this.default = this.default || this._key; + this._key = key; + this.update(); + } + } + + @Input() set params(params: any) { + this.currentParams = params || {}; + this.update(); + } + + constructor( + private service: I18nService, + private store: Store, + private element: ElementRef, + private _ref: ChangeDetectorRef + ) { + this.sub = this.store.select(fromI18n.getMessages).subscribe(m => { + if (m && Object.keys(m).length) { + this.messages = m; + this.update(); + } + }); + } + + update(): void { + const translated = this.service.translate(this._key, this.currentParams, this.messages); + this.element.nativeElement.textContent = translated; + } + + ngOnDestroy() { + if (this.sub) { + this.sub.unsubscribe(); + } + } +} diff --git a/ui/src/app/i18n/i18n.module.ts b/ui/src/app/i18n/i18n.module.ts index 3823f7462..245504aba 100644 --- a/ui/src/app/i18n/i18n.module.ts +++ b/ui/src/app/i18n/i18n.module.ts @@ -10,8 +10,11 @@ import { MessageEffects } from './effect/message.effect'; import { TranslatePipe } from './pipe/i18n.pipe'; import { CoreModule } from '../core/core.module'; import { TranslateDirective } from './directive/translate.directive'; +import { TranslateComponent } from './component/translate.component'; -export const COMPONENTS = []; +export const COMPONENTS = [ + TranslateComponent +]; export const DIRECTIVES = [ TranslateDirective ]; diff --git a/ui/src/app/i18n/service/i18n.service.ts b/ui/src/app/i18n/service/i18n.service.ts index cc38f0404..6b9726180 100644 --- a/ui/src/app/i18n/service/i18n.service.ts +++ b/ui/src/app/i18n/service/i18n.service.ts @@ -38,8 +38,7 @@ export class I18nService { translate(value: string, interpolated: any, messages: Messages): string { interpolated = interpolated || {}; - let val = messages.hasOwnProperty(value) ? messages[value] : value; - console.log(val, messages); + let val = messages.hasOwnProperty(value) ? messages[value] : ''; return this.interpolate(val, interpolated); }