First Commit
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
import React, { createContext, useContext, useState } from "react";
|
||||
import { translations, TranslationKey, UiLanguage } from "./translations";
|
||||
|
||||
type Ctx = {
|
||||
language: UiLanguage;
|
||||
setLanguage: (l: UiLanguage) => void;
|
||||
t: (key: TranslationKey) => string;
|
||||
};
|
||||
|
||||
const I18nContext = createContext<Ctx | null>(null);
|
||||
|
||||
export function I18nProvider({ children }: { children: React.ReactNode }) {
|
||||
const [language, setLanguageState] = useState<UiLanguage>(() => {
|
||||
const raw = window.localStorage.getItem("uiLanguage");
|
||||
return raw === "no" ? "no" : "en";
|
||||
});
|
||||
|
||||
const setLanguage = (l: UiLanguage) => {
|
||||
setLanguageState(l);
|
||||
window.localStorage.setItem("uiLanguage", l);
|
||||
};
|
||||
|
||||
const t = (key: TranslationKey) => translations[language][key] ?? translations.en[key];
|
||||
|
||||
return <I18nContext.Provider value={{ language, setLanguage, t }}>{children}</I18nContext.Provider>;
|
||||
}
|
||||
|
||||
export function useI18n() {
|
||||
const ctx = useContext(I18nContext);
|
||||
if (!ctx) throw new Error("useI18n must be used within I18nProvider");
|
||||
return ctx;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
export type UiLanguage = "en" | "no";
|
||||
|
||||
export const translations = {
|
||||
en: {
|
||||
appTitle: "Job Tracker",
|
||||
dashboard: "Dashboard",
|
||||
jobApplications: "Job Applications",
|
||||
reminders: "Reminders",
|
||||
kanbanBoard: "Kanban Board",
|
||||
companies: "Companies",
|
||||
trash: "Trash",
|
||||
settings: "Settings",
|
||||
addJob: "Add Job",
|
||||
addJobApplication: "Add Job Application",
|
||||
company: "Company",
|
||||
location: "Location",
|
||||
},
|
||||
no: {
|
||||
appTitle: "Jobbsporing",
|
||||
dashboard: "Dashboard",
|
||||
jobApplications: "Jobbsøknader",
|
||||
reminders: "Påminnelser",
|
||||
kanbanBoard: "Kanban-tavle",
|
||||
companies: "Selskaper",
|
||||
trash: "Papirkurv",
|
||||
settings: "Innstillinger",
|
||||
addJob: "Legg til jobb",
|
||||
addJobApplication: "Legg til jobbsøknad",
|
||||
company: "Selskap",
|
||||
location: "Sted",
|
||||
},
|
||||
} as const;
|
||||
|
||||
export type TranslationKey = keyof typeof translations.en;
|
||||
|
||||
Reference in New Issue
Block a user