web: refactor and deduplicate locale preference logic
This commit is contained in:
31
web/src/lib/i18n/locale.ts
Normal file
31
web/src/lib/i18n/locale.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { derived } from 'svelte/store';
|
||||
|
||||
import languages from '$i18n/languages.json';
|
||||
|
||||
import settings from '$lib/state/settings';
|
||||
import { device } from '$lib/device';
|
||||
import { INTERNAL_locale, defaultLocale } from '$lib/i18n/translations';
|
||||
|
||||
const isValid = (lang: string) => (
|
||||
Object.keys(languages).includes(lang)
|
||||
);
|
||||
|
||||
export default derived(
|
||||
settings,
|
||||
($settings) => {
|
||||
let currentLocale = defaultLocale;
|
||||
|
||||
if ($settings.appearance.autoLanguage) {
|
||||
if (isValid(device.prefers.language)) {
|
||||
currentLocale = device.prefers.language;
|
||||
}
|
||||
} else {
|
||||
if (isValid($settings.appearance.language)) {
|
||||
currentLocale = $settings.appearance.language;
|
||||
}
|
||||
}
|
||||
|
||||
INTERNAL_locale.set(currentLocale);
|
||||
return currentLocale;
|
||||
}
|
||||
);
|
||||
@@ -1,6 +1,6 @@
|
||||
import i18n from 'sveltekit-i18n';
|
||||
|
||||
import type { Config } from 'sveltekit-i18n'
|
||||
import type { Config } from 'sveltekit-i18n';
|
||||
import type {
|
||||
GenericImport,
|
||||
StructuredLocfileInfo,
|
||||
@@ -45,6 +45,6 @@ const config: Config = {
|
||||
|
||||
export { defaultLocale };
|
||||
export const {
|
||||
t, loading, locales, locale, translations,
|
||||
t, loading, locales, locale: INTERNAL_locale, translations,
|
||||
loadTranslations, addTranslations, setLocale, setRoute
|
||||
} = new i18n(config);
|
||||
|
||||
Reference in New Issue
Block a user