Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : MacLustr Command Center (MCC)8 * File : apps/web/src/i18n/index.tsx9 * Module : i18n10 * Repository : https://git.spboucher.ai/maclustr-command-center11 * Project Website : https://clustr.spboucher.ai12 * Created : 2026-11-1813 * Last Modified : 2026-11-1814 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: admin@spboucher.ai23 * Technical Contact: dev@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, useCallback, useContext, useEffect, useMemo, useState, type ReactNode } from "react";44import * as F from "@/lib/format";45import type { Locale } from "@/lib/format";46import { writeUiCookie } from "@/lib/prefs";47import { translate, translatePlural } from "./core";48import type { TKey, TPluralKey, Vars } from "./types";4950export type { Locale, TKey, TPluralKey };5152type Tail<T extends unknown[]> = T extends [unknown, ...infer R] ? R : never;53type Bound<Fn> = Fn extends (...a: infer A) => infer R ? (...a: Tail<A>) => R : never;5455export interface I18n {56 locale: Locale;57 setLocale: (l: Locale) => void;58 t: (key: TKey, vars?: Vars) => string;59 tn: (key: TPluralKey, count: number, vars?: Vars) => string;60 fmt: {61 number: Bound<typeof F.formatNumber>;62 integer: Bound<typeof F.formatInteger>;63 percent: Bound<typeof F.formatPercent>;64 ratio: Bound<typeof F.formatRatio>;65 bytes: Bound<typeof F.formatBytes>;66 byteRate: Bound<typeof F.formatByteRate>;67 ms: Bound<typeof F.formatMs>;68 duration: Bound<typeof F.formatDuration>;69 dateTime: Bound<typeof F.formatDateTime>;70 utc: Bound<typeof F.formatUtc>;71 date: Bound<typeof F.formatDate>;72 time: Bound<typeof F.formatTime>;73 relative: Bound<typeof F.formatRelative>;74 metric: Bound<typeof F.formatMetric>;75 };76}7778const Ctx = createContext<I18n | null>(null);7980export function I18nProvider({ initialLocale, children }: { initialLocale: Locale; children: ReactNode }) {81 const [locale, setLocaleState] = useState<Locale>(initialLocale);82 const setLocale = useCallback((l: Locale) => {83 setLocaleState(l);84 writeUiCookie("mcc_locale", l);85 }, []);86 useEffect(() => {87 document.documentElement.lang = locale;88 }, [locale]);89 const value = useMemo<I18n>(90 () => ({91 locale,92 setLocale,93 t: (key, vars) => translate(locale, key, vars),94 tn: (key, count, vars) => translatePlural(locale, key, count, vars),95 fmt: {96 number: (...a) => F.formatNumber(locale, ...a),97 integer: (...a) => F.formatInteger(locale, ...a),98 percent: (...a) => F.formatPercent(locale, ...a),99 ratio: (...a) => F.formatRatio(locale, ...a),100 bytes: (...a) => F.formatBytes(locale, ...a),101 byteRate: (...a) => F.formatByteRate(locale, ...a),102 ms: (...a) => F.formatMs(locale, ...a),103 duration: (...a) => F.formatDuration(locale, ...a),104 dateTime: (...a) => F.formatDateTime(locale, ...a),105 utc: (...a) => F.formatUtc(locale, ...a),106 date: (...a) => F.formatDate(locale, ...a),107 time: (...a) => F.formatTime(locale, ...a),108 relative: (...a) => F.formatRelative(locale, ...a),109 metric: (...a) => F.formatMetric(locale, ...a),110 },111 }),112 [locale, setLocale],113 );114 return <Ctx.Provider value={value}>{children}</Ctx.Provider>;115}116117export function useI18n(): I18n {118 const v = useContext(Ctx);119 if (!v) throw new Error("useI18n outside I18nProvider");120 return v;121}122