Polish UI, harden company creation, and add error pages
This commit is contained in:
@@ -1,14 +1,21 @@
|
||||
import React, { createContext, useContext, useState } from "react";
|
||||
import { translations, TranslationKey, UiLanguage } from "./translations";
|
||||
|
||||
type TranslationParams = Record<string, string | number>;
|
||||
|
||||
type Ctx = {
|
||||
language: UiLanguage;
|
||||
setLanguage: (l: UiLanguage) => void;
|
||||
t: (key: TranslationKey) => string;
|
||||
t: (key: TranslationKey, params?: TranslationParams) => string;
|
||||
};
|
||||
|
||||
const I18nContext = createContext<Ctx | null>(null);
|
||||
|
||||
function interpolate(template: string, params?: TranslationParams) {
|
||||
if (!params) return template;
|
||||
return template.replace(/\{(\w+)\}/g, (_, key) => String(params[key] ?? `{${key}}`));
|
||||
}
|
||||
|
||||
export function I18nProvider({ children }: { children: React.ReactNode }) {
|
||||
const [language, setLanguageState] = useState<UiLanguage>(() => {
|
||||
const raw = window.localStorage.getItem("uiLanguage");
|
||||
@@ -20,7 +27,7 @@ export function I18nProvider({ children }: { children: React.ReactNode }) {
|
||||
window.localStorage.setItem("uiLanguage", l);
|
||||
};
|
||||
|
||||
const t = (key: TranslationKey) => translations[language][key] ?? translations.en[key];
|
||||
const t = (key: TranslationKey, params?: TranslationParams) => interpolate(translations[language][key] ?? translations.en[key], params);
|
||||
|
||||
return <I18nContext.Provider value={{ language, setLanguage, t }}>{children}</I18nContext.Provider>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user