All files / src/lib chosen-locale.ts

100% Statements 9/9
100% Branches 4/4
100% Functions 3/3
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35            3x         3x 3x             29x 29x 28x   1x         3x 3x          
// A language the user chose from the sign-in page's menu, remembered for this
// tab only (spec: "Choosing the language before sign-in"). sessionStorage is
// gone when the tab closes, so a choice made by the previous user of a shared
// device never carries over; the language cookie alone never counts as one.
import { isLocale, type Locale } from '#/paraglide/runtime.js'
 
const KEY = 'kotivalo_locale_chosen'
 
// Some private modes throw on sessionStorage: then nothing is remembered and
// the sign-in behaves as without a choice.
export function rememberChosenLocale(locale: Locale): void {
  try {
    sessionStorage.setItem(KEY, locale)
  } catch {
    // Not available.
  }
}
 
export function chosenLocale(): Locale | undefined {
  try {
    const value = sessionStorage.getItem(KEY)
    return value !== null && isLocale(value) ? value : undefined
  } catch {
    return undefined
  }
}
 
export function forgetChosenLocale(): void {
  try {
    sessionStorage.removeItem(KEY)
  } catch {
    // Not available.
  }
}