SPB Git

Repository Public

index.tsx 1000 B 24 lines tsx

1"use client";2import { createContext, useContext } from "react";34export type Locale = "fr" | "en";5const LocaleContext = createContext<Locale>("fr");6export const LocaleProvider = ({ locale, children }: { locale: Locale; children: React.ReactNode }) => <LocaleContext.Provider value={locale}>{children}</LocaleContext.Provider>;7export const useLocale = () => useContext(LocaleContext);89/** Each area owns one dictionary file: `{ fr: {...}, en: {...} }` with the same keys. */10export function useT<D extends Record<string, string>>(dict: { fr: D; en: D }) {11  const locale = useLocale();12  const d = dict[locale];13  return (key: keyof D, vars?: Record<string, string | number>) => {14    let s: string = d[key] ?? String(key);15    if (vars) for (const [k, v] of Object.entries(vars)) s = s.replaceAll(`{${k}}`, String(v));16    return s;17  };18}1920export function setLocaleCookie(l: Locale) {21  document.cookie = `spcal_lang=${l}; path=/; max-age=31536000; samesite=lax`;22  window.location.reload();23}24
Files main
Menu

Theme