SPB Git

Repository Public

LangContext.tsx 3.0 KB 87 lines tsx

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : Vrai-Prix8 * File             : src/components/LangContext.tsx9 * Module           : components10 * Repository       : https://git.spboucher.ai/vrai-prix11 * Created          : 2026-08-0912 * Last Modified    : 2026-08-2513 *14 * Author           : Simon-Pierre Boucher15 * Position         : Professor of Finance16 * Institution      : Université du Québec en Outaouais (UQO)17 * Organization     : SP Boucher18 * Website          : https://spboucher.ai19 * Git              : https://git.spboucher.ai20 *21 * Preferred Contact: contact@spboucher.ai22 * Development      : dev@spboucher.ai23 * Support          : support@spboucher.ai24 * Security/Admin   : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License          : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142"use client";43import { createContext, useContext, useEffect, useState } from "react";44import { t as translate, type Lang, type TKey } from "@/lib/i18n";4546const Ctx = createContext<{47  lang: Lang;48  setLang: (l: Lang) => void;49  t: (k: TKey) => string;50}>({ lang: "fr", setLang: () => {}, t: (k) => translate("fr", k) });5152export function LangProvider({ children }: { children: React.ReactNode }) {53  const [lang, setLangState] = useState<Lang>("fr");54  useEffect(() => {55    const saved = window.localStorage.getItem("vp-lang");56    if (saved !== "en" && saved !== "fr") return;57    const id = setTimeout(() => setLangState(saved), 0);58    return () => clearTimeout(id);59  }, []);60  const setLang = (l: Lang) => {61    setLangState(l);62    window.localStorage.setItem("vp-lang", l);63  };64  return (65    <Ctx.Provider value={{ lang, setLang, t: (k) => translate(lang, k) }}>66      {children}67    </Ctx.Provider>68  );69}7071export function useLang() {72  return useContext(Ctx);73}7475export function LangToggle() {76  const { lang, setLang } = useLang();77  return (78    <button79      onClick={() => setLang(lang === "fr" ? "en" : "fr")}80      className="vp-mono min-h-[36px] border-b-2 border-transparent px-1 py-1.5 text-[11px] font-medium uppercase tracking-[0.1em] text-ink-2 transition-colors hover:border-accent hover:text-ink"81      aria-label="Changer de langue / Switch language"82    >83      {lang === "fr" ? "EN" : "FR"}84    </button>85  );86}87
Files main
Menu

Theme