web: refactor and deduplicate locale preference logic

This commit is contained in:
dumbmoron
2024-07-20 14:37:52 +00:00
parent d4d4eded32
commit 8a080c55f6
7 changed files with 40 additions and 50 deletions

View 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;
}
);

View File

@@ -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);