Skip to content

Commit

Permalink
fix response
Browse files Browse the repository at this point in the history
  • Loading branch information
KishiTheMechanic committed Oct 26, 2024
1 parent 2eda59d commit e82996f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 33 deletions.
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
35 changes: 3 additions & 32 deletions src/i18nPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>, or an empty object if parsing fails.
*/
async function readJsonFile(filePath: string): Promise<Record<string, string>> {
const content = await Deno.readTextFile(filePath)
try {
Expand All @@ -30,15 +17,6 @@ async function readJsonFile(filePath: string): Promise<Record<string, string>> {
}
}

/**
* 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<
Expand All @@ -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
Expand All @@ -61,12 +38,6 @@ export const i18nPlugin = (

const translationDataSSR: Record<string, Record<string, string>> = {}

/**
* 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`)
Expand All @@ -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)
Expand All @@ -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 })
}
}

0 comments on commit e82996f

Please sign in to comment.