Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions view/hooks/use-translation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
'use client';

import { useEffect, useState } from 'react';
import { defaultLocale, locales } from '@/lib/i18n/config';
import { defaultLocale } from '@/lib/i18n/config';
import en from '@/lib/i18n/locales/en.json'

// Recursive way to infer types from nested json keys
type DeepKeyOf<T> = {
[K in keyof T & string]:
T[K] extends Record<string, any> ? `${K}` | `${K}.${DeepKeyOf<T[K]>}` : `${K}`;
}[keyof T & string];

// This will help us to make sure whatever keys that are entered in the t(``) string are correct,
// and enable autocompletion in editors
type translationKey = DeepKeyOf<typeof en>

export function useTranslation() {
const [translations, setTranslations] = useState<Record<string, any>>({});
Expand Down Expand Up @@ -31,7 +42,7 @@ export function useTranslation() {
loadTranslations();
}, []);

const t = (key: string, params?: Record<string, string>): string => {
const t = (key: translationKey, params?: Record<string, string>): string => {
if (isLoading) return key;

const keys = key.split('.');
Expand Down
Loading