|
12 | 12 | * |
13 | 13 | * @module @lingo.dev/compiler/react (server) |
14 | 14 | */ |
15 | | - |
16 | | -import type { ReactNode } from "react"; |
17 | | -import { use } from "react"; |
18 | | -import { getServerLocale } from "@lingo.dev/compiler/locale/server"; |
19 | | -import { fetchTranslationsOnServer } from "../server-only/translations"; |
20 | | -import { |
21 | | - renderRichText, |
22 | | - type RichTextParams, |
23 | | -} from "../shared/render-rich-text"; |
24 | | -import { logger } from "../../utils/logger"; |
25 | | -import type { TranslationHook } from "../types"; |
26 | | - |
27 | | -// TODO (AleksandrSl 01/12/2025): Should we add back the cache? |
28 | | -const getTranslations = async (hashes: string[]) => { |
29 | | - // 1. Resolve locale (framework-specific) |
30 | | - const locale = await getServerLocale(); |
31 | | - |
32 | | - // 2. Fetch translations (universal) |
33 | | - const translations = await fetchTranslationsOnServer(locale, hashes); |
34 | | - |
35 | | - logger.debug( |
36 | | - `Server. The translations for locale ${locale} are: ${JSON.stringify(translations)}`, |
37 | | - ); |
38 | | - |
39 | | - return { |
40 | | - locale, |
41 | | - translations, |
42 | | - }; |
43 | | -}; |
44 | | - |
45 | | -/** |
46 | | - * Server-side translation hook |
47 | | - * |
48 | | - * Works in Server Components WITHOUT async/await! |
49 | | - * Uses React's use() hook to unwrap the cached promise. |
50 | | - * |
51 | | - * This hook has the SAME signature as the client-side version, |
52 | | - * making components truly isomorphic. |
53 | | - * |
54 | | - * @param hashes - List of translation hashes used in component (injected by compiler) |
55 | | - * @returns Translation function |
56 | | - * |
57 | | - * @example |
58 | | - * ```tsx |
59 | | - * // Works in Server Components (no async needed!) |
60 | | - * export default function ServerPage() { |
61 | | - * const t = useTranslation(['hash_abc', 'hash_def']); |
62 | | - * return <h1>{t('hash_abc', 'Welcome')}</h1>; |
63 | | - * } |
64 | | - * |
65 | | - * // Also works in Client Components (via conditional exports) |
66 | | - * 'use client'; |
67 | | - * export default function ClientPage() { |
68 | | - * const t = useTranslation(['hash_abc', 'hash_def']); |
69 | | - * return <h1>{t('hash_abc', 'Welcome')}</h1>; |
70 | | - * } |
71 | | - * ``` |
72 | | - */ |
73 | | -export const useTranslation: TranslationHook = (hashes: string[]) => { |
74 | | - const { locale, translations } = use(getTranslations(hashes)); |
75 | | - logger.debug( |
76 | | - `Server. The translations for locale ${locale} are: ${JSON.stringify(translations)}`, |
77 | | - ); |
78 | | - |
79 | | - return { |
80 | | - t: ( |
81 | | - hash: string, |
82 | | - source: string, |
83 | | - params?: RichTextParams, |
84 | | - ): string | ReactNode => { |
85 | | - const text = translations[hash] || source; |
86 | | - |
87 | | - if (!params) { |
88 | | - return text; |
89 | | - } |
90 | | - |
91 | | - return renderRichText(text, params); |
92 | | - }, |
93 | | - locale, |
94 | | - }; |
95 | | -}; |
96 | | - |
97 | 15 | export { TranslationProvider } from "./ServerTranslationProvider"; |
98 | 16 | export { LocaleSwitcher } from "../shared/LocaleSwitcher"; |
| 17 | +export { useTranslation } from "./useTranslation"; |
0 commit comments