Skip to content

Commit 301f76c

Browse files
authored
feat(hono): support hono v4 (#33)
* docs(h3): update readme * fix(hono): replace to new style utils * feat(hono): support hono v4 * docs(hono): updat readme
1 parent b042358 commit 301f76c

File tree

18 files changed

+178
-1320
lines changed

18 files changed

+178
-1320
lines changed

knip.config.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,5 @@ export default {
1111
}
1212
},
1313
ignore: ['**/src/**.test-d.ts'],
14-
ignoreDependencies: [
15-
'lint-staged',
16-
'@vitest/coverage-v8',
17-
'vitest-environment-miniflare',
18-
'miniflare'
19-
]
14+
ignoreDependencies: ['lint-staged', '@vitest/coverage-v8']
2015
} satisfies KnipConfig

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,10 @@
6969
"eslint-plugin-yml": "^1.19.0",
7070
"knip": "^5.69.1",
7171
"lint-staged": "^16.2.6",
72-
"miniflare": "^3.20231016.0",
7372
"prettier": "^3.6.2",
7473
"typescript": "^5.9.3",
7574
"typescript-eslint": "^8.46.4",
76-
"vitest": "^4.0.0",
77-
"vitest-environment-miniflare": "^2.14.1"
75+
"vitest": "^4.0.0"
7876
},
7977
"prettier": "@kazupon/prettier-config",
8078
"lint-staged": {

packages/h3/README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ app.get('/', event => {
7676

7777
### Translation
7878

79+
If you want to use translation, you need to install plugin. As a result, you can use `useTranslation` within the handler:
80+
7981
```ts
8082
import { createServer } from 'node:http'
8183
import { H3, toNodeListener } from 'h3'
@@ -84,6 +86,7 @@ import { plugin as i18n, detectLocaleFromAcceptLanguageHeader, useTranslation }
8486
// install plugin with `H3` constructor
8587
const app = new H3({
8688
plugins: [
89+
// configure plugin options
8790
i18n({
8891
// detect locale with `accept-language` header
8992
locale: detectLocaleFromAcceptLanguageHeader,
@@ -295,10 +298,6 @@ If you are using [Visual Studio Code](https://code.visualstudio.com/) as an edit
295298
296299
<!-- eslint-disable markdown/no-missing-label-refs -- NOTE(kazupon): ignore github alert -->
297300
298-
> [!WARNING]
299-
> **This is experimental feature (inspired from [vue-i18n](https://vue-i18n.intlify.dev/guide/advanced/typescript.html#typescript-support)).**
300-
> We would like to get feedback from you 🙂.
301-
302301
> [!NOTE]
303302
> Resource Keys completion can be used if you are using [Visual Studio Code](https://code.visualstudio.com/)
304303

packages/h3/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"@types/node": "catalog:",
7171
"h3": "^2.0.1-rc.5",
7272
"publint": "catalog:",
73-
"srvx": "^0.9.6",
73+
"srvx": "catalog:",
7474
"tsdown": "catalog:",
7575
"typedoc": "catalog:",
7676
"typedoc-plugin-markdown": "catalog:",

packages/hono/README.md

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ Internationalization middleware & utilities for [Hono](https://hono.dev/)
88

99
## 🌟 Features
1010

11+
✅️️ &nbsp;**Internationalization utilities:** support [internationalization
12+
utils](https://github.com/intlify/srvmid/blob/main/packages/hono/docs/index.md) via [@intlify/utils](https://github.com/intlify/utils)
13+
1114
✅️ &nbsp;**Translation:** Simple API like
1215
[vue-i18n](https://vue-i18n.intlify.dev/)
1316

1417
&nbsp;**Custom locale detector:** You can implement your own locale detector
1518
on server-side
1619

17-
✅️️ &nbsp;**Useful utilities:** support internationalization composables
18-
utilities via [@intlify/utils](https://github.com/intlify/utils)
19-
2020
## 💿 Installation
2121

2222
```sh
@@ -35,6 +35,42 @@ bun add @intlify/hono
3535

3636
## 🚀 Usage
3737

38+
### Detect locale with utils
39+
40+
Detect locale from `accept-language` header:
41+
42+
```ts
43+
import { Hono } from 'hono'
44+
import { getHeaderLocale } from '@intlify/h3'
45+
46+
const app = new Hono()
47+
48+
app.get('/', c => {
49+
// detect locale from HTTP header which has `Accept-Language: ja,en-US;q=0.7,en;q=0.3`
50+
const locale = getHeaderLocale(c.req.raw)
51+
return c.text(locale.toString())
52+
})
53+
```
54+
55+
Detect locale from URL query:
56+
57+
```ts
58+
import { Hono } from 'hono'
59+
import { getQueryLocale } from '@intlify/h3'
60+
61+
const app = new Hono()
62+
63+
app.get('/', c => {
64+
// detect locale from query which has 'http://localhost:3000?locale=en'
65+
const locale = getQueryLocale(c.req.raw)
66+
return c.text(locale.toString())
67+
})
68+
```
69+
70+
### Translation
71+
72+
If you want to use translation, you need to install middleware. As a result, you can use `useTranslation` within the handler:
73+
3874
```ts
3975
import { Hono } from 'hono'
4076
import {
@@ -89,7 +125,7 @@ const DEFAULT_LOCALE = 'en'
89125
// define custom locale detector
90126
const localeDetector = (ctx: Context): string => {
91127
try {
92-
return getQueryLocale(ctx).toString()
128+
return getQueryLocale(ctx.req.raw).toString()
93129
} catch {
94130
return DEFAULT_LOCALE
95131
}
@@ -173,10 +209,6 @@ If you are using [Visual Studio Code](https://code.visualstudio.com/) as an edit
173209
174210
<!-- eslint-disable markdown/no-missing-label-refs -- NOTE(kazupon): ignore github alert -->
175211
176-
> [!WARNING]
177-
> **This is experimental feature (inspired from [vue-i18n](https://vue-i18n.intlify.dev/guide/advanced/typescript.html#typescript-support)).**
178-
> We would like to get feedback from you 🙂.
179-
180212
> [!NOTE]
181213
> Resource Keys completion can be used if you are using [Visual Studio Code](https://code.visualstudio.com/)
182214

packages/hono/package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,21 @@
5858
"scripts": {
5959
"build": "tsdown",
6060
"build:docs": "typedoc --excludeInternal",
61-
"play:basic": "wrangler dev ./playground/basic/index.ts",
61+
"play:basic": "node ./playground/basic/index.ts",
6262
"prepack": "pnpm build"
6363
},
6464
"dependencies": {
6565
"@intlify/core": "^11.0.0",
6666
"@intlify/utils": "catalog:"
6767
},
6868
"devDependencies": {
69-
"@cloudflare/workers-types": "^4.20231016.0",
7069
"@types/node": "catalog:",
71-
"hono": "^3.8.1",
70+
"hono": "^4.10.6",
7271
"publint": "catalog:",
72+
"srvx": "catalog:",
7373
"tsdown": "catalog:",
7474
"typedoc": "catalog:",
7575
"typedoc-plugin-markdown": "catalog:",
76-
"typescript": "catalog:",
77-
"wrangler": "^3.6.0"
76+
"typescript": "catalog:"
7877
}
7978
}

packages/hono/playground/basic/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Hono } from 'hono'
2+
import { serve } from 'srvx'
23
import {
34
defineI18nMiddleware,
45
detectLocaleFromAcceptLanguageHeader,
@@ -17,11 +18,16 @@ const i18n = defineI18nMiddleware({
1718
}
1819
})
1920

20-
const app = new Hono()
21+
const app: Hono = new Hono()
2122
app.use('*', i18n)
2223
app.get('/', c => {
2324
const t = useTranslation(c)
2425
return c.text(t('hello', { name: 'hono' }) + `\n`)
2526
})
2627

27-
export default app
28+
const server = serve({
29+
port: 3000,
30+
fetch: app.fetch
31+
})
32+
33+
await server.ready()

packages/hono/playground/global-schema/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const i18n = defineI18nMiddleware({
2525
}
2626
})
2727

28-
const app = new Hono()
28+
const app: Hono = new Hono()
2929
app.use('*', i18n)
3030
app.get('/', c => {
3131
const t = useTranslation(c)

packages/hono/playground/local-schema/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const i18n = defineI18nMiddleware({
1616
}
1717
})
1818

19-
const app = new Hono()
19+
const app: Hono = new Hono()
2020
app.use('*', i18n)
2121
app.get('/', c => {
2222
type ResourceSchema = {

packages/hono/playground/typesafe-schema/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// in your project, `import { ... } from '@inlify/hono'`
2-
import { defineI18nMiddleware } from '../../src/index.ts'
32
import { Hono } from 'hono'
3+
import { defineI18nMiddleware } from '../../src/index.ts'
44

55
// define resource schema
66
type ResourceSchema = {
@@ -20,7 +20,7 @@ const i18n = defineI18nMiddleware<[ResourceSchema], 'en' | 'ja'>({
2020
// ...
2121
})
2222

23-
const app = new Hono()
23+
const app: Hono = new Hono()
2424
app.use('*', i18n)
2525
// something your implementation code ...
2626
// ...

0 commit comments

Comments
 (0)