Files
classroomio/apps/dashboard/src/lib/utils/functions/translations.ts
T

99 lines
2.3 KiB
TypeScript
Raw Normal View History

import { LOCALE } from '$lib/utils/types';
import i18n from '@sveltekit-i18n/base';
import parser from '@sveltekit-i18n/parser-icu';
2024-04-19 05:45:48 +01:00
import { writable } from 'svelte/store';
export const config = {
parser: parser(),
2024-04-19 05:45:48 +01:00
loaders: [
{
locale: 'en',
key: '',
loader: async () => (await import('../translations/en.json')).default
},
{
locale: 'hi',
key: '',
loader: async () => (await import('../translations/hi.json')).default
},
{
locale: 'fr',
key: '',
loader: async () => (await import('../translations/fr.json')).default
},
2025-08-08 04:53:38 +01:00
{
locale: 'pl',
key: '',
loader: async () => (await import('../translations/pl.json')).default
},
2024-04-19 05:45:48 +01:00
{
locale: 'pt',
key: '',
loader: async () => (await import('../translations/pt.json')).default
},
{
locale: 'de',
key: '',
loader: async () => (await import('../translations/de.json')).default
},
{
locale: 'vi',
key: '',
loader: async () => (await import('../translations/vi.json')).default
},
{
locale: 'ru',
key: '',
loader: async () => (await import('../translations/ru.json')).default
},
{
locale: 'es',
key: '',
loader: async () => (await import('../translations/es.json')).default
},
{
locale: 'da',
key: '',
loader: async () => (await import('../translations/da.json')).default
2024-04-19 05:45:48 +01:00
}
]
};
2025-11-01 15:49:29 +01:00
export const { t, loading, locales, locale, initialized, translations, loadTranslations } = new i18n(config);
2024-04-19 05:45:48 +01:00
export const selectedLocale = writable<string>('en');
// Translations logs
loading.subscribe(async ($loading) => {
if ($loading) {
console.log('Loading translations...');
await loading.toPromise();
}
});
2025-11-01 15:49:29 +01:00
export function handleLocaleChange(newLocale: LOCALE) {
2024-04-19 05:45:48 +01:00
if (!newLocale) {
return;
}
locale.set(newLocale);
2025-01-09 01:30:01 +01:00
selectedLocale.set(newLocale);
2024-04-19 05:45:48 +01:00
}
2025-11-01 15:49:29 +01:00
export function lessonFallbackNote(note: string, translation: Record<LOCALE, string>, locale: LOCALE) {
2024-04-19 05:45:48 +01:00
if (!translation) {
return note;
}
const content = translation[locale];
// if locale is english and no translated content for english but note exists
if (locale === LOCALE.EN && !content && note?.length) {
return note;
}
return content;
}