Merge pull request #12077 from vvlladd28/improvent/page-web-lang

Update document 'lang' attribute based on user language preference
This commit is contained in:
Andrew Shvayka 2024-11-20 14:09:23 +01:00 committed by GitHub
commit bbf88076c7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 4 deletions

View File

@ -15,7 +15,7 @@
/// ///
import { ActivationEnd, Router } from '@angular/router'; import { ActivationEnd, Router } from '@angular/router';
import { Injectable } from '@angular/core'; import { Inject, Injectable } from '@angular/core';
import { select, Store } from '@ngrx/store'; import { select, Store } from '@ngrx/store';
import { Actions, createEffect, ofType } from '@ngrx/effects'; import { Actions, createEffect, ofType } from '@ngrx/effects';
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
@ -31,6 +31,7 @@ import { updateUserLang } from '@app/core/settings/settings.utils';
import { UtilsService } from '@core/services/utils.service'; import { UtilsService } from '@core/services/utils.service';
import { getCurrentAuthUser } from '@core/auth/auth.selectors'; import { getCurrentAuthUser } from '@core/auth/auth.selectors';
import { ActionAuthUpdateLastPublicDashboardId } from '../auth/auth.actions'; import { ActionAuthUpdateLastPublicDashboardId } from '../auth/auth.actions';
import { DOCUMENT } from '@angular/common';
export const SETTINGS_KEY = 'SETTINGS'; export const SETTINGS_KEY = 'SETTINGS';
@ -43,7 +44,8 @@ export class SettingsEffects {
private router: Router, private router: Router,
private localStorageService: LocalStorageService, private localStorageService: LocalStorageService,
private titleService: TitleService, private titleService: TitleService,
private translate: TranslateService private translate: TranslateService,
@Inject(DOCUMENT) private document: Document,
) { ) {
} }
@ -56,7 +58,7 @@ export class SettingsEffects {
distinctUntilChanged((a, b) => a?.userLang === b?.userLang), distinctUntilChanged((a, b) => a?.userLang === b?.userLang),
tap(setting => { tap(setting => {
this.localStorageService.setItem(SETTINGS_KEY, setting); this.localStorageService.setItem(SETTINGS_KEY, setting);
updateUserLang(this.translate, setting.userLang); updateUserLang(this.translate, this.document, setting.userLang);
}) })
), {dispatch: false}); ), {dispatch: false});

View File

@ -19,7 +19,7 @@ import { TranslateService } from '@ngx-translate/core';
import _moment from 'moment'; import _moment from 'moment';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
export function updateUserLang(translate: TranslateService, userLang: string, translations = env.supportedLangs): Observable<any> { export function updateUserLang(translate: TranslateService, document: Document, userLang: string, translations = env.supportedLangs): Observable<any> {
let targetLang = userLang; let targetLang = userLang;
if (!translations) { if (!translations) {
translations = env.supportedLangs; translations = env.supportedLangs;
@ -37,6 +37,7 @@ export function updateUserLang(translate: TranslateService, userLang: string, tr
if (!env.production) { if (!env.production) {
console.log(`Detected supported lang: ${detectedSupportedLang}`); console.log(`Detected supported lang: ${detectedSupportedLang}`);
} }
document.documentElement.lang = detectedSupportedLang.replace('_', '-');
_moment.locale([detectedSupportedLang]); _moment.locale([detectedSupportedLang]);
return translate.use(detectedSupportedLang); return translate.use(detectedSupportedLang);
} }