2025-11-24 01:00:50 +01:00
|
|
|
import type { TLocale } from '@cio/db/types';
|
2024-12-30 02:59:10 +01:00
|
|
|
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';
|
|
|
|
|
|
2024-12-30 02:59:10 +01:00
|
|
|
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
|
2025-10-13 18:07:46 +01:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
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');
|
2026-03-09 11:18:22 +01:00
|
|
|
export const LOCALE_STORAGE_KEY = 'classroomio_locale';
|
|
|
|
|
export const LOCALE_COOKIE_KEY = 'classroomio_locale';
|
2024-04-19 05:45:48 +01:00
|
|
|
|
|
|
|
|
// Translations logs
|
|
|
|
|
loading.subscribe(async ($loading) => {
|
|
|
|
|
if ($loading) {
|
|
|
|
|
console.log('Loading translations...');
|
|
|
|
|
|
|
|
|
|
await loading.toPromise();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-11-24 01:00:50 +01:00
|
|
|
export function handleLocaleChange(newLocale: TLocale) {
|
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);
|
2026-03-09 11:18:22 +01:00
|
|
|
|
|
|
|
|
persistLocale(newLocale);
|
2024-04-19 05:45:48 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-09 11:18:22 +01:00
|
|
|
export function getPersistedLocale(): string | null {
|
|
|
|
|
if (typeof window === 'undefined') {
|
|
|
|
|
return null;
|
2024-04-19 05:45:48 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-09 11:18:22 +01:00
|
|
|
try {
|
|
|
|
|
const savedLocale = window.localStorage.getItem(LOCALE_STORAGE_KEY);
|
|
|
|
|
if (savedLocale) {
|
|
|
|
|
return savedLocale;
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.warn('Failed to read saved locale from localStorage', error);
|
2024-04-19 05:45:48 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-09 11:18:22 +01:00
|
|
|
const cookieMatch = document.cookie.match(new RegExp(`(?:^|; )${LOCALE_COOKIE_KEY}=([^;]*)`));
|
|
|
|
|
return cookieMatch?.[1] ? decodeURIComponent(cookieMatch[1]) : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function persistLocale(newLocale: TLocale) {
|
|
|
|
|
if (typeof window === 'undefined') {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
window.localStorage.setItem(LOCALE_STORAGE_KEY, newLocale);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.warn('Failed to save locale to localStorage', error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
document.cookie = `${LOCALE_COOKIE_KEY}=${encodeURIComponent(newLocale)}; path=/; max-age=31536000; SameSite=Lax`;
|
2024-04-19 05:45:48 +01:00
|
|
|
}
|