fix: boot in the account's language when offline

The store boots from localStorage, but 'app_language' is only written
on an explicit in-app choice; a language that lives in the account's
server settings never reaches the device. A PWA cold start with no
network then runs in English even though every online session ran in
the user's language, the same stranding #1618 fixed for currency and
units.

loadSettings now mirrors the fetched language into its own key
('app_language_server'), and the boot chain falls back to it between
the explicit choice and 'en'. A separate key on purpose: 'app_language'
means an explicit choice, and the login page's detection chain must
keep running for users who never made one.
This commit is contained in:
Cartoone9
2026-08-29 17:38:51 +02:00
committed by Maurice
parent 92bb834f2d
commit 537c450653
2 changed files with 54 additions and 1 deletions
+39
View File
@@ -90,3 +90,42 @@ describe('settings tile template hygiene', () => {
expect(clearTileCache).not.toHaveBeenCalled()
})
})
// The account's language lives on the server; the store boots from localStorage. Without a
// mirror of the server value, a cold start with no network (PWA in airplane mode) booted in
// English even though every online session ran in the user's language — the same stranding
// #1618 fixed for currency and units. 'app_language' stays reserved for an explicit in-app
// choice (the login page's detection chain keys off it), so the mirror gets its own key.
describe('offline language fallback', () => {
beforeEach(() => {
localStorage.removeItem('app_language')
localStorage.removeItem('app_language_server')
})
it('SETTINGS-LANG-001: loadSettings mirrors the account language for the next launch', async () => {
vi.mocked(settingsApi.get).mockResolvedValue({ settings: { language: 'fr' } } as never)
await useSettingsStore.getState().loadSettings()
expect(localStorage.getItem('app_language_server')).toBe('fr')
})
it('SETTINGS-LANG-002: the mirror never claims to be an explicit choice', async () => {
vi.mocked(settingsApi.get).mockResolvedValue({ settings: { language: 'fr' } } as never)
await useSettingsStore.getState().loadSettings()
expect(localStorage.getItem('app_language')).toBeNull()
})
it('SETTINGS-LANG-003: a cold start with no explicit choice boots in the mirrored language', async () => {
localStorage.setItem('app_language_server', 'fr')
vi.resetModules()
const fresh = await import('./settingsStore')
expect(fresh.DEFAULT_SETTINGS.language).toBe('fr')
})
it('SETTINGS-LANG-004: an explicit in-app choice outranks the mirror', async () => {
localStorage.setItem('app_language', 'de')
localStorage.setItem('app_language_server', 'fr')
vi.resetModules()
const fresh = await import('./settingsStore')
expect(fresh.DEFAULT_SETTINGS.language).toBe('de')
})
})
+15 -1
View File
@@ -35,7 +35,11 @@ export const DEFAULT_SETTINGS: Settings = {
dark_mode: false,
// Empty = no personal display currency, so Costs falls back to the trip's own.
default_currency: '',
language: localStorage.getItem('app_language') || 'en',
// Explicit in-app choice first, then the mirror of the account's server-side
// language (written by loadSettings below), then English. Without the mirror
// an offline cold start boots in English: the account's language lives on the
// server, and the fetch that would apply it cannot happen.
language: localStorage.getItem('app_language') || localStorage.getItem('app_language_server') || 'en',
temperature_unit: 'celsius',
distance_unit: 'metric',
time_format: '24h',
@@ -99,6 +103,16 @@ export const useSettingsStore = create<SettingsState>((set, get) => ({
// The startup redirect runs before this ever resolves, so keep a mirror
// it can read synchronously on the next launch.
rememberStartDestination(incoming)
// Same trick for the language: mirror the account's value so the next
// launch — including an offline one, where this fetch fails — boots in
// it instead of English (#1618 fixed the same stranding for currency
// and units, but the language never made it into that fix). Its own
// key rather than 'app_language': that one means an explicit in-app
// choice, and the login page's detection chain must keep running for
// users who never made one.
try {
if (incoming.language) localStorage.setItem('app_language_server', incoming.language)
} catch { /* private mode — the session still has the value in the store */ }
} catch (err: unknown) {
// Leave isLoaded false so a transient failure — offline at launch, or a
// (docker) server cold-start racing the first request — is retried on