Skip to content
intlifyPublic

About

Collection of i18n utilities

Topics

Resources

Code of conduct

Contributing

Stars

17 stars

Watchers

2 watching

Forks

Repository files navigation

@intilfy/utils

npm version npm downloads CI

Collection of i18n utilities

🌟 Features

✅️  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

💿 Installation

🐢 Node.js

# Using npm
npm install @intlify/utils

# Using yarn
yarn add @intlify/utils

# Using pnpm
pnpm add @intlify/utils

🦕 Deno

Note: Deno is no longer supported, and @intlify/utils is not published to JSR after 1.1.0. On Deno, you can still import the npm package with npm:@intlify/utils, but it is not tested.

🥟 Bun

bun add @intlify/utils

🌍 Browser

in 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>

🧩 Using with h3 and Hono

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/h3 and @intlify/utils/hono are removed in v2. The functions of @intlify/utils have the same names and parameters, so replace the import and the first argument. If you use @intlify/h3 or @intlify/hono, you don't need to use @intlify/utils directly.

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())
})

🍭 Playground

You can play the below examples:

  • 🐢 Node.js: npm run play:node
  • 🥟 Bun: npm run play:bun
  • 🌍 Browser: npm run play:browser

🤝 API

See the API docs

🙌 Contributing guidelines

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.

🤝 Sponsors

The development of @intlify/utils is supported by my OSS sponsors!

sponsor

©️ License

MIT

About

Collection of i18n utilities

Topics

Resources

Code of conduct

Contributing

Stars

17 stars

Watchers

2 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages