Skip to content

Commit a68dac5

Browse files
committed
Improves passing of locale data and adds addRoute test
1 parent a285b69 commit a68dac5

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

lib/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,12 @@ export function freshPlugin (i18next) {
141141
return async (ctx) => {
142142
const placeholder = { headers: new Headers() }
143143
handle(i18next, { attachLocals: true })(ctx.req, placeholder, () => {})
144-
ctx.state.t = placeholder.locals.t
145-
ctx.state.i18n = placeholder.locals.i18n
144+
ctx.state.t = ctx.req.t
145+
ctx.state.i18n = ctx.req.i18n
146+
ctx.state.lng= ctx.req.lng
147+
ctx.state.locale = ctx.req.locale
148+
ctx.state.language = ctx.req.language
149+
ctx.state.languages = ctx.req.languages
146150
const resp = await ctx.next()
147151
resp.headers.set('Content-Language', placeholder.headers.get('Content-Language'))
148152
return resp

test/deno/addRoute.fresh.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { expect } from 'jsr:@std/expect'
2+
import i18next from 'https://deno.land/x/i18next/index.js'
3+
import i18nextMiddleware from '../../index.js'
4+
const { test } = Deno
5+
import { App } from "jsr:@fresh/core";
6+
7+
i18next
8+
.init({
9+
preload: ["en", "fr"],
10+
fallbackLng: "en"
11+
});
12+
13+
test('addRoute fresh', async () => {
14+
const routeHandle = (ctx) => {
15+
expect(ctx.state.lng).toEqual('en')
16+
expect(ctx.state.locale).toEqual('en')
17+
expect(ctx.state.language).toEqual('en')
18+
expect(ctx.state.languages).toEqual( ['en'])
19+
expect(ctx.state.i18n).not.toBeUndefined()
20+
expect(ctx.state.t).not.toBeUndefined()
21+
22+
expect(ctx.state.t('key')).toEqual('key')
23+
return new Response(ctx.state.t('key'))
24+
}
25+
const app = new App()
26+
.use(i18nextMiddleware.freshPlugin(i18next))
27+
i18nextMiddleware.addRoute(i18next, '/myroute/:lng/:ns', ['en'], app, 'get', routeHandle)
28+
const handler = app.handler()
29+
const res = await handler(
30+
new Request('http://localhost/myroute/en/test')
31+
);
32+
expect(await res.text()).toEqual('key')
33+
})

test/deno/middleware.fresh.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { expect } from 'jsr:@std/expect'
2-
import { assertEquals, assertNotEquals } from 'https://deno.land/std/testing/asserts.ts'
32
import i18next from 'https://deno.land/x/i18next/index.js'
43
import i18nextMiddleware from '../../index.js'
54
const { test } = Deno

0 commit comments

Comments
 (0)