feat(ui): sync profile locale and theme preferences (#1604)

This commit is contained in:
Zach
2026-06-02 16:40:16 -04:00
committed by GitHub
parent ad63a95e24
commit d6d18aa90b
25 changed files with 543 additions and 196 deletions
@@ -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));
};
}