feat(ui): sync profile locale and theme preferences (#1604)
This commit is contained in:
@@ -1,90 +0,0 @@
|
||||
import {TestBed} from '@angular/core/testing';
|
||||
import {of} from 'rxjs';
|
||||
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
|
||||
import {TranslocoService} from '@jsverse/transloco';
|
||||
|
||||
import {initializeLanguage, LANG_STORAGE_KEY, LEGACY_LANG_STORAGE_KEY} from './language-initializer';
|
||||
|
||||
describe('initializeLanguage', () => {
|
||||
const translocoService = {
|
||||
setActiveLang: vi.fn(),
|
||||
load: vi.fn(),
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
localStorage.clear();
|
||||
translocoService.setActiveLang.mockReset();
|
||||
translocoService.load.mockReset();
|
||||
translocoService.load.mockReturnValue(of({}));
|
||||
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
{provide: TranslocoService, useValue: translocoService},
|
||||
]
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
localStorage.clear();
|
||||
TestBed.resetTestingModule();
|
||||
});
|
||||
|
||||
it('uses the current persisted language when it is available', async () => {
|
||||
localStorage.setItem(LANG_STORAGE_KEY, 'de');
|
||||
|
||||
const initializer = TestBed.runInInjectionContext(() => initializeLanguage());
|
||||
|
||||
await TestBed.runInInjectionContext(() => initializer());
|
||||
|
||||
expect(translocoService.setActiveLang).toHaveBeenCalledWith('de');
|
||||
expect(translocoService.load).toHaveBeenCalledWith('de');
|
||||
expect(localStorage.getItem(LANG_STORAGE_KEY)).toBe('de');
|
||||
});
|
||||
|
||||
it('migrates the legacy persisted language key', async () => {
|
||||
localStorage.setItem(LEGACY_LANG_STORAGE_KEY, 'fr');
|
||||
|
||||
const initializer = TestBed.runInInjectionContext(() => initializeLanguage());
|
||||
|
||||
await TestBed.runInInjectionContext(() => initializer());
|
||||
|
||||
expect(translocoService.setActiveLang).toHaveBeenCalledWith('fr');
|
||||
expect(localStorage.getItem(LANG_STORAGE_KEY)).toBe('fr');
|
||||
});
|
||||
|
||||
it('prefers the exact browser locale when it is available', async () => {
|
||||
vi.spyOn(window.navigator, 'language', 'get').mockReturnValue('pt');
|
||||
|
||||
const initializer = TestBed.runInInjectionContext(() => initializeLanguage());
|
||||
|
||||
await TestBed.runInInjectionContext(() => initializer());
|
||||
|
||||
expect(translocoService.setActiveLang).toHaveBeenCalledWith('pt');
|
||||
expect(localStorage.getItem(LANG_STORAGE_KEY)).toBe('pt');
|
||||
});
|
||||
|
||||
it('falls back to the browser base language when the full locale is unavailable', async () => {
|
||||
vi.spyOn(window.navigator, 'language', 'get').mockReturnValue('pt-BR');
|
||||
|
||||
const initializer = TestBed.runInInjectionContext(() => initializeLanguage());
|
||||
|
||||
await TestBed.runInInjectionContext(() => initializer());
|
||||
|
||||
expect(translocoService.setActiveLang).toHaveBeenCalledWith('pt');
|
||||
expect(localStorage.getItem(LANG_STORAGE_KEY)).toBe('pt');
|
||||
});
|
||||
|
||||
it('falls back to english when there is no supported saved or browser language', async () => {
|
||||
localStorage.setItem(LANG_STORAGE_KEY, 'zz');
|
||||
vi.spyOn(window.navigator, 'language', 'get').mockReturnValue('zz-ZZ');
|
||||
|
||||
const initializer = TestBed.runInInjectionContext(() => initializeLanguage());
|
||||
|
||||
await TestBed.runInInjectionContext(() => initializer());
|
||||
|
||||
expect(translocoService.setActiveLang).toHaveBeenCalledWith('en');
|
||||
expect(translocoService.load).toHaveBeenCalledWith('en');
|
||||
expect(localStorage.getItem(LANG_STORAGE_KEY)).toBe('en');
|
||||
});
|
||||
});
|
||||
@@ -1,38 +0,0 @@
|
||||
import {inject} from '@angular/core';
|
||||
import {TranslocoService} from '@jsverse/transloco';
|
||||
import {firstValueFrom} from 'rxjs';
|
||||
import {AVAILABLE_LANGS} from './transloco-loader';
|
||||
|
||||
// TODO(grimmory-cleanup): Remove after the Booklore-to-Grimmory localStorage migration window closes.
|
||||
export const LEGACY_LANG_STORAGE_KEY = 'booklore-lang';
|
||||
export const LANG_STORAGE_KEY = 'grimmory-lang';
|
||||
|
||||
function detectLanguage(available: string[]): string {
|
||||
const saved = localStorage.getItem(LANG_STORAGE_KEY) ?? localStorage.getItem(LEGACY_LANG_STORAGE_KEY);
|
||||
if (saved && available.includes(saved)) {
|
||||
localStorage.setItem(LANG_STORAGE_KEY, saved);
|
||||
return saved;
|
||||
}
|
||||
|
||||
const browserLocale = navigator.language;
|
||||
if (browserLocale && available.includes(browserLocale)) {
|
||||
return browserLocale;
|
||||
}
|
||||
|
||||
const baseLang = browserLocale?.split('-')[0];
|
||||
if (baseLang && available.includes(baseLang)) {
|
||||
return baseLang;
|
||||
}
|
||||
|
||||
return 'en';
|
||||
}
|
||||
|
||||
export function initializeLanguage() {
|
||||
return () => {
|
||||
const translocoService = inject(TranslocoService);
|
||||
const lang = detectLanguage(AVAILABLE_LANGS);
|
||||
translocoService.setActiveLang(lang);
|
||||
localStorage.setItem(LANG_STORAGE_KEY, lang);
|
||||
return firstValueFrom(translocoService.load(lang));
|
||||
};
|
||||
}
|
||||
@@ -1,17 +1,24 @@
|
||||
import {TestBed} from '@angular/core/testing';
|
||||
import {firstValueFrom} from 'rxjs';
|
||||
import {describe, expect, it} from 'vitest';
|
||||
import {describe, expect, it, vi} from 'vitest';
|
||||
|
||||
import {PostLoginInitializerService} from './post-login-initializer.service';
|
||||
import {StartupService} from '../../shared/service/startup.service';
|
||||
|
||||
describe('PostLoginInitializerService', () => {
|
||||
it('completes successfully', async () => {
|
||||
it('hydrates startup preferences after login', async () => {
|
||||
const load = vi.fn().mockResolvedValue(undefined);
|
||||
|
||||
TestBed.configureTestingModule({
|
||||
providers: [PostLoginInitializerService],
|
||||
providers: [
|
||||
PostLoginInitializerService,
|
||||
{provide: StartupService, useValue: {load}},
|
||||
],
|
||||
});
|
||||
|
||||
const service = TestBed.inject(PostLoginInitializerService);
|
||||
|
||||
await expect(firstValueFrom(service.initialize())).resolves.toBeUndefined();
|
||||
expect(load).toHaveBeenCalledOnce();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {Observable, of} from 'rxjs';
|
||||
import {Injectable, inject, Injector} from '@angular/core';
|
||||
import {defer, from, Observable} from 'rxjs';
|
||||
import {StartupService} from '../../shared/service/startup.service';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class PostLoginInitializerService {
|
||||
private readonly injector = inject(Injector);
|
||||
|
||||
initialize(): Observable<void> {
|
||||
return of(undefined);
|
||||
return defer(() => from(this.injector.get(StartupService).load()));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user