Skip to content

Commit 5510eb5

Browse files
committed
docs: add intlify/hono api docs
1 parent 998ee1d commit 5510eb5

23 files changed

+717
-42
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
packages/h3/docs/**
2+
packages/hono/docs/**

eslint.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ const config: ReturnType<typeof defineConfig> = defineConfig(
103103
'.github/FUNDING.yml',
104104
'./**/playground/**',
105105
'./packages/h3/docs/**',
106+
'./packages/hono/docs/**',
106107
'design/**'
107108
]) as Linter.Config
108109
)

packages/h3/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@
6464
"@types/supertest": "^6.0.3",
6565
"h3": "^1.15.4",
6666
"supertest": "^7.1.4",
67-
"typedoc": "^0.28.14",
68-
"typedoc-plugin-markdown": "^4.9.0",
69-
"typescript": "^5.9.3",
67+
"typedoc": "catalog:",
68+
"typedoc-plugin-markdown": "catalog:",
69+
"typescript": "catalog:",
7070
"unbuild": "catalog:"
7171
}
7272
}

packages/hono/README.md

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -90,28 +90,32 @@ const DEFAULT_LOCALE = 'en'
9090
const localeDetector = (ctx: Context): string => {
9191
try {
9292
return getQueryLocale(ctx).toString()
93-
} catch () {
93+
} catch {
9494
return DEFAULT_LOCALE
9595
}
9696
}
9797

9898
const middleware = defineI18nMiddleware({
9999
// set your custom locale detector
100-
locale: localeDetector,
100+
locale: localeDetector
101101
// something options
102102
// ...
103103
})
104104
```
105105

106106
## 🧩 Type-safe resources
107107

108+
<!-- eslint-disable markdown/no-missing-label-refs -- NOTE(kazupon): ignore github alert -->
109+
108110
> [!WARNING]
109111
> **This is experimental feature (inspired from [vue-i18n](https://vue-i18n.intlify.dev/guide/advanced/typescript.html#typescript-support)).**
110112
> We would like to get feedback from you 🙂.
111113
112114
> [!NOTE]
113115
> The exeample code is [here](https://github.com/intlify/hono/tree/main/playground/typesafe-schema)
114116
117+
<!-- eslint-enable markdown/no-missing-label-refs -- NOTE(kazupon): ignore github alert -->
118+
115119
You can support the type-safe resources with schema using TypeScript on `defineI18nMiddleware` options.
116120

117121
Locale messages resource:
@@ -167,13 +171,17 @@ If you are using [Visual Studio Code](https://code.visualstudio.com/) as an edit
167171
168172
## 🖌️ Resource keys completion
169173
174+
<!-- eslint-disable markdown/no-missing-label-refs -- NOTE(kazupon): ignore github alert -->
175+
170176
> [!WARNING]
171177
> **This is experimental feature (inspired from [vue-i18n](https://vue-i18n.intlify.dev/guide/advanced/typescript.html#typescript-support)).**
172178
> We would like to get feedback from you 🙂.
173179
174180
> [!NOTE]
175181
> Resource Keys completion can be used if you are using [Visual Studio Code](https://code.visualstudio.com/)
176182
183+
<!-- eslint-enable markdown/no-missing-label-refs -- NOTE(kazupon): ignore github alert -->
184+
177185
You can completion resources key on translation function with `useTranslation`.
178186
179187
![Key Completion](assets/key-completion.gif)
@@ -182,9 +190,13 @@ resource keys completion has twe ways.
182190
183191
### Type parameter for `useTranslation`
184192
193+
<!-- eslint-disable markdown/no-missing-label-refs -- NOTE(kazupon): ignore github alert -->
194+
185195
> [!NOTE]
186196
> The exeample code is [here](https://github.com/intlify/hono/tree/main/playground/local-schema)
187197
198+
<!-- eslint-enable markdown/no-missing-label-refs -- NOTE(kazupon): ignore github alert -->
199+
188200
You can `useTranslation` set the type parameter to the resource schema you want to key completion of the translation function.
189201
190202
the part of example:
@@ -198,14 +210,18 @@ app.get('/', c => {
198210
const t = useTranslation<ResourceSchema>(c)
199211
// you can completion when you type `t('`
200212
return c.json(t('hello', { name: 'hono' }))
201-
}),
213+
})
202214
```
203215
204216
### global resource schema with `declare module '@intlify/hono'`
205217
218+
<!-- eslint-disable markdown/no-missing-label-refs -- NOTE(kazupon): ignore github alert -->
219+
206220
> [!NOTE]
207221
> The exeample code is [here](https://github.com/intlify/hono/tree/main/playground/global-schema)
208222
223+
<!-- eslint-enable markdown/no-missing-label-refs -- NOTE(kazupon): ignore github alert -->
224+
209225
You can do resource key completion with the translation function using the typescript `declare module`.
210226
211227
the part of example:
@@ -226,8 +242,7 @@ app.get('/', c => {
226242
const t = useTranslation(c)
227243
// you can completion when you type `t('`
228244
return c.json(t('hello', { name: 'hono' }))
229-
}),
230-
245+
})
231246
```
232247
233248
The advantage of this way is that it is not necessary to specify the resource schema in the `useTranslation` type parameter.
@@ -236,37 +251,11 @@ The advantage of this way is that it is not necessary to specify the resource sc
236251
237252
`@intlify/hono` has a concept of composable utilities & helpers.
238253
239-
### Utilities
240-
241-
`@intlify/hono` composable utilities accept context (from
242-
`(context) => {})`) as their first argument. (Exclud `useTranslation`) return the [`Intl.Locale`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale)
243-
244-
### Translations
245-
246-
- `useTranslation(context)`: use translation function
247-
248-
### Headers
249-
250-
- `getHeaderLocale(context, options)`: get locale from `accept-language` header
251-
- `getHeaderLocales(context, options)`: get some locales from `accept-language` header
252-
253-
### Cookies
254-
255-
- `getCookieLocale(context, options)`: get locale from cookie
256-
- `setCookieLocale(context, options)`: set locale to cookie
257-
258-
### Misc
259-
260-
- `getPathLocale(context, options)`: get locale from path
261-
- `getQueryLocale(context, options)`: get locale from query
262-
263-
## Helpers
264-
265-
- `detectLocaleFromAcceptLanguageHeader(context)`: detect locale from `accept-language` header
254+
See the [API References](https://github.com/intlify/srvmid/blob/main/packages/hono/docs/index.md)
266255
267256
## 🙌 Contributing guidelines
268257
269-
If you are interested in contributing to `@intlify/hono`, I highly recommend checking out [the contributing guidelines](/CONTRIBUTING.md) here. You'll find all the relevant information such as [how to make a PR](/CONTRIBUTING.md#pull-request-guidelines), [how to setup development](/CONTRIBUTING.md#development-setup)) etc., there.
258+
If you are interested in contributing to `@intlify/hono`, I highly recommend checking out [the contributing guidelines](https://github.com/intlify/srvmid/blob/main/CONTRIBUTING.md) here. You'll find all the relevant information such as [how to make a PR](https://github.com/intlify/srvmid/blob/main/CONTRIBUTING.md#pull-request-guidelines), [how to setup development](https://github.com/intlify/srvmid/blob/main/CONTRIBUTING.md#development-setup) etc., there.
270259
271260
## ©️ License
272261
@@ -278,5 +267,5 @@ If you are interested in contributing to `@intlify/hono`, I highly recommend che
278267
[npm-version-href]: https://npmjs.com/package/@intlify/hono
279268
[npm-downloads-src]: https://img.shields.io/npm/dm/@intlify/hono?style=flat&colorA=18181B&colorB=FFAD33
280269
[npm-downloads-href]: https://npmjs.com/package/@intlify/hono
281-
[ci-src]: https://github.com/intlify/utils/actions/workflows/ci.yml/badge.svg
282-
[ci-href]: https://github.com/intlify/utils/actions/workflows/ci.yml
270+
[ci-src]: https://github.com/intlify/srvmid/actions/workflows/ci.yml/badge.svg
271+
[ci-href]: https://github.com/intlify/srvmid/actions/workflows/ci.yml
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
[**@intlify/hono**](../index.md)
2+
3+
***
4+
5+
[@intlify/hono](../index.md) / defineI18nMiddleware
6+
7+
# Function: defineI18nMiddleware()
8+
9+
```ts
10+
function defineI18nMiddleware<Schema, Locales, Message, Options>(options): MiddlewareHandler;
11+
```
12+
13+
define i18n middleware for Hono
14+
15+
## Type Parameters
16+
17+
| Type Parameter | Default type |
18+
| ------ | ------ |
19+
| `Schema` | `RemoveIndexSignature`\<\{ \[`key`: `string`\]: `LocaleMessageValue`\<`string`\>; `hello`: `string`; `nest`: \{ `foo`: \{ `bar`: `string`; \}; \}; \}\> |
20+
| `Locales` | `string` |
21+
| `Message` | `string` |
22+
| `Options` *extends* `CoreOptions`\<`Message`, `SchemaParams`\<`Schema`, `Message`\>, `LocaleParams`\<`Locales`\>, `LocaleParams`\<`Locales`\> *extends* `object` ? `M` : `LocaleParams`\<`Locales`\> *extends* `string` ? `string` & `LocaleParams`\<`Locales`\> : `string`, `LocaleParams`\<`Locales`\> *extends* `object` ? `D` : `LocaleParams`\<`Locales`\> *extends* `string` ? `string` & `LocaleParams`\<`Locales`\> : `string`, `LocaleParams`\<`Locales`\> *extends* `object` ? `N` : `LocaleParams`\<`Locales`\> *extends* `string` ? `string` & `LocaleParams`\<`Locales`\> : `string`, `SchemaParams`\<`Schema`, `Message`\> *extends* `object` ? `M` : `LocaleMessage`\<`string`\>, `SchemaParams`\<`Schema`, `Message`\> *extends* `object` ? `D` : `DateTimeFormat`, `SchemaParams`\<`Schema`, `Message`\> *extends* `object` ? `N` : `NumberFormat`, `LocaleMessages`\<`SchemaParams`\<`Schema`, `Message`\> *extends* `object` ? `M` : `LocaleMessage`\<`string`\>, `LocaleParams`\<`Locales`\> *extends* `object` ? `M` : `LocaleParams`\<`Locales`\> *extends* `string` ? `string` & `LocaleParams`\<`Locales`\> : `string`, `Message`\>, `DateTimeFormats`\<`SchemaParams`\<`Schema`, `Message`\> *extends* `object` ? `D` : `DateTimeFormat`, `LocaleParams`\<`Locales`\> *extends* `object` ? `D` : `LocaleParams`\<`Locales`\> *extends* `string` ? `string` & `LocaleParams`\<`Locales`\> : `string`\>, `NumberFormats`\<`SchemaParams`\<`Schema`, `Message`\> *extends* `object` ? `N` : `NumberFormat`, `LocaleParams`\<`Locales`\> *extends* `object` ? `N` : `LocaleParams`\<`Locales`\> *extends* `string` ? `string` & `LocaleParams`\<`Locales`\> : `string`\>\> | `CoreOptions`\<`Message`, `SchemaParams`\<`Schema`, `Message`\>, `LocaleParams`\<`Locales`\>, `LocaleParams`\<`Locales`\> *extends* `object` ? `M` : `LocaleParams`\<`Locales`\> *extends* `string` ? `string` & `LocaleParams`\<`Locales`\> : `string`, `LocaleParams`\<`Locales`\> *extends* `object` ? `D` : `LocaleParams`\<`Locales`\> *extends* `string` ? `string` & `LocaleParams`\<`Locales`\> : `string`, `LocaleParams`\<`Locales`\> *extends* `object` ? `N` : `LocaleParams`\<`Locales`\> *extends* `string` ? `string` & `LocaleParams`\<`Locales`\> : `string`, `SchemaParams`\<`Schema`, `Message`\> *extends* `object` ? `M` : `LocaleMessage`\<`string`\>, `SchemaParams`\<`Schema`, `Message`\> *extends* `object` ? `D` : `DateTimeFormat`, `SchemaParams`\<`Schema`, `Message`\> *extends* `object` ? `N` : `NumberFormat`, `LocaleMessages`\<`SchemaParams`\<`Schema`, `Message`\> *extends* `object` ? `M` : `LocaleMessage`\<`string`\>, `LocaleParams`\<`Locales`\> *extends* `object` ? `M` : `LocaleParams`\<`Locales`\> *extends* `string` ? `string` & `LocaleParams`\<`Locales`\> : `string`, `Message`\>, `DateTimeFormats`\<`SchemaParams`\<`Schema`, `Message`\> *extends* `object` ? `D` : `DateTimeFormat`, `LocaleParams`\<`Locales`\> *extends* `object` ? `D` : `LocaleParams`\<`Locales`\> *extends* `string` ? `string` & `LocaleParams`\<`Locales`\> : `string`\>, `NumberFormats`\<`SchemaParams`\<`Schema`, `Message`\> *extends* `object` ? `N` : `NumberFormat`, `LocaleParams`\<`Locales`\> *extends* `object` ? `N` : `LocaleParams`\<`Locales`\> *extends* `string` ? `string` & `LocaleParams`\<`Locales`\> : `string`\>\> |
23+
24+
## Parameters
25+
26+
| Parameter | Type | Description |
27+
| ------ | ------ | ------ |
28+
| `options` | `Options` | An i18n options like vue-i18n [`createI18n`]([https://vue-i18n.intlify.dev/guide/#javascript](https://vue-i18n.intlify.dev/guide/#javascript)), which are passed to `createCoreContext` of `@intlify/core`, see about details [`CoreOptions` of `@intlify/core`](https://github.com/intlify/vue-i18n-next/blob/6a9947dd3e0fe90de7be9c87ea876b8779998de5/packages/core-base/src/context.ts#L196-L216) |
29+
30+
## Returns
31+
32+
`MiddlewareHandler`
33+
34+
A defined i18n middleware
35+
36+
## Description
37+
38+
Define the middleware to be specified for Hono [`app.use`]([https://hono.dev/guides/middleware](https://hono.dev/guides/middleware))
39+
40+
## Example
41+
42+
```js
43+
import { Hono } from 'hono'
44+
import { defineI18nMiddleware } from '@intlify/hono'
45+
46+
const i18nMiddleware = defineI18nMiddleware({
47+
messages: {
48+
en: {
49+
hello: 'Hello {name}!',
50+
},
51+
ja: {
52+
hello: 'こんにちは、{name}!',
53+
},
54+
},
55+
// your locale detection logic here
56+
locale: (event) => {
57+
// ...
58+
},
59+
})
60+
61+
const app = new Hono()
62+
app.use('*', i18nMiddleware)
63+
```
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
[**@intlify/hono**](../index.md)
2+
3+
***
4+
5+
[@intlify/hono](../index.md) / detectLocaleFromAcceptLanguageHeader
6+
7+
# Function: detectLocaleFromAcceptLanguageHeader()
8+
9+
```ts
10+
function detectLocaleFromAcceptLanguageHeader(ctx): string;
11+
```
12+
13+
locale detection with `Accept-Language` header
14+
15+
## Parameters
16+
17+
| Parameter | Type | Description |
18+
| ------ | ------ | ------ |
19+
| `ctx` | `Context` | A Hono context |
20+
21+
## Returns
22+
23+
`string`
24+
25+
A locale string, which will be detected of **first** from `Accept-Language` header
26+
27+
## Example
28+
29+
```js
30+
import { Hono } from 'hono'
31+
import { defineI18nMiddleware, detectLocaleWithAcceeptLanguageHeader } from '@intlify/hono'
32+
33+
const i18nMiddleware = defineI18nMiddleware({
34+
messages: {
35+
en: {
36+
hello: 'Hello {name}!',
37+
},
38+
ja: {
39+
hello: 'こんにちは、{name}!',
40+
},
41+
},
42+
locale: detectLocaleWithAcceeptLanguageHeader
43+
})
44+
45+
const app = new Hono()
46+
app.use('*', i18nMiddleware)
47+
```
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
[**@intlify/hono**](../index.md)
2+
3+
***
4+
5+
[@intlify/hono](../index.md) / getCookieLocale
6+
7+
# Function: getCookieLocale()
8+
9+
```ts
10+
function getCookieLocale(context, __namedParameters?): Locale;
11+
```
12+
13+
get locale from cookie
14+
15+
## Parameters
16+
17+
| Parameter | Type | Description |
18+
| ------ | ------ | ------ |
19+
| `context` | `Context` | A Context \| Hono context |
20+
| `__namedParameters?` | \{ `lang?`: `string`; `name?`: `string`; \} | - |
21+
| `__namedParameters.lang?` | `string` | - |
22+
| `__namedParameters.name?` | `string` | - |
23+
24+
## Returns
25+
26+
`Locale`
27+
28+
The locale that resolved from cookie
29+
30+
## Example
31+
32+
example for Hono:
33+
34+
```ts
35+
import { Hono } from 'hono'
36+
import { getCookieLocale } from '@intlify/utils/hono'
37+
38+
const app = new Hono()
39+
app.use('/', c => {
40+
const locale = getCookieLocale(c)
41+
console.log(locale) // output `Intl.Locale` instance
42+
// ...
43+
})
44+
```
45+
46+
## Throws
47+
48+
Throws a RangeError if `lang` option or cookie name value are not a well-formed BCP 47 language tag.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
[**@intlify/hono**](../index.md)
2+
3+
***
4+
5+
[@intlify/hono](../index.md) / getHeaderLanguage
6+
7+
# Function: getHeaderLanguage()
8+
9+
```ts
10+
function getHeaderLanguage(context, __namedParameters?): string;
11+
```
12+
13+
get language from header
14+
15+
## Parameters
16+
17+
| Parameter | Type | Description |
18+
| ------ | ------ | ------ |
19+
| `context` | `Context` | A Context \| Hono context |
20+
| `__namedParameters?` | `HeaderOptions` | - |
21+
22+
## Returns
23+
24+
`string`
25+
26+
A **first language tag** of header, if header is not exists, or `*` (any language), return empty string.
27+
28+
## Description
29+
30+
parse header string, default `accept-language`. if you use `accept-language`, this function retuns the **first language tag** of `accept-language` header.
31+
32+
## Example
33+
34+
example for Hone:
35+
36+
```ts
37+
import { Hono } from 'hono'
38+
import { getHeaderLanguage } from '@intlify/utils/hono'
39+
40+
const app = new Hono()
41+
app.use('/', c => {
42+
const langTag = getHeaderLanguage(c)
43+
// ...
44+
return c.text(`accepted language: ${langTag}`)
45+
})
46+
```

0 commit comments

Comments
 (0)