Skip to content

Commit 4a24b30

Browse files
committed
docs: add API docs
1 parent a3af73c commit 4a24b30

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+2444
-53
lines changed

README.md

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -74,59 +74,9 @@ You can play the below examples:
7474
- 🌍 [Browser](https://github.com/intlify/utils/tree/main/examples/browser):
7575
`npm run play:browser`
7676

77-
## 🔨 Utilities
77+
## 🤝 API
7878

79-
### Common
80-
81-
- `isLocale`
82-
- `toLocale`
83-
- `parseAcceptLanguage`
84-
- `validateLangTag`
85-
- `normalizeLanguageName`
86-
87-
You can do `import { ... } from '@intlify/utils'` the above utilities
88-
89-
### Navigator
90-
91-
- `getNavigatorLocales`
92-
- `getNavigatorLocale`
93-
94-
You can do `import { ... } from '@intlify/utils'` the above utilities
95-
96-
<!-- eslint-disable markdown/no-missing-label-refs -->
97-
98-
> [!NOTE]
99-
> for Node.js You need to do `import { ... } from '@intlify/utils/node'`
100-
101-
<!-- eslint-enable markdown/no-missing-label-refs -->
102-
103-
### HTTP
104-
105-
- `getHeaderLanguages`
106-
- `getHeaderLanguage`
107-
- `getHeaderLocales`
108-
- `getHeaderLocale`
109-
- `getCookieLocale`
110-
- `setCookieLocale`
111-
- `getPathLocale`
112-
- `getQueryLocale`
113-
- `tryHeaderLocales`
114-
- `tryHeaderLocale`
115-
- `tryCookieLocale`
116-
- `tryPathLocale`
117-
- `tryQueryLocale`
118-
119-
The about utilities functions accept Web APIs such as [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) and [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) that is supported by JS environments (such as Deno, Bun, and Browser)
120-
121-
#### Specialized environments
122-
123-
If you will use Node.js and H3, You can do `import { ... } from '@intlify/utils/{ENV}'` the above utilities.
124-
125-
The namespace `{ENV}` is one of the following:
126-
127-
- `node`: accept `IncomingMessage` and `Outgoing` by Node.js [http](https://nodejs.org/api/http.html) module
128-
- `h3`: accept `H3Event` by HTTP framework [h3](https://github.com/unjs/h3)
129-
- `hono`: accept `Context` by edge-side web framework [hono](https://github.com/honojs/hono)
79+
See the [API docs](./docs/index.md)
13080

13181
## 🙌 Contributing guidelines
13282

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[**@intlify/utils**](../../index.md)
2+
3+
***
4+
5+
[@intlify/utils](../../index.md) / [default](../index.md) / createPathIndexLanguageParser
6+
7+
# Function: createPathIndexLanguageParser()
8+
9+
```ts
10+
function createPathIndexLanguageParser(index): PathLanguageParser;
11+
```
12+
13+
create a parser, which can split with slash `/`
14+
15+
## Parameters
16+
17+
| Parameter | Type | Default value | Description |
18+
| ------ | ------ | ------ | ------ |
19+
| `index` | `number` | `0` | An index of locale, which is included in path |
20+
21+
## Returns
22+
23+
`PathLanguageParser`
24+
25+
A return a parser, which has `PathLanguageParser` interface
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
[**@intlify/utils**](../../index.md)
2+
3+
***
4+
5+
[@intlify/utils](../../index.md) / [default](../index.md) / getCookieLocale
6+
7+
# Function: getCookieLocale()
8+
9+
```ts
10+
function getCookieLocale(request, options): Locale;
11+
```
12+
13+
get locale from cookie
14+
15+
## Parameters
16+
17+
| Parameter | Type | Description |
18+
| ------ | ------ | ------ |
19+
| `request` | `Request` | The Request \| request |
20+
| `options` | `CookieLocaleOptions` | The CookieLocaleOptions \| cookie locale options, `lang` option is `en-US` as default, you must specify the language tag with the [BCP 47 syntax](https://datatracker.ietf.org/doc/html/rfc4646#section-2.1). `name` option is `i18n_locale` as default. |
21+
22+
## Returns
23+
24+
`Locale`
25+
26+
The locale that resolved from cookie
27+
28+
## Example
29+
30+
example for Web API request on Deno:
31+
32+
```ts
33+
import { getCookieLocale } from 'https://esm.sh/@intlify/utils/web'
34+
35+
Deno.serve({
36+
port: 8080,
37+
}, (req) => {
38+
const locale = getCookieLocale(req)
39+
console.log(locale) // output `Intl.Locale` instance
40+
// ...
41+
})
42+
```
43+
44+
## Throws
45+
46+
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/utils**](../../index.md)
2+
3+
***
4+
5+
[@intlify/utils](../../index.md) / [default](../index.md) / getHeaderLanguage
6+
7+
# Function: getHeaderLanguage()
8+
9+
```ts
10+
function getHeaderLanguage(request, options): string;
11+
```
12+
13+
get language from header
14+
15+
## Parameters
16+
17+
| Parameter | Type | Description |
18+
| ------ | ------ | ------ |
19+
| `request` | `Request` | The Request \| request |
20+
| `options` | `HeaderOptions` | The HeaderOptions \| header options object |
21+
22+
## Returns
23+
24+
`string`
25+
26+
The **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 returns the **first language tag** of `accept-language` header.
31+
32+
## Example
33+
34+
example for Web API request on Deno:
35+
36+
```ts
37+
import { getAcceptLanguage } from 'https://esm.sh/@intlify/utils/web'
38+
39+
Deno.serve({
40+
port: 8080,
41+
}, (req) => {
42+
const langTag = getHeaderLanguage(req)
43+
// ...
44+
return new Response(`accepted language: ${langTag}`
45+
})
46+
```
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
[**@intlify/utils**](../../index.md)
2+
3+
***
4+
5+
[@intlify/utils](../../index.md) / [default](../index.md) / getHeaderLanguages
6+
7+
# Function: getHeaderLanguages()
8+
9+
```ts
10+
function getHeaderLanguages(request, options): string[];
11+
```
12+
13+
get languages from header
14+
15+
## Parameters
16+
17+
| Parameter | Type | Description |
18+
| ------ | ------ | ------ |
19+
| `request` | `Request` | The Request \| request |
20+
| `options` | `HeaderOptions` | The HeaderOptions \| header options object. `name` option is `accept-language` as default. |
21+
22+
## Returns
23+
24+
`string`[]
25+
26+
The array of language tags, if you use `accept-language` header and `*` (any language) or empty string is detected, return an empty array.
27+
28+
## Description
29+
30+
parse header string, default `accept-language` header
31+
32+
## Example
33+
34+
example for Web API request on Deno:
35+
36+
```ts
37+
import { getHeaderLanguages } from 'https://esm.sh/@intlify/utils/web'
38+
39+
Deno.serve({
40+
port: 8080,
41+
}, (req) => {
42+
const langTags = getHeaderLanguages(req)
43+
// ...
44+
return new Response(`accepted languages: ${langTags.join(', ')}`
45+
})
46+
```
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
[**@intlify/utils**](../../index.md)
2+
3+
***
4+
5+
[@intlify/utils](../../index.md) / [default](../index.md) / getHeaderLocale
6+
7+
# Function: getHeaderLocale()
8+
9+
```ts
10+
function getHeaderLocale(request, options): Locale;
11+
```
12+
13+
get locale from header
14+
15+
## Parameters
16+
17+
| Parameter | Type | Description |
18+
| ------ | ------ | ------ |
19+
| `request` | `Request` | The Request \| request |
20+
| `options` | `HeaderOptions` & `object` | The HeaderOptions \| header options object. `lang` option is `en-US` as default, you must specify the language tag with the [BCP 47 syntax](https://datatracker.ietf.org/doc/html/rfc4646#section-2.1). `name` option is `accept-language` as default, and `parser` option is `parseDefaultHeader` as default. |
21+
22+
## Returns
23+
24+
`Locale`
25+
26+
The first locale that resolved from header string. if you use `accept-language` header and `*` (any language) or empty string is detected, return `en-US`.
27+
28+
## Description
29+
30+
wrap language tag with Intl.Locale \| locale, languages tags will be parsed from `accept-language` header as default.
31+
32+
## Example
33+
34+
```ts
35+
example for Web API request on Bun:
36+
37+
import { getHeaderLocale } from '@intlify/utils/web'
38+
39+
Bun.serve({
40+
port: 8080,
41+
fetch(req) {
42+
const locale = getHeaderLocale(req)
43+
// ...
44+
return new Response(`accpected locale: ${locale.toString()}`)
45+
},
46+
})
47+
```
48+
49+
## Throws
50+
51+
Throws the `RangeError` if `lang` option or header are not a well-formed BCP 47 language tag.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
[**@intlify/utils**](../../index.md)
2+
3+
***
4+
5+
[@intlify/utils](../../index.md) / [default](../index.md) / getHeaderLocales
6+
7+
# Function: getHeaderLocales()
8+
9+
```ts
10+
function getHeaderLocales(request, options): Locale[];
11+
```
12+
13+
get locales from header
14+
15+
## Parameters
16+
17+
| Parameter | Type | Description |
18+
| ------ | ------ | ------ |
19+
| `request` | `Request` | The Request \| request |
20+
| `options` | `HeaderOptions` | The HeaderOptions \| header options object |
21+
22+
## Returns
23+
24+
`Locale`[]
25+
26+
The locales that wrapped from header, if you use `accept-language` header and `*` (any language) or empty string is detected, return an empty array.
27+
28+
## Description
29+
30+
wrap language tags with Intl.Locale \| locale, languages tags will be parsed from `accept-language` header as default.
31+
32+
## Example
33+
34+
example for Web API request on Bun:
35+
36+
```ts
37+
import { getHeaderLocales } from '@intlify/utils/web'
38+
39+
Bun.serve({
40+
port: 8080,
41+
fetch(req) {
42+
const locales = getHeaderLocales(req)
43+
// ...
44+
return new Response(`accpected locales: ${locales.map(locale => locale.toString()).join(', ')}`)
45+
},
46+
})
47+
```
48+
49+
## Throws
50+
51+
Throws the `RangeError` if header are not a well-formed BCP 47 language tag.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[**@intlify/utils**](../../index.md)
2+
3+
***
4+
5+
[@intlify/utils](../../index.md) / [default](../index.md) / getNavigatorLocale
6+
7+
# Function: getNavigatorLocale()
8+
9+
```ts
10+
function getNavigatorLocale(): Locale;
11+
```
12+
13+
get navigator locale
14+
15+
## Returns
16+
17+
`Locale`
18+
19+
The Intl.Locale \| locale
20+
21+
## Description
22+
23+
This function is the `Intl.Locale` wrapper of `navigator.language`.
24+
The value depends on the environments. if you use this function on the browser, you can get the languages, that are set in the browser, else if you use this function on the server side (Deno only), that value is the language set in the server.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[**@intlify/utils**](../../index.md)
2+
3+
***
4+
5+
[@intlify/utils](../../index.md) / [default](../index.md) / getNavigatorLocales
6+
7+
# Function: getNavigatorLocales()
8+
9+
```ts
10+
function getNavigatorLocales(): readonly Locale[];
11+
```
12+
13+
get navigator locales
14+
15+
## Returns
16+
17+
readonly `Locale`[]
18+
19+
The array of Intl.Locale \| locales
20+
21+
## Description
22+
23+
This function is a wrapper that maps in `Intl.Locale` in `navigator.languages`.
24+
This function return values depends on the environments. if you use this function on the browser, you can get the languages, that are set in the browser, else if you use this function on the server side (Deno only), that value is the languages set in the server.

0 commit comments

Comments
 (0)