fixed language issues, updated routing, completed first version of managment page

This commit is contained in:
Henri
2026-04-07 23:55:38 +03:00
parent ae1a44b65f
commit 3d27217769
11 changed files with 208 additions and 558 deletions

View File

@@ -1,6 +1,8 @@
import { writable } from 'svelte/store';
import { getLanguageFromRoute } from '../routes';
const localeModules = import.meta.glob('./locales/*/*.json');
// Get initial language from URL or localStorage
function getInitialLanguage() {
if (typeof window === 'undefined') return 'est';
@@ -31,11 +33,9 @@ async function loadTranslations(lang) {
// Dynamically import all translation files for the specific language
// Vite will code-split these and only bundle the ones actually used
const modules = import.meta.glob('./locales/*/*.json');
for (const path in modules) {
for (const path in localeModules) {
if (path.includes(`/locales/${lang}/`) || path.includes(`\\locales\\${lang}\\`)) {
const module = await modules[path]();
const module = await localeModules[path]();
Object.assign(translations, module.default || module);
}
}
@@ -48,6 +48,41 @@ async function loadTranslations(lang) {
loadTranslations(initialLang);
/**
* Create a translation store that only loads one page file per language.
* Example: createPageTextStore('Home') -> ./locales/est/Home.json or ./locales/en/Home.json
*/
export function createPageTextStore(pageName) {
const pageText = writable({});
async function loadPageTranslations(lang) {
try {
const filePath = `./locales/${lang}/${pageName}.json`;
const loader = localeModules[filePath];
if (!loader) {
pageText.set({});
return;
}
const module = await loader();
pageText.set(module.default || module);
} catch (error) {
console.error(`Failed to load ${pageName} translations for ${lang}:`, error);
pageText.set({});
}
}
const unsubscribe = currentLang.subscribe((lang) => {
loadPageTranslations(lang);
});
return {
subscribe: pageText.subscribe,
destroy: unsubscribe,
};
}
export function switchLang(lang) {
currentLang.set(lang);
loadTranslations(lang);

View File

@@ -1,5 +1,5 @@
export { navigate, goBack, reload, getPath, getQuery, switchLanguageRoute } from './router/router.js';
export { default as Router } from './router/Router.svelte';
export { currentLang, text, switchLang } from './i18n.js';
export { currentLang, text, switchLang, createPageTextStore } from './i18n.js';
export { getLangText } from './langHelpers.js';

View File

@@ -5,8 +5,7 @@
"intro": "People who keep the organisation moving forward."
},
"current": {
"label": "Current board",
"hint": "Contacts and responsibilities"
"label": "Current board"
},
"history": {
"label": "People who have led the organisation over time",

View File

@@ -8,11 +8,11 @@
},
"events": "Events",
"eventsPages": {
"calendar": "Events Calendar",
"workshops": "Workshops",
"fresh": "Freshmen Initiation",
"repair": "Repair Café"
"tipilan": "TipiLAN",
"asi": "ASI Karikas",
"repair": "Remondikohvik"
},
"contact": "Contact",
"helpdesk": "Helpdesk"
"helpdesk": "Helpdesk",
"contact": "Contact"
}

View File

@@ -5,8 +5,7 @@
"intro": "Inimesed, kes viivad organisatsiooni edasi."
},
"current": {
"label": "Praegune juhtkond",
"hint": "Kontaktid ja vastutusvaldkonnad"
"label": "Praegune juhtkond"
},
"history": {
"label": "Organisatsiooni juhtinud inimesed läbi aegade",

View File

@@ -8,11 +8,11 @@
},
"events": "Üritused",
"eventsPages": {
"calendar": "Ürituste Kalender",
"workshops": "Koolitused",
"fresh": "Rebaste Ristimine",
"tipilan": "TipiLAN",
"asi": "ASI Karikas",
"repair": "Remondikohvik"
},
"contact": "Kontakt",
"helpdesk": "Helpdesk"
"helpdesk": "Helpdesk",
"contact": "Kontakt"
}

View File

@@ -13,11 +13,18 @@ export function navigate(path, options = {}) {
if (external) {
window.open(path, '_blank', 'noopener,noreferrer');
} else {
window.history.pushState({}, "", path);
const selectedLang =
localStorage.getItem('language') ||
getLanguageFromRoute(window.location.pathname) ||
'est';
const resolvedPath = getTranslatedRoute(path, selectedLang);
window.history.pushState({}, "", resolvedPath);
window.dispatchEvent(new PopStateEvent('popstate'));
// Update language based on new route
const lang = getLanguageFromRoute(path);
const lang = getLanguageFromRoute(resolvedPath);
if (lang) {
switchLang(lang);
}