From 59cc7ec645db1b35943ebc0b41e28143afc4159d Mon Sep 17 00:00:00 2001 From: meta-d Date: Sat, 27 Jan 2024 20:51:43 +0800 Subject: [PATCH] fix: Auth login page language --- apps/cloud/src/app/@core/config/index.ts | 1 + apps/cloud/src/app/@core/config/language.ts | 13 +++++++ apps/cloud/src/app/@core/constants/index.ts | 2 -- apps/cloud/src/app/@core/index.ts | 3 +- apps/cloud/src/app/@core/providers/index.ts | 2 ++ apps/cloud/src/app/@core/providers/logger.ts | 13 +++++++ .../src/app/@core/providers/translate.ts | 5 +++ apps/cloud/src/app/app.component.ts | 4 +-- libs/apps/auth/src/lib/auth.component.html | 4 +-- libs/apps/auth/src/lib/auth.component.ts | 35 +++++++++---------- 10 files changed, 56 insertions(+), 26 deletions(-) create mode 100644 apps/cloud/src/app/@core/config/index.ts create mode 100644 apps/cloud/src/app/@core/config/language.ts delete mode 100644 apps/cloud/src/app/@core/constants/index.ts create mode 100644 apps/cloud/src/app/@core/providers/index.ts create mode 100644 apps/cloud/src/app/@core/providers/logger.ts create mode 100644 apps/cloud/src/app/@core/providers/translate.ts diff --git a/apps/cloud/src/app/@core/config/index.ts b/apps/cloud/src/app/@core/config/index.ts new file mode 100644 index 000000000..9e99e9177 --- /dev/null +++ b/apps/cloud/src/app/@core/config/index.ts @@ -0,0 +1 @@ +export * from './language' \ No newline at end of file diff --git a/apps/cloud/src/app/@core/config/language.ts b/apps/cloud/src/app/@core/config/language.ts new file mode 100644 index 000000000..64bbf5c7d --- /dev/null +++ b/apps/cloud/src/app/@core/config/language.ts @@ -0,0 +1,13 @@ +export enum LanguagesEnum { + Chinese = 'zh-CN', + SimplifiedChinese = 'zh-Hans', + TraditionalChinese = 'zh-Hant', + English = 'en' +} + +export const LanguagesMap = { + 'zh-CN': LanguagesEnum.SimplifiedChinese, + 'zh-Hans': LanguagesEnum.SimplifiedChinese, + zh: LanguagesEnum.SimplifiedChinese, + 'zh-HK': LanguagesEnum.TraditionalChinese, +} diff --git a/apps/cloud/src/app/@core/constants/index.ts b/apps/cloud/src/app/@core/constants/index.ts deleted file mode 100644 index 47e2d9178..000000000 --- a/apps/cloud/src/app/@core/constants/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -// export * from './app.constants'; -// export * from './layout.constants' diff --git a/apps/cloud/src/app/@core/index.ts b/apps/cloud/src/app/@core/index.ts index dbf4d985e..8e8257e14 100644 --- a/apps/cloud/src/app/@core/index.ts +++ b/apps/cloud/src/app/@core/index.ts @@ -8,4 +8,5 @@ export * from './theme/index' export * from './types' export * from './validators/index' export * from './animations/index' -export * from './utils' \ No newline at end of file +export * from './utils' +export * from './providers/' \ No newline at end of file diff --git a/apps/cloud/src/app/@core/providers/index.ts b/apps/cloud/src/app/@core/providers/index.ts new file mode 100644 index 000000000..d16171b6e --- /dev/null +++ b/apps/cloud/src/app/@core/providers/index.ts @@ -0,0 +1,2 @@ +export * from './translate' +export * from './logger' \ No newline at end of file diff --git a/apps/cloud/src/app/@core/providers/logger.ts b/apps/cloud/src/app/@core/providers/logger.ts new file mode 100644 index 000000000..c93996484 --- /dev/null +++ b/apps/cloud/src/app/@core/providers/logger.ts @@ -0,0 +1,13 @@ +import { EnvironmentProviders, importProvidersFrom } from '@angular/core' +import { environment } from 'apps/cloud/src/environments/environment' +import { LoggerModule, NgxLoggerLevel } from 'ngx-logger' + +export function provideLogger(): EnvironmentProviders { + return importProvidersFrom( + LoggerModule.forRoot({ + // serverLoggingUrl: '/api/logs', + level: environment.production ? NgxLoggerLevel.WARN : NgxLoggerLevel.DEBUG, + serverLogLevel: NgxLoggerLevel.ERROR + }) + ) +} diff --git a/apps/cloud/src/app/@core/providers/translate.ts b/apps/cloud/src/app/@core/providers/translate.ts new file mode 100644 index 000000000..36195fbbf --- /dev/null +++ b/apps/cloud/src/app/@core/providers/translate.ts @@ -0,0 +1,5 @@ +import { LanguagesMap } from '../config' + +export function navigatorLanguage() { + return LanguagesMap[navigator.language] || navigator.language +} diff --git a/apps/cloud/src/app/app.component.ts b/apps/cloud/src/app/app.component.ts index 5c1922f14..9fa5726ba 100644 --- a/apps/cloud/src/app/app.component.ts +++ b/apps/cloud/src/app/app.component.ts @@ -11,7 +11,7 @@ import { TranslateService } from '@ngx-translate/core' import { NGXLogger } from 'ngx-logger' import { combineLatest } from 'rxjs' import { filter, map } from 'rxjs/operators' -import { ICONS, LanguagesService, PACThemeService, Store, UpdateService, mapDateLocale } from './@core' +import { ICONS, LanguagesService, PACThemeService, Store, UpdateService, mapDateLocale, navigatorLanguage } from './@core' import { AppService } from './app.service' @@ -48,7 +48,7 @@ export class AppComponent implements OnInit { ) { translate.setDefaultLang('en') // the lang to use, if the lang isn't available, it will use the current loader to get them - translate.use(this.store.preferredLanguage || navigator.language || 'en') + translate.use(this.store.preferredLanguage || navigatorLanguage()) this.document.documentElement.lang = translate.currentLang this.store.preferredLanguage$.pipe(filter(nonNullable)).subscribe((language) => { diff --git a/libs/apps/auth/src/lib/auth.component.html b/libs/apps/auth/src/lib/auth.component.html index 5116505f6..9a7d406af 100644 --- a/libs/apps/auth/src/lib/auth.component.html +++ b/libs/apps/auth/src/lib/auth.component.html @@ -14,11 +14,11 @@ diff --git a/libs/apps/auth/src/lib/auth.component.ts b/libs/apps/auth/src/lib/auth.component.ts index 26f54e71b..2eb804bbe 100644 --- a/libs/apps/auth/src/lib/auth.component.ts +++ b/libs/apps/auth/src/lib/auth.component.ts @@ -1,7 +1,9 @@ import { Location } from '@angular/common' -import { Component } from '@angular/core' -import { UntilDestroy } from '@ngneat/until-destroy' +import { Component, effect, inject } from '@angular/core' +import { toSignal } from '@angular/core/rxjs-interop' import { Store } from '@metad/cloud/state' +import { UntilDestroy } from '@ngneat/until-destroy' +import { TranslateService } from '@ngx-translate/core' import { map } from 'rxjs/operators' import { PacAuthService } from './services/auth.service' @@ -15,7 +17,13 @@ import { PacAuthService } from './services/auth.service' } }) export class PacAuthComponent { - authenticated = false + readonly #translate = inject(TranslateService) + protected auth = inject(PacAuthService) + protected store = inject(Store) + protected location = inject(Location) + + readonly language = toSignal(this.#translate.onLangChange.pipe(map(() => this.#translate.currentLang))) + token = '' // showcase of how to use the onAuthenticationChange method @@ -34,29 +42,18 @@ export class PacAuthComponent { } ] - set language(value: string) { - this.store.preferredLanguage = value + constructor() { + effect(() => { + console.log(this.language()) + }) } - public readonly preferredLanguage$ = this.store.preferredLanguage$.pipe( - map((preferredLanguage) => preferredLanguage ?? navigator.language ?? 'en') - ) - - private _AuthSub = this.auth.onAuthenticationChange().subscribe((authenticated: boolean) => { - this.authenticated = authenticated - }) - constructor( - protected auth: PacAuthService, - protected store: Store, - protected location: Location, - ) {} - back() { this.location.back() return false } onLanguage(value) { - this.language = value + this.store.preferredLanguage = value } }