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'] 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} 14 15register('en', () => import('../locales/en.json')) 16register('zh', () => import('../locales/zh.json')) 17register('ja', () => import('../locales/ja.json')) 18register('ko', () => import('../locales/ko.json')) 19 20function getInitialLocale(): string { 21 const stored = localStorage.getItem(LOCALE_STORAGE_KEY) 22 if (stored && SUPPORTED_LOCALES.includes(stored as SupportedLocale)) { 23 return stored 24 } 25 26 const browserLocale = getLocaleFromNavigator() 27 if (browserLocale) { 28 const lang = browserLocale.split('-')[0] 29 if (SUPPORTED_LOCALES.includes(lang as SupportedLocale)) { 30 return lang 31 } 32 } 33 34 return 'en' 35} 36 37export function initI18n() { 38 init({ 39 fallbackLocale: 'en', 40 initialLocale: getInitialLocale() 41 }) 42} 43 44export function setLocale(newLocale: SupportedLocale) { 45 locale.set(newLocale) 46 localStorage.setItem(LOCALE_STORAGE_KEY, newLocale) 47 document.documentElement.lang = newLocale 48} 49 50export function getSupportedLocales(): SupportedLocale[] { 51 return [...SUPPORTED_LOCALES] 52} 53 54export { locale, _ }