Redesign whole website #15

Merged
leomurca merged 27 commits from redesign into development 2025-04-12 18:03:34 +00:00
Showing only changes of commit 44853095b2 - Show all commits

View file

@ -1,23 +1,35 @@
import { derived, writable } from "svelte/store"; import { derived, writable } from "svelte/store";
import translations from "./translations"; import translations from "./translations";
const storedLocale = localStorage.getItem("locale");
const browserLocale = navigator.language || "en"; const browserLocale = navigator.language || "en";
const [baseLang, region] = browserLocale.split("-"); const [baseLang] = browserLocale.split("-");
export const DEFAULT_LOCALE =
storedLocale && translations[storedLocale] ? storedLocale :
translations[browserLocale] ? browserLocale :
translations[baseLang] ? baseLang :
"en";
export const DEFAULT_LOCALE = translations[browserLocale] ? browserLocale : translations[baseLang] ? baseLang :"en";
export const locale = writable(DEFAULT_LOCALE); export const locale = writable(DEFAULT_LOCALE);
locale.subscribe((value) => {
if (value) localStorage.setItem("locale", value);
});
export const locales = Object.entries(translations).map(([key, lang]) => [key, lang.name]); export const locales = Object.entries(translations).map(([key, lang]) => [key, lang.name]);
function translate(locale, key, vars = {}) { function translate(locale, key, vars = {}) {
if (!key) throw new Error("Translation key is required."); if (!key) throw new Error("Translation key is required.");
const fallbackLocale = "en";
const validLocale = translations[locale] const validLocale = translations[locale]
? locale ? locale
: translations[baseLang] : translations[baseLang]
? baseLang ? baseLang
: "en"; : fallbackLocale;
let text = translations[validLocale][key] || translations["en"][key]; let text = translations[validLocale][key] || translations[fallbackLocale][key];
if (!text) { if (!text) {
console.error(`Missing translation for key "${key}" in locale "${validLocale}".`); console.error(`Missing translation for key "${key}" in locale "${validLocale}".`);
@ -32,4 +44,4 @@ function translate(locale, key, vars = {}) {
export const t = derived(locale, ($locale) => (key, vars = {}) => export const t = derived(locale, ($locale) => (key, vars = {}) =>
translate($locale, key, vars) translate($locale, key, vars)
); );