feat: automatically detect language on server and translate UI (#530)
* feat: detect language on the server and translate page * translate auth pages * PR reviews
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
import { config, loadTranslations } from '$lib/utils/functions/translations';
|
||||
import { profile } from '$lib/utils/store/user';
|
||||
import { get } from 'svelte/store';
|
||||
|
||||
const SUPPORTED_LANGUAGES = config?.loaders?.map((loader) => loader.locale) || [];
|
||||
|
||||
export const load = async ({ url, data }) => {
|
||||
const { pathname } = url;
|
||||
|
||||
const profileStore = get(profile);
|
||||
|
||||
const serverLang = data.serverLang.split('-')[0];
|
||||
|
||||
const userLocale = profileStore.id ? profileStore.locale : getInitialLocale(serverLang);
|
||||
|
||||
const initLocale = getInitialLocale(userLocale);
|
||||
await loadTranslations(initLocale, pathname); // keep this just before the `return`
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
function getInitialLocale(lang: string): string {
|
||||
const locale = lang.split('-')[0];
|
||||
|
||||
if (SUPPORTED_LANGUAGES.includes(locale)) return locale;
|
||||
|
||||
return 'en';
|
||||
}
|
||||
Reference in New Issue
Block a user