Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/deno.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ jobs:
with:
deno-version: ${{ matrix.deno }}
- run: deno --version
- run: deno test --allow-net --allow-env test/deno/*.js
- run: deno install && deno test --allow-net --allow-env test/deno/*.js
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,35 @@ app.get('/', async (req) => {
await app.listen({ port })
```

#### Fresh 2.x

```js
import i18next from 'https://deno.land/x/i18next/index.js'
import i18nextMiddleware from 'https://deno.land/x/i18next_http_middleware/index.js'
import { App, createDefine } from "jsr:@fresh/core";

i18next
.use(i18nextMiddleware.LanguageDetector)
.init({
preload: ["en", "fr"],
fallbackLng: "en",
resources: {
en: {
translation: { hi: "hello" }
},
fr: {
translation: { hi: "bonjour" }
}
}
});

const app = new App()
.use(i18nextMiddleware.freshPlugin(i18next))
.get("/", (ctx) => {
return new Response(ctx.state.t('hi'))
});
```

## add routes

```js
Expand Down
13 changes: 13 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ export function koaPlugin (i18next, options) {
}
}

export function freshPlugin (i18next) {
return async (ctx) => {
const placeholder = { headers: new Headers() }
handle(i18next, { attachLocals: true })(ctx.req, placeholder, () => {})
ctx.state.t = placeholder.locals.t
ctx.state.i18n = placeholder.locals.i18n
const resp = await ctx.next()
resp.headers.set('Content-Language', placeholder.headers.get('Content-Language'))
return resp
}
}

export const hapiPlugin = {
name: 'i18next-http-middleware',
version: '1',
Expand Down Expand Up @@ -311,6 +323,7 @@ export default {
plugin,
hapiPlugin,
koaPlugin,
freshPlugin,
handle,
getResourcesHandler,
missingKeyHandler,
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
"@babel/core": "7.26.0",
"@babel/preset-env": "7.26.0",
"@hapi/hapi": "^21.3.12",
"@opentelemetry/api": "^1.9.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are all these new dependencies necessary?

"@preact/signals": "^2.2.1",
"@types/express-serve-static-core": "^5.0.1",
"@koa/router": "12.0.1",
"koa": "2.16.1",
Expand All @@ -48,6 +50,8 @@
"fastify": "5.3.2",
"i18next": "25.5.2",
"mocha": "10.8.2",
"preact": "10.27.2",
"preact-render-to-string": "6.6.3",
"supertest": "7.0.0",
"tsd": "0.31.2",
"uglify-js": "3.19.3"
Expand Down
40 changes: 40 additions & 0 deletions test/deno/middleware.fresh.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { expect } from 'jsr:@std/expect'
import { assertEquals, assertNotEquals } from 'https://deno.land/std/testing/asserts.ts'
import i18next from 'https://deno.land/x/i18next/index.js'
import i18nextMiddleware from '../../index.js'
const { test } = Deno
import { App } from "jsr:@fresh/core";

i18next
.use(i18nextMiddleware.LanguageDetector)
.init({
preload: ["en", "fr"],
fallbackLng: "en",
resources: {
en: {
translation: { hi: "hello" }
},
fr: {
translation: { hi: "bonjour" }
}
}
});

test('middleware fresh', async () => {
const handler = new App()
.use(i18nextMiddleware.freshPlugin(i18next))
.get("/", (ctx) => {
return new Response(ctx.state.t('hi'))
})
.handler();

const headers = new Headers()
headers.append('Accept-Language', 'fr')
const res = await handler(
new Request('http://localhost',
{ headers }
));
const resHeaders = Object.fromEntries(res.headers.entries())
expect(resHeaders).toEqual(expect.objectContaining({ 'content-language': 'fr'}))
expect(await res.text()).toEqual("bonjour")
})