@@ -6,6 +6,30 @@ import type { InitHonolateOptions } from "./InitHonolateOptions.ts";
66import type { LocalizedValue } from "./LocalizedValue.ts" ;
77import { ensureRequestLanguage } from "./ensureRequestLanguage.ts" ;
88
9+ /**
10+ * Initializes Honolate with the given options.
11+ *
12+ * Make sure to add the returned middleware to your Hono app before any route handlers that use localization.
13+ *
14+ * @param options the options with which Honolate will be initialized. See {@link InitHonolateOptions}
15+ * @returns the middleware required to show localized strings using Honolate
16+ *
17+ * @example
18+ * import { InitHonolateOptions, initHonolate } from '@wuespace/honolate';
19+ *
20+ * const honolateOptions: InitHonolateOptions<'en' | 'de'> = {
21+ * defaultLanguage: 'en',
22+ * languages: {
23+ * en: './locales/en.json',
24+ * de: './locales/de.json',
25+ * },
26+ * // optional: add custom language detection logic here
27+ * };
28+ *
29+ * const app = new Hono();
30+ * app.use('*', initHonolate(honolateOptions));
31+ * // ...other middlewares and route handlers
32+ */
933export const initHonolate : < T extends string > (
1034 options : InitHonolateOptions < T > ,
1135) => ReturnType < typeof createMiddleware < HonolateContext < T > > > = <
@@ -18,30 +42,7 @@ export const initHonolate: <T extends string>(
1842 const languages = new Map < T , Record < string , LocalizedValue > > ( ) ;
1943
2044 for ( const lang in options . languages ) {
21- const path = options . languages [ lang as T ] ;
22- const module = neverThrow ( ( ) => Deno . readFileSync ( normalizePath ( path ) ) ) ;
23- if ( module instanceof Error ) {
24- console . error (
25- `Failed to load language file for ${ lang } at ${ path } :` ,
26- module ,
27- ) ;
28- languages . set ( lang as T , { } ) ;
29- continue ;
30- }
31-
32- const decodedModule = new TextDecoder ( ) . decode ( module ) ;
33- const parsedModule = neverThrow ( ( ) => JSON . parse ( decodedModule ) ) ;
34-
35- if ( parsedModule instanceof Error ) {
36- console . error (
37- `Failed to parse language file for ${ lang } at ${ path } :` ,
38- parsedModule ,
39- ) ;
40- languages . set ( lang as T , { } ) ;
41- continue ;
42- }
43-
44- languages . set ( lang as T , parsedModule as Record < string , LocalizedValue > ) ;
45+ languages . set ( lang , loadLanguage ( options . languages [ lang ] ) ) ;
4546 }
4647
4748 return createMiddleware < HonolateContext < T > > ( async ( c , next ) => {
@@ -56,3 +57,39 @@ export const initHonolate: <T extends string>(
5657 return next ( ) ;
5758 } ) ;
5859} ;
60+
61+ /**
62+ * Loads a language file from the given path.
63+ * @param languagePath the path to the language
64+ * @returns the loaded localization map
65+ */
66+ function loadLanguage ( languagePath : string ) : Record < string , LocalizedValue > {
67+ // Load
68+ const module = neverThrow ( ( ) =>
69+ Deno . readFileSync ( normalizePath ( languagePath ) )
70+ ) ;
71+
72+ if ( module instanceof Error ) {
73+ // Handle file read error
74+ console . error (
75+ `Failed to load language file at ${ languagePath } :` ,
76+ module ,
77+ ) ;
78+ return { } ;
79+ }
80+
81+ // Parse
82+ const decodedModule = new TextDecoder ( ) . decode ( module ) ;
83+ const parsedModule = neverThrow ( ( ) => JSON . parse ( decodedModule ) ) ;
84+
85+ if ( parsedModule instanceof Error ) {
86+ // Handle JSON parse error
87+ console . error (
88+ `Failed to parse language file at ${ languagePath } :` ,
89+ parsedModule ,
90+ ) ;
91+ return { } ;
92+ }
93+
94+ return parsedModule as Record < string , LocalizedValue > ;
95+ }
0 commit comments