import { ChangeDetectionStrategy, Component, Input, booleanAttribute, inject, DOCUMENT } from '@angular/core'; import { I18NService } from '@core'; import { YUNZAI_I18N_TOKEN, I18nPipe, SettingsService } from '@yelon/theme'; import { NzDropDownModule } from 'ng-zorro-antd/dropdown'; import { NzIconModule } from 'ng-zorro-antd/icon'; import { NzMenuModule } from 'ng-zorro-antd/menu'; @Component({ selector: 'header-i18n', template: ` @if (showLangText) {
{{ 'menu.lang' | i18n }}
} @else { } `, host: { '[class.flex-1]': 'true' }, changeDetection: ChangeDetectionStrategy.OnPush, imports: [I18nPipe, NzDropDownModule, NzIconModule, NzMenuModule] }) export class HeaderI18nComponent { private readonly settings = inject(SettingsService); private readonly i18n = inject(YUNZAI_I18N_TOKEN); private readonly doc = inject(DOCUMENT); /** Whether to display language text */ @Input({ transform: booleanAttribute }) showLangText = true; get langs(): Array<{ code: string; text: string; abbr: string }> { return this.i18n.getLangs(); } get curLangCode(): string { return this.settings.layout.lang; } change(lang: string): void { const spinEl = this.doc.createElement('div'); spinEl.setAttribute('class', `page-loading ant-spin ant-spin-lg ant-spin-spinning`); spinEl.innerHTML = ``; this.doc.body.appendChild(spinEl); this.i18n.loadLangData(lang).subscribe(res => { this.i18n.use(lang, res); this.settings.setLayout('lang', lang); setTimeout(() => this.doc.location.reload()); }); } }