Skip to content

Commit

Permalink
fix middleware type
Browse files Browse the repository at this point in the history
  • Loading branch information
KishiTheMechanic committed Oct 26, 2024
1 parent 6c889e2 commit 8c3096b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 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.9.2",
"version": "0.9.3",
"description": "A simple and flexible internationalization (i18n) plugin for Deno's Fresh framework.",
"runtimes": ["deno", "browser"],
"exports": "./mod.ts",
Expand Down
20 changes: 10 additions & 10 deletions src/i18nPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { join } from '@std/path'
import { pathname, translationData } from '@/src/store.ts'
import type { MiddlewareFn, TranslationState } from '@/src/types.ts'
import type { MiddlewareFn } from '@/src/types.ts'

/**
* Configuration options for the i18n plugin.
Expand Down Expand Up @@ -41,7 +41,9 @@ async function readJsonFile(filePath: string): Promise<Record<string, string>> {
*/
export const i18nPlugin = (
{ languages, defaultLanguage, localesDir }: I18nOptions,
): MiddlewareFn<TranslationState> => {
): MiddlewareFn<
{ t: Record<string, Record<string, string>>; path: string; locale: string }
> => {
return async (ctx) => {
const url = new URL(ctx.req.url)
const pathSegments = url.pathname.split('/').filter(Boolean)
Expand All @@ -55,8 +57,8 @@ export const i18nPlugin = (
: url.pathname

// Set the current state values
ctx.state.path = rootPath
ctx.state.locale = lang
ctx.state.path = rootPath // Valid
ctx.state.locale = lang // Valid

pathname.value = rootPath

Expand All @@ -69,12 +71,10 @@ export const i18nPlugin = (
* @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`)
const data = await readJsonFile(filePath)
translationDataSSR[namespace] = data
} catch {
// Ignore if the translation file does not exist
const filePath = join(localesDir, lang, `${namespace}.json`)
const data = await readJsonFile(filePath)
if (Object.keys(data).length > 0) {
translationDataSSR[namespace] = data // Only add if data exists
}
}

Expand Down

0 comments on commit 8c3096b

Please sign in to comment.