diff --git a/deno.json b/deno.json index 3244502..135740e 100644 --- a/deno.json +++ b/deno.json @@ -1,6 +1,6 @@ { "name": "@elsoul/fresh-i18n", - "version": "0.7.0", + "version": "0.7.1", "description": "A simple and flexible internationalization (i18n) plugin for Deno's Fresh framework.", "runtimes": ["deno", "browser"], "exports": "./mod.ts", diff --git a/src/i18nPlugin.ts b/src/i18nPlugin.ts index 7fe327b..8599d44 100644 --- a/src/i18nPlugin.ts +++ b/src/i18nPlugin.ts @@ -2,25 +2,12 @@ import { join } from '@std/path' import { pathname, translationData } from '@/src/store.ts' import type { MiddlewareFn } from '@/src/types.ts' -/** - * Configuration options for the i18n plugin. - * - * @property languages - An array of supported language codes (e.g., ['en', 'ja']). - * @property defaultLanguage - The default language code to use when no language is detected. - * @property localesDir - The directory path where translation JSON files are stored. - */ export interface I18nOptions { languages: string[] defaultLanguage: string localesDir: string } -/** - * Reads a JSON file and parses its contents. - * - * @param filePath - The path to the JSON file. - * @returns Parsed JSON object as Record, or an empty object if parsing fails. - */ async function readJsonFile(filePath: string): Promise> { const content = await Deno.readTextFile(filePath) try { @@ -30,15 +17,6 @@ async function readJsonFile(filePath: string): Promise> { } } -/** - * Middleware function to initialize internationalization (i18n) support. - * This plugin detects the user's language based on the URL, loads the necessary - * translation files dynamically, and saves the translations and base path as - * global signals for both client-side and server-side access. - * - * @param options - Configuration options for the i18n plugin. - * @returns A middleware function that handles language detection and translation loading. - */ export const i18nPlugin = ( { languages, defaultLanguage, localesDir }: I18nOptions, ): MiddlewareFn< @@ -51,7 +29,6 @@ export const i18nPlugin = ( ? pathSegments[0] : defaultLanguage - // Sets the root path without the language prefix for client-side navigation. const rootPath = lang === pathSegments[0] ? '/' + pathSegments.slice(1).join('/') : url.pathname @@ -61,12 +38,6 @@ export const i18nPlugin = ( const translationDataSSR: Record> = {} - /** - * Loads a translation namespace by reading the corresponding JSON file from `localesDir`. - * If the file does not exist, it is ignored. - * - * @param namespace - The namespace of the translation file to load (e.g., 'common'). - */ const loadTranslation = async (namespace: string) => { try { const filePath = join(localesDir, lang, `${namespace}.json`) @@ -77,7 +48,6 @@ export const i18nPlugin = ( } } - // Load the common namespace and additional namespaces based on the URL path. await loadTranslation('common') for ( const segment of pathSegments.slice(lang === pathSegments[0] ? 1 : 0) @@ -88,7 +58,8 @@ export const i18nPlugin = ( ctx.state.t = translationDataSSR translationData.value = translationDataSSR - // Call `ctx.next()` to continue to the next middleware in the chain. - await ctx.next() + // Ensure `ctx.next()` returns a response and handle the response in the middleware chain. + const response = await ctx.next() + return response ?? new Response(null, { status: 204 }) } }