lang changes, new pages, data changes

This commit is contained in:
Henri
2026-02-12 05:27:53 +02:00
parent 1504844f0b
commit 9d13b57d2e
268 changed files with 3310 additions and 1541 deletions

View File

@@ -1,26 +1,52 @@
import { writable } from 'svelte/store';
import { getLanguageFromRoute } from '../routes';
// Load saved language or default to 'est'
const savedLang = typeof window !== 'undefined'
? localStorage.getItem('language') || 'est'
: 'est';
// Get initial language from URL or localStorage
function getInitialLanguage() {
if (typeof window === 'undefined') return 'est';
const path = window.location.pathname;
const langFromRoute = getLanguageFromRoute(path);
// If we detected a language from route, use it and save it
if (langFromRoute) {
localStorage.setItem('language', langFromRoute);
return langFromRoute;
}
// Otherwise use saved language or default to 'est'
return localStorage.getItem('language') || 'est';
}
export const currentLang = writable(savedLang);
const initialLang = getInitialLanguage();
export const currentLang = writable(initialLang);
// Store for translations
export const text = writable({});
async function loadTranslations(lang) {
try {
const response = await fetch(`/locales/${lang}.json`);
const data = await response.json();
text.set(data);
const translations = {};
// 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('/src/locales/*/*.json');
for (const path in modules) {
if (path.includes(`/locales/${lang}/`)) {
const module = await modules[path]();
Object.assign(translations, module.default || module);
}
}
text.set(translations);
} catch (error) {
console.error(`Failed to load translations for ${lang}:`, error);
}
}
loadTranslations(savedLang);
loadTranslations(initialLang);
export function switchLang(lang) {
currentLang.set(lang);
@@ -28,4 +54,15 @@ export function switchLang(lang) {
if (typeof window !== 'undefined') {
localStorage.setItem('language', lang);
}
}
// Update language when route changes
if (typeof window !== 'undefined') {
window.addEventListener('popstate', () => {
const path = window.location.pathname;
const lang = getLanguageFromRoute(path);
if (lang) {
switchLang(lang);
}
});
}

View File

@@ -1,4 +1,5 @@
export { navigate, goBack, reload, getPath, getQuery } from './router/router.js';
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 } from './i18n.js';
export { getLangText } from './langHelpers.js';

17
src/lib/langHelpers.js Normal file
View File

@@ -0,0 +1,17 @@
// Helper function to get text with fallback to Estonian
export function getLangText(item, key, lang = 'est') {
const value = item[key];
// If it's a simple string, return it
if (typeof value === 'string') {
return value;
}
// If it's an object with language keys
if (value && typeof value === 'object') {
// Try to get the requested language, fallback to Estonian
return value[lang] || value.est || '';
}
return '';
}

View File

@@ -1,14 +1,43 @@
import { getTranslatedRoute, getLanguageFromRoute } from '../../routes';
import { switchLang } from '../i18n.js';
/**
* Navigate to a path (internal or external)
* @param {string} path - The path or URL to navigate to
* @param {boolean} external - Whether this is an external link (default: false)
* @param {Object} options - Navigation options
* @param {boolean} options.external - Whether this is an external link
*/
export function navigate(path, external = false) {
export function navigate(path, options = {}) {
const { external = false } = options;
if (external) {
window.location.href = path;
} else {
window.history.pushState({}, "", path);
window.dispatchEvent(new PopStateEvent('popstate'));
// Update language based on new route
const lang = getLanguageFromRoute(path);
if (lang) {
switchLang(lang);
}
}
}
/**
* Switch language and navigate to translated route
* @param {string} targetLang - Target language ('est' or 'en')
*/
export function switchLanguageRoute(targetLang) {
const currentPath = window.location.pathname;
const translatedPath = getTranslatedRoute(currentPath, targetLang);
// Always update the language first
switchLang(targetLang);
// Only navigate if the path is different
if (translatedPath !== currentPath) {
navigate(translatedPath);
}
}