|
1 | | -import { useRequestContext } from "@hono/hono/jsx-renderer"; |
2 | | -import { neverThrow } from "../common/neverThrow.ts"; |
| 1 | +import { currentLocalizedValuesStorage } from "./initHonolate.ts"; |
3 | 2 |
|
4 | 3 | /** |
5 | 4 | * @returns the current locale's localization map, mapping localization keys to their respective translations |
6 | 5 | */ |
7 | 6 | export function getLocalizationMap(): Record<string, string> { |
8 | | - const ctx = neverThrow(() => useRequestContext()); |
9 | | - if (ctx instanceof Error) { |
| 7 | + const localizationMap = currentLocalizedValuesStorage.getStore(); |
| 8 | + if (!localizationMap) { |
10 | 9 | console.warn( |
11 | 10 | new Error( |
12 | | - "t was called outside of a request context. Using default locale 'default'." + |
13 | | - "\nMake sure to wrap t calls within a Hono JSX Renderer context." + |
14 | | - "\nIf you're not in a functional component, use asFC() to wrap the parameter of c.render():" + |
15 | | - "\nc.render(asFC(() => <>{t`...`}</>))" + |
16 | | - "\ninstead of c.render(<>{t`...`}</>)", |
17 | | - { cause: ctx }, |
| 11 | + "t was called outside of a request context." + |
| 12 | + "\nMake sure to only call t within routes that use the middleware returned by initHonolate.", |
18 | 13 | ), |
19 | 14 | ); |
20 | 15 | return {}; |
21 | 16 | } |
22 | | - |
23 | | - const map = ctx.get("localizedValues"); |
24 | | - if (!map || typeof map !== "object") { |
25 | | - console.warn( |
26 | | - new Error( |
27 | | - "Localized values not found in request context. Using empty localization map." + |
28 | | - "\nMake sure to update the localization files and initialize Honolate middleware correctly.", |
29 | | - { cause: ctx }, |
30 | | - ), |
31 | | - ); |
32 | | - return {}; |
33 | | - } |
34 | | - return map as Record<string, string>; |
| 17 | + return localizationMap; |
35 | 18 | } |
0 commit comments