diff --git a/src/i18n.ts b/src/i18n.ts index d86ca05..1cd0140 100644 --- a/src/i18n.ts +++ b/src/i18n.ts @@ -1,64 +1,64 @@ import { ref, computed } from 'vue'; import type en from '../i18n/en.json'; export type MessageKey = Exclude; // Auto-discovers every json file in the i18n/ folder — no manual list to maintain. // Adding i18n/ru.json (etc) is enough; this picks it up automatically. const modules = import.meta.glob('../i18n/*.json', { eager: true }) as Record< string, { default: Record } >; const messageFiles: Record> = {}; for (const path in modules) { const match = path.match(/\/([\w-]+)\.json$/); - if (match && match[1] !== 'qqq') { + if (match && match[1] !== 'qqq' && match[1] !== 'languages') { messageFiles[match[1]] = modules[path].default; } } const STORAGE_KEY = 'wikinewsie-locale'; function getInitialLocale(): string { try { - return localStorage.getItem(STORAGE_KEY) || 'en'; + return localStorage.getItem(STORAGE_KEY) || navigator.language || 'en'; } catch { return 'en'; } } export const currentLocale = ref(getInitialLocale()); export function setLocale(code: string) { currentLocale.value = code; try { localStorage.setItem(STORAGE_KEY, code); } catch { // localStorage unavailable (private mode, etc) — just skip saving } } const activeMessages = computed(() => { return messageFiles[currentLocale.value] || messageFiles.en; }); function getMessage(key: MessageKey): string { return (activeMessages.value[key] ?? messageFiles.en[key]) as string; } // for placeholders export function text(key: MessageKey, ...args: (string | number)[]): string { return getMessage(key).replace(/\$(\d+)/g, (match, n) => { const arg = args[Number(n) - 1]; return arg === undefined ? match : String(arg); }); } // for links export function messageParts(key: MessageKey): (string | { slot: string })[] { return getMessage(key) .split(/\{([\w-]+)\}/g) .map((part, i) => (i % 2 === 1 ? { slot: part } : part)) .filter((part) => part !== ''); } \ No newline at end of file