Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

feat(web): add i18next, locale hook, and Vite i18n integration

Tin860338571830
diff --git a/apps/web/index.html b/apps/web/index.html
index 598ba3712..975e6dfd8 100644
--- a/apps/web/index.html
+++ b/apps/web/index.html
@@ -1,5 +1,5 @@
<!doctype html>
-<html lang="en">
+<html lang="en-US">
<head>
<meta charset="UTF-8" />
diff --git a/apps/web/package.json b/apps/web/package.json
index 129360792..a84f45738 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -73,6 +73,7 @@
"hono": "^4.12.4",
"immer": "^11.1.4",
"input-otp": "^1.4.2",
+ "i18next": "^25.5.3",
"lowlight": "^3.3.0",
"lucide-react": "^0.577.0",
"marked": "^17.0.4",
@@ -81,6 +82,7 @@
"react-day-picker": "9.14.0",
"react-dom": "^19.2.4",
"react-hook-form": "^7.71.2",
+ "react-i18next": "^15.7.3",
"react-markdown": "^10.1.0",
"react-use-websocket": "^4.11.1",
"shiki": "^4.0.2",
diff --git a/apps/web/src/hooks/use-locale.ts b/apps/web/src/hooks/use-locale.ts
new file mode 100644
index 000000000..50faa422e
--- /dev/null
+++ b/apps/web/src/hooks/use-locale.ts
@@ -0,0 +1,27 @@
+import { useMemo } from "react";
+import { useTranslation } from "react-i18next";
+import { authClient } from "@/lib/auth-client";
+import { getBrowserLocale, resolveLocale } from "@/lib/i18n";
+
+export function useLocale() {
+ const { i18n } = useTranslation();
+
+ const locale = useMemo(
+ () => resolveLocale(i18n.resolvedLanguage, getBrowserLocale()),
+ [i18n.resolvedLanguage],
+ );
+
+ const setLocale = async (nextLocale: string) => {
+ const { error } = await authClient.updateUser({ locale: nextLocale });
+ if (error) {
+ throw new Error(error.message || "Failed to update locale");
+ }
+
+ await i18n.changeLanguage(resolveLocale(nextLocale, null));
+ };
+
+ return {
+ locale,
+ setLocale,
+ };
+}
diff --git a/apps/web/src/lib/format.ts b/apps/web/src/lib/format.ts
new file mode 100644
index 000000000..9bbaca9dd
--- /dev/null
+++ b/apps/web/src/lib/format.ts
@@ -0,0 +1,77 @@
+import { i18n } from "./i18n";
+
+type DateInput = Date | string | number;
+
+function toDate(input: DateInput) {
+ return input instanceof Date ? input : new Date(input);
+}
+
+function getLocale(locale?: string) {
+ return locale || i18n.resolvedLanguage || i18n.language || "en-US";
+}
+
+export function formatDate(
+ value: DateInput,
+ options?: Intl.DateTimeFormatOptions,
+ locale?: string,
+) {
+ return new Intl.DateTimeFormat(getLocale(locale), options).format(
+ toDate(value),
+ );
+}
+
+export function formatDateShort(value: DateInput, locale?: string) {
+ return formatDate(
+ value,
+ {
+ month: "short",
+ day: "numeric",
+ },
+ locale,
+ );
+}
+
+export function formatDateMedium(value: DateInput, locale?: string) {
+ return formatDate(
+ value,
+ {
+ month: "short",
+ day: "numeric",
+ year: "numeric",
+ },
+ locale,
+ );
+}
+
+export function formatRelativeTime(
+ value: DateInput,
+ locale?: string,
+ now = new Date(),
+) {
+ const target = toDate(value);
+ const diffMs = target.getTime() - now.getTime();
+ const diffSeconds = Math.round(diffMs / 1000);
+ const absSeconds = Math.abs(diffSeconds);
+
+ const units: Array<[Intl.RelativeTimeFormatUnit, number]> = [
+ ["year", 60 * 60 * 24 * 365],
+ ["month", 60 * 60 * 24 * 30],
+ ["week", 60 * 60 * 24 * 7],
+ ["day", 60 * 60 * 24],
+ ["hour", 60 * 60],
+ ["minute", 60],
+ ["second", 1],
+ ];
+
+ const formatter = new Intl.RelativeTimeFormat(getLocale(locale), {
+ numeric: "auto",
+ });
+
+ for (const [unit, unitSeconds] of units) {
+ if (absSeconds >= unitSeconds || unit === "second") {
+ return formatter.format(Math.round(diffSeconds / unitSeconds), unit);
+ }
+ }
+
+ return formatter.format(0, "second");
+}
diff --git a/apps/web/src/lib/i18n/domain.ts b/apps/web/src/lib/i18n/domain.ts
new file mode 100644
index 000000000..0e669715c
--- /dev/null
+++ b/apps/web/src/lib/i18n/domain.ts
@@ -0,0 +1,22 @@
+import i18n from "i18next";
+
+export function getStatusLabel(status: string) {
+ return i18n.t(`tasks:status.${status}`, {
+ defaultValue: toDisplayCase(status),
+ });
+}
+
+export function getPriorityLabel(priority: string) {
+ return i18n.t(`tasks:priority.${priority}`, {
+ defaultValue: toDisplayCase(priority),
+ });
+}
+
+function toDisplayCase(value: string) {
+ return value
+ .replace(/[-_]/g, " ")
+ .split(" ")
+ .filter(Boolean)
+ .map((part) => part.charAt(0).toUpperCase() + part.slice(1).toLowerCase())
+ .join(" ");
+}
diff --git a/apps/web/src/lib/i18n/index.ts b/apps/web/src/lib/i18n/index.ts
new file mode 100644
index 000000000..48d444c1f
--- /dev/null
+++ b/apps/web/src/lib/i18n/index.ts
@@ -0,0 +1,51 @@
+import {
+ type AppLocale,
+ defaultLocale,
+ resources,
+ supportedLocales,
+} from "@i18n/resources";
+import i18n from "i18next";
+import { initReactI18next } from "react-i18next";
+
+function getLanguageCode(locale: string) {
+ return locale.toLowerCase().split("-")[0];
+}
+
+export function resolveLocale(
+ preferredLocale?: string | null,
+ browserLocale?: string | null,
+): AppLocale {
+ const candidates = [preferredLocale, browserLocale]
+ .filter(Boolean)
+ .map((value) => value?.toLowerCase());
+
+ for (const candidate of candidates) {
+ if (!candidate) continue;
+ const exactMatch = supportedLocales.find((locale) => locale === candidate);
+ if (exactMatch) return exactMatch;
+
+ const languageMatch = supportedLocales.find(
+ (locale) => getLanguageCode(locale) === getLanguageCode(candidate),
+ );
+ if (languageMatch) return languageMatch;
+ }
+
+ return defaultLocale;
+}
+
+export function getBrowserLocale(): string | null {
+ if (typeof navigator === "undefined") return null;
+ return navigator.language || navigator.languages?.[0] || null;
+}
+
+void i18n.use(initReactI18next).init({
+ resources,
+ lng: resolveLocale(null, getBrowserLocale()),
+ fallbackLng: defaultLocale,
+ defaultNS: "common",
+ interpolation: {
+ escapeValue: false,
+ },
+});
+
+export { i18n };
diff --git a/apps/web/src/lib/i18n/provider.tsx b/apps/web/src/lib/i18n/provider.tsx
new file mode 100644
index 000000000..ca54ed636
--- /dev/null
+++ b/apps/web/src/lib/i18n/provider.tsx
@@ -0,0 +1,20 @@
+import { type PropsWithChildren, useEffect, useMemo } from "react";
+import { I18nextProvider } from "react-i18next";
+import useAuth from "@/components/providers/auth-provider/hooks/use-auth";
+import { getBrowserLocale, i18n, resolveLocale } from "./index";
+
+export function AppI18nProvider({ children }: PropsWithChildren) {
+ const { user } = useAuth();
+
+ const resolvedLocale = useMemo(
+ () => resolveLocale(user?.locale, getBrowserLocale()),
+ [user?.locale],
+ );
+
+ useEffect(() => {
+ void i18n.changeLanguage(resolvedLocale);
+ document.documentElement.lang = resolvedLocale;
+ }, [resolvedLocale]);
+
+ return <I18nextProvider i18n={i18n}>{children}</I18nextProvider>;
+}
diff --git a/apps/web/src/main.tsx b/apps/web/src/main.tsx
index 30c2bae8f..a5cade413 100644
--- a/apps/web/src/main.tsx
+++ b/apps/web/src/main.tsx
@@ -9,6 +9,7 @@ import { KeyboardShortcutsHelp } from "./components/keyboard-shortcuts-help";
import AuthProvider from "./components/providers/auth-provider";
import { ThemeProvider } from "./components/providers/theme-provider";
import { KeyboardShortcutsProvider } from "./hooks/use-keyboard-shortcuts";
+import { AppI18nProvider } from "./lib/i18n/provider";
import { routeTree } from "./routeTree.gen";
console.log(`
@@ -56,10 +57,12 @@ if (!rootElement.innerHTML) {
<QueryClientProvider client={queryClient}>
<ThemeProvider>
<AuthProvider>
- <KeyboardShortcutsProvider>
- <App />
- <KeyboardShortcutsHelp />
- </KeyboardShortcutsProvider>
+ <AppI18nProvider>
+ <KeyboardShortcutsProvider>
+ <App />
+ <KeyboardShortcutsHelp />
+ </KeyboardShortcutsProvider>
+ </AppI18nProvider>
</AuthProvider>
</ThemeProvider>
</QueryClientProvider>
diff --git a/apps/web/tsconfig.app.json b/apps/web/tsconfig.app.json
index 9c5082d35..e01b6c83e 100644
--- a/apps/web/tsconfig.app.json
+++ b/apps/web/tsconfig.app.json
@@ -9,6 +9,7 @@
/* Bundler mode */
"moduleResolution": "bundler",
+ "resolveJsonModule": true,
"allowImportingTsExtensions": true,
"isolatedModules": true,
"moduleDetection": "force",
@@ -22,10 +23,11 @@
"noFallthroughCasesInSwitch": true,
"noUncheckedSideEffectImports": true,
"paths": {
- "@/*": ["./src/*"]
+ "@/*": ["./src/*"],
+ "@i18n/*": ["../../i18n/*"]
}
},
- "include": ["src"],
+ "include": ["src", "../../i18n"],
"exclude": ["../api/**/*", "../docs/**/*", "../../packages/**/*"],
"baseUrl": "."
}
diff --git a/apps/web/tsconfig.json b/apps/web/tsconfig.json
index 29c26bfe0..5a29f4f8a 100644
--- a/apps/web/tsconfig.json
+++ b/apps/web/tsconfig.json
@@ -7,7 +7,8 @@
"compilerOptions": {
"baseUrl": ".",
"paths": {
- "@/*": ["./src/*"]
+ "@/*": ["./src/*"],
+ "@i18n/*": ["../../i18n/*"]
}
},
"exclude": ["../api/**/*", "../docs/**/*", "../../packages/**/*"]
diff --git a/apps/web/vite.config.ts b/apps/web/vite.config.ts
index 3f88976ff..b988b49c2 100644
--- a/apps/web/vite.config.ts
+++ b/apps/web/vite.config.ts
@@ -33,6 +33,7 @@ export default defineConfig({
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
+ "@i18n": path.resolve(__dirname, "../../i18n"),
},
},
build: {
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 82bb00fa9..dedb25db3 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -395,6 +395,9 @@ importers:
hono:
specifier: 4.12.7
version: 4.12.7
+ i18next:
+ specifier: ^25.5.3
+ version: 25.10.10([email protected])
immer:
specifier: ^11.1.4
version: 11.1.4
@@ -425,6 +428,9 @@ importers:
react-hook-form:
specifier: ^7.71.2
version: 7.71.2([email protected])
+ react-i18next:
+ specifier: ^15.7.3
react-markdown:
specifier: ^10.1.0
version: 10.1.0(@types/[email protected])([email protected])
@@ -907,6 +913,10 @@ packages:
resolution: {integrity: sha512-05WQkdpL9COIMz4LjTxGpPNCdlpyimKppYNoJ5Di5EUObifl8t4tuLuUBBZEpoLYOmfvIWrsp9fCl0HoPRVTdA==}
engines: {node: '>=6.9.0'}
+ '@babel/[email protected]':
+ resolution: {integrity: sha512-JiDShH45zKHWyGe4ZNVRrCjBz8Nh9TMmZG1kh4QTK8hCBTWBi8Da+i7s1fJw7/lYpM4ccepSNfqzZ/QvABBi5g==}
+ engines: {node: '>=6.9.0'}
+
resolution: {integrity: sha512-YA6Ma2KsCdGb+WC6UpBVFJGXL58MDA6oyONbjyF/+5sBgxY/dwkhLogbMT2GXXyU84/IhRw/2D1Os1B/giz+BQ==}
engines: {node: '>=6.9.0'}
@@ -5225,6 +5235,9 @@ packages:
resolution: {integrity: sha512-jq9l1DM0zVIvsm3lv9Nw9nlJnMNPOcAtsbsgiUhWcFzPE99Gvo6yRTlszSLLYacMeQ6quHD6hMfId8crVHvexw==}
engines: {node: '>=16.9.0'}
+ resolution: {integrity: sha512-KknJ50kTInJ7qIScF3jeaFRpMpE8/lfiTdzf/twXyPBLAGrLRTmkz3AdTnKeh40X8k9L2fdYwEp/42WGXIRGcg==}
+
resolution: {integrity: sha512-qY60FjREgVZL03vJU6IfMV4GDjGBIoOyvuFdpBDIX9yTlDw0TjxVBQp+P8NvpdIXNJvfWBTNul7fsAQJq2FNpg==}
engines: {node: '>=14'}
@@ -5262,6 +5275,14 @@ packages:
engines: {node: '>=18'}
hasBin: true
+ resolution: {integrity: sha512-cqUW2Z3EkRx7NqSyywjkgCLK7KLCL6IFVFcONG7nVYIJ3ekZ1/N5jUsihHV6Bq37NfhgtczxJcxduELtjTwkuQ==}
+ peerDependencies:
+ typescript: ^5 || ^6
+ peerDependenciesMeta:
+ typescript:
+ optional: true
+
resolution: {integrity: sha512-im9DjEDQ55s9fL4EYzOAv0yMqmMBSZp6G0VvFyTMPKWxiSBHUj9NW/qqLmXUwXrrM7AvqSlTCfvqRb0cM8yYqw==}
engines: {node: '>=0.10.0'}
@@ -6449,6 +6470,22 @@ packages:
peerDependencies:
react: ^16.8.0 || ^17 || ^18 || ^19
+ resolution: {integrity: sha512-nyU8iKNrI5uDJch0z9+Y5XEr34b0wkyYj3Rp+tfbahxtlswxSCjcUL9H0nqXo9IR3/t5Y5PKIA3fx3MfUyR9Xw==}
+ peerDependencies:
+ i18next: '>= 23.4.0'
+ react: '>= 16.8.0'
+ react-dom: '*'
+ react-native: '*'
+ typescript: ^5
+ peerDependenciesMeta:
+ react-dom:
+ optional: true
+ react-native:
+ optional: true
+ typescript:
+ optional: true
+
resolution: {integrity: sha512-qKxVopLT/TyA6BX3Ue5NwabOsAzm0Q7kAPwq6L+wWDwisYs7R8vZ0nRXqq6rkueboxpkjvLGU9fWifiX/ZZFxQ==}
peerDependencies:
@@ -7154,6 +7191,10 @@ packages:
yaml:
optional: true
+ resolution: {integrity: sha512-Dhxzh5HZuiHQhbvTW9AMetFfBHDMYpo23Uo9btPXgdYP+3T5S+p+jgNy7spra+veYhBP2dCSgxR/i2Y02h5/6w==}
+ engines: {node: '>=0.10.0'}
+
resolution: {integrity: sha512-dpojBhNsCNN7T82Tm7k26A6G9ML3NkhDsnw9n/eoxSRlVBB4CEtIQ/KTCLI2Fwf3ataSXRhYFkQi3SlnFwPvPQ==}
@@ -8000,6 +8041,8 @@ snapshots:
'@babel/[email protected]': {}
+ '@babel/[email protected]': {}
+
dependencies:
'@babel/code-frame': 7.29.0
@@ -13163,6 +13206,10 @@ snapshots:
+ dependencies:
+ void-elements: 3.1.0
+
dependencies:
'@selderee/plugin-htmlparser2': 0.11.0
@@ -13206,6 +13253,12 @@ snapshots:
+ dependencies:
+ '@babel/runtime': 7.29.2
+ optionalDependencies:
+ typescript: 5.8.3
+
dependencies:
safer-buffer: 2.1.2
@@ -14606,6 +14659,16 @@ snapshots:
dependencies:
react: 19.2.4
+ dependencies:
+ '@babel/runtime': 7.28.6
+ html-parse-stringify: 3.0.1
+ i18next: 25.10.10([email protected])
+ react: 19.2.4
+ optionalDependencies:
+ react-dom: 19.2.4([email protected])
+ typescript: 5.8.3
+
dependencies:
'@types/hast': 3.0.4
@@ -15459,6 +15522,8 @@ snapshots:
tsx: 4.21.0
yaml: 2.8.2
+