Skip to content

Commit

Permalink
refactor: add example case
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Sep 20, 2023
1 parent 79a142a commit 0e8a660
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 3 deletions.
30 changes: 29 additions & 1 deletion src/h3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ import type { H3Event } from 'h3'
*
* @description parse `accept-language` header string
*
* @example
* example for h3 event:
*
* ```ts
* import { createApp, eventHandler } from 'h3'
* import { getAcceptLanguages } from '@intlify/utils/h3'
*
* const app = createApp()
* app.use(eventHandler(event) => {
* const acceptLanguages = getAcceptLanguages(event)
* // ...
* })
* ```
*
* @param {H3Event} event The {@link H3Event | H3} event
*
* @returns {Array<string>} The array of language tags, if `*` (any language) or empty string is detected, return an empty array.
Expand All @@ -23,12 +37,26 @@ export function getAcceptLanguages(event: H3Event): string[] {
/**
* get locale
*
* @example
* example for h3 event:
*
* ```ts
* import { createApp, eventHandler } from 'h3'
* import { getAcceptLanguages } from '@intlify/utils/h3'
*
* app.use(eventHandler(event) => {
* const locale = getLocale(event)
* console.log(locale) // output `Intl.Locale` instance
* // ...
* })
* ```
*
* @param {H3Event} event The {@link H3Event | H3} event
* @param {string} lang The default language tag, default is `en-US`. You must specify the language tag with the {@link https://datatracker.ietf.org/doc/html/rfc4646#section-2.1 | BCP 47 syntax}.
*
* @throws {RangeError} Throws a {@link RangeError} if `lang` option or `accpet-languages` are not a well-formed BCP 47 language tag.
*
* @returns {Intl.Locale} The locale that resolved from `accept-language` header string, first language tag is used. if `*` (any language) or empty string is detected, return `en-US`.
* @returns {Intl.Locale} The first locale that resolved from `accept-language` header string, first language tag is used. if `*` (any language) or empty string is detected, return `en-US`.
*/
export function getLocale(event: H3Event, lang = 'en-US'): Intl.Locale {
return getLocaleWithGetter(() => getAcceptLanguages(event)[0] || lang)
Expand Down
29 changes: 28 additions & 1 deletion src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ import { getAcceptLanguagesFromGetter, getLocaleWithGetter } from './http.ts'
*
* @description parse `accept-language` header string
*
* @example
* example for Node.js request:
*
* ```ts
* import { createServer } from 'node:http'
* import { getAcceptLanguages } from '@intlify/utils/node'
*
* const server = createServer((req, res) => {
* const acceptLanguages = getAcceptLanguages(req)
* // ...
* })
* ```
*
* @param {IncomingMessage} event The {@link IncomingMessage | request}
*
* @returns {Array<string>} The array of language tags, if `*` (any language) or empty string is detected, return an empty array.
Expand All @@ -18,12 +31,26 @@ export function getAcceptLanguages(req: IncomingMessage) {
/**
* get locale
*
* @example
* example for Node.js request:
*
* ```ts
* import { createServer } from 'node:http'
* import { getAcceptLanguages } from '@intlify/utils/node'
*
* const server = createServer((req, res) => {
* const locale = getLocale(req)
* console.log(locale) // output `Intl.Locale` instance
* // ...
* })
* ```
*
* @param {IncomingMessage} event The {@link IncomingMessage | request}
* @param {string} lang The default language tag, default is `en-US`. You must specify the language tag with the {@link https://datatracker.ietf.org/doc/html/rfc4646#section-2.1 | BCP 47 syntax}.
*
* @throws {RangeError} Throws a {@link RangeError} if `lang` option or `accpet-languages` are not a well-formed BCP 47 language tag.
*
* @returns {Intl.Locale} The locale that resolved from `accept-language` header string, first language tag is used. if `*` (any language) or empty string is detected, return `en-US`.
* @returns {Intl.Locale} The first locale that resolved from `accept-language` header string, first language tag is used. if `*` (any language) or empty string is detected, return `en-US`.
*/
export function getLocale(req: IncomingMessage, lang = 'en-US'): Intl.Locale {
return getLocaleWithGetter(() => getAcceptLanguages(req)[0] || lang)
Expand Down
31 changes: 30 additions & 1 deletion src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ import { getAcceptLanguagesFromGetter, getLocaleWithGetter } from './http.ts'
*
* @description parse `accept-language` header string
*
* @example
* example for Web API request on Deno:
*
* ```ts
* import { getAcceptLanguages } from 'https://esm.sh/@intlify/utils/web'
*
* Deno.serve({
* port: 8080,
* }, (req) => {
* const acceptLanguages = getAcceptLanguages(req)
* // ...
* return new Response('Hello World!')
* })
* ```
*
* @param {Request} event The {@link Request | request}
*
* @returns {Array<string>} The array of language tags, if `*` (any language) or empty string is detected, return an empty array.
Expand All @@ -17,12 +32,26 @@ export function getAcceptLanguages(req: Request) {
/**
* get locale
*
* @example
* example for Web API request on Bun:
*
* import { getAcceptLanguages } from '@intlify/utils/web'
*
* Bun.serve({
* port: 8080,
* fetch(req) {
* const locale = getLocale(req)
* console.log(locale) // output `Intl.Locale` instance
* // ...
* },
* })
*
* @param {Request} event The {@link Request | request}
* @param {string} lang The default language tag, default is `en-US`. You must specify the language tag with the {@link https://datatracker.ietf.org/doc/html/rfc4646#section-2.1 | BCP 47 syntax}.
*
* @throws {RangeError} Throws a {@link RangeError} if `lang` option or `accpet-languages` are not a well-formed BCP 47 language tag.
*
* @returns {Intl.Locale} The locale that resolved from `accept-language` header string, first language tag is used. if `*` (any language) or empty string is detected, return `en-US`.
* @returns {Intl.Locale} The first locale that resolved from `accept-language` header string, first language tag is used. if `*` (any language) or empty string is detected, return `en-US`.
*/
export function getLocale(req: Request, lang = 'en-US'): Intl.Locale {
return getLocaleWithGetter(() => getAcceptLanguages(req)[0] || lang)
Expand Down

0 comments on commit 0e8a660

Please sign in to comment.