this repo has no description
1import { register, init, getLocaleFromNavigator, locale, _ } from 'svelte-i18n'
2
3const LOCALE_STORAGE_KEY = 'tranquil-pds-locale'
4
5const SUPPORTED_LOCALES = ['en', 'zh', 'ja', 'ko', 'sv', 'fi'] as const
6export type SupportedLocale = typeof SUPPORTED_LOCALES[number]
7
8export const localeNames: Record<SupportedLocale, string> = {
9 en: 'English',
10 zh: '中文',
11 ja: '日本語',
12 ko: '한국어',
13 sv: 'Svenska',
14 fi: 'Suomi'
15}
16
17register('en', () => import('../locales/en.json'))
18register('zh', () => import('../locales/zh.json'))
19register('ja', () => import('../locales/ja.json'))
20register('ko', () => import('../locales/ko.json'))
21register('sv', () => import('../locales/sv.json'))
22register('fi', () => import('../locales/fi.json'))
23
24function getInitialLocale(): string {
25 const stored = localStorage.getItem(LOCALE_STORAGE_KEY)
26 if (stored && SUPPORTED_LOCALES.includes(stored as SupportedLocale)) {
27 return stored
28 }
29
30 const browserLocale = getLocaleFromNavigator()
31 if (browserLocale) {
32 const lang = browserLocale.split('-')[0]
33 if (SUPPORTED_LOCALES.includes(lang as SupportedLocale)) {
34 return lang
35 }
36 }
37
38 return 'en'
39}
40
41export function initI18n() {
42 init({
43 fallbackLocale: 'en',
44 initialLocale: getInitialLocale()
45 })
46}
47
48export function setLocale(newLocale: SupportedLocale) {
49 locale.set(newLocale)
50 localStorage.setItem(LOCALE_STORAGE_KEY, newLocale)
51 document.documentElement.lang = newLocale
52}
53
54export function getSupportedLocales(): SupportedLocale[] {
55 return [...SUPPORTED_LOCALES]
56}
57
58export { locale, _ }