Collection of i18n utilities
✅️ Modern: ES Modules first and respect Web Standard and ECMAScript Internationalization APIs
✅️ Compatible: ships ES Modules only, which you can also require() on Node.js 22.12+, and supports various JS environments
✅️️ Minimal: Small and fully tree-shakable
✅️️ Type Strong: Written in TypeScript, with full JSdoc
# Using npm
npm install @intlify/utils
# Using yarn
yarn add @intlify/utils
# Using pnpm
pnpm add @intlify/utilsNote: Deno is no longer supported, and
@intlify/utilsis not published to JSR after 1.1.0. On Deno, you can still import the npm package withnpm:@intlify/utils, but it is not tested.
bun add @intlify/utilsin your HTML:
<script type="module">
/**
* you can install via other CDN URL such as skypack,
* or, you can also use import maps
*/
import { isLocale } from 'https://esm.sh/@intlify/utils'
// something todo
// ...
</script>The functions of @intlify/utils take the Web standard Request and Response, so you can use them with h3 and Hono. Pass the Request of the framework as the first argument.
Note:
@intlify/utils/h3and@intlify/utils/honoare removed in v2. The functions of@intlify/utilshave the same names and parameters, so replace the import and the first argument. If you use@intlify/h3or@intlify/hono, you don't need to use@intlify/utilsdirectly.
h3 v2: pass event.req, and event.res to set the cookie.
import { H3 } from 'h3'
import { getHeaderLocale, setCookieLocale } from '@intlify/utils'
const app = new H3().get('/', event => {
const locale = getHeaderLocale(event.req)
setCookieLocale(event.res, locale)
return locale.toString()
})h3 v1: pass toWebRequest(event), and use setCookie of h3 to set the cookie.
import { eventHandler, setCookie, toWebRequest } from 'h3'
import { getHeaderLocale } from '@intlify/utils'
export default eventHandler(event => {
const locale = getHeaderLocale(toWebRequest(event))
setCookie(event, 'i18n_locale', locale.toString(), { path: '/' })
return locale.toString()
})Hono: pass c.req.raw (c.req is not a Web standard Request), and use setCookie of Hono to set the cookie.
import { Hono } from 'hono'
import { setCookie } from 'hono/cookie'
import { getHeaderLocale } from '@intlify/utils'
const app = new Hono().get('/', c => {
const locale = getHeaderLocale(c.req.raw)
setCookie(c, 'i18n_locale', locale.toString(), { path: '/' })
return c.text(locale.toString())
})You can play the below examples:
See the API docs
If you are interested in contributing to @intlify/utils, I highly recommend checking out the contributing guidelines here. You'll find all the relevant information such as how to make a PR, how to setup development) etc., there.
The development of @intlify/utils is supported by my OSS sponsors!