JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 260749
  • Score
    100M100P100Q173683F
  • License MIT

Internationalization middleware & utilities for h3

Package Exports

  • @intlify/h3
  • @intlify/h3/dist/index.cjs
  • @intlify/h3/dist/index.d.cts
  • @intlify/h3/dist/index.d.mts
  • @intlify/h3/dist/index.d.ts
  • @intlify/h3/dist/index.mjs
  • @intlify/h3/package.json

Readme

@intlify/h3

npm version npm downloads CI

Internationalization middleware & utilities for h3

๐ŸŒŸ Features

โœ…๏ธ  Translation: Simple API like vue-i18n

โœ…  Custom locale detector: You can implement your own locale detector on server-side

โœ…๏ธ๏ธ  Useful utilities: support internationalization composables utilities via @intlify/utils

๐Ÿ’ฟ Installation

# Using npm
npm install @intlify/h3

# Using yarn
yarn add @intlify/h3

# Using pnpm
pnpm add @intlify/h3

# Using bun
bun add @intlify/h3

๐Ÿš€ Usage

import { createServer } from 'node:http'
import { createApp, createRouter, eventHandler, toNodeListener } from 'h3'
import {
  defineI18nMiddleware,
  detectLocaleFromAcceptLanguageHeader,
  useTranslation,
} from '@intlify/h3'

// define middleware with vue-i18n like options
const middleware = defineI18nMiddleware({
  // detect locale with `accept-language` header
  locale: detectLocaleFromAcceptLanguageHeader,
  // resource messages
  messages: {
    en: {
      hello: 'Hello {name}!',
    },
    ja: {
      hello: 'ใ“ใ‚“ใซใกใฏใ€{name}๏ผ',
    },
  },
  // something options
  // ...
})

// install middleware with `createApp` option
const app = createApp({ ...middleware })

const router = createRouter()
router.get(
  '/',
  eventHandler((event) => {
    // use `useTranslation` in event handler
    const t = useTranslation(event)
    return t('hello', { name: 'h3' })
  }),
)

app.use(router)
createServer(toNodeListener(app)).listen(3000)

๐Ÿ› ๏ธ Custom locale detection

You can detect locale with your custom logic from current H3Event.

example for detecting locale from url query:

import { defineI18nMiddleware, getQueryLocale } from '@intlify/h3'
import type { H3Event } from 'h3'

const DEFAULT_LOCALE = 'en'

// define custom locale detector
const localeDetector = (event: H3Event): string => {
  try {
    return getQueryLocale(event).toString()
  } catch () {
    return DEFAULT_LOCALE
  }
}

const middleware = defineI18nMiddleware({
  // set your custom locale detector
  locale: localeDetector,
  // something options
  // ...
})

๐Ÿ› ๏ธ Utilites & Helpers

@intlify/h3 has a concept of composable utilities & helpers.

Utilities

@intlify/h3 composable utilities accept event (from eventHandler((event) => {})) as their first argument. (Exclud useTranslation) return the Intl.Locale

Translations

  • useTranslation(event): use translation function

Headers

  • getHeaderLocale(event): get locale from accept-language header
  • getHeaderLocales(event): get some locales from accept-language header

Cookies

  • getCookieLocale(): get locale from cookie
  • setCookieLocale(): set locale to cookie

Misc

  • getPathLocale(event): get locale from path
  • getQueryLocale(event): get locale from query

Helpers

  • detectLocaleFromAcceptLanguageHeader: detect locale from accept-language header

๐Ÿ™Œ Contributing guidelines

If you are interested in contributing to @intlify/h3, 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.

ยฉ๏ธ License

MIT