コンテンツへスキップ
JavaScript i18n

JavaScriptの国際化(i18n):Intl API によるブラウザネイティブの国際化

JavaScriptには、数値、日付、リストの書式設定や、ロケールに応じた複数形ルールの処理を行うための標準機能としてIntl APIが組み込まれています。外部ライブラリは不要です。このAPIはすべての最新ブラウザおよびNode.jsでサポートされており、ロケールに応じた文字列の比較、分割、相対的な時刻の書式設定を、追加の設定なしですぐに利用できます。

Package
lefine/strings
Runs in
Browsers · Node · Deno · Bun · Workers
lefine
01ターミナル
npm install lefine
02i18n.js
import { createTranslator } from 'lefine/strings'import messages from './locales/en.json' with { type: 'json' }// messages is { namespace: { key: text } }, as pull wrote itexport const t = createTranslator({  locale: 'en',  messages,  namespace: 'home',})
03main.js
import { t } from './i18n.js'document.querySelector('h1').textContent = t('title')// ICU values work too: "Hello, {name}"document.querySelector('p').textContent = t('greeting', { name: 'World' })
t('title')
Welcome
01/Setup

プレーンなJavaScriptにi18nを3ステップで追加する

JavaScript専用パッケージは不要:@better-i18n/coreは他のSDKが包む、単純なJavaScriptクライアントだ。

  1. 01

    クライアントをインストールする

    依存関係のない単一のパッケージ。ブラウザ、Node、Deno、Bun、Cloudflare Workersで動作する。

    terminalbash
    npm install lefinenpx lefine loginnpx lefine strings pull -o locales# lefine/strings has no framework dependency.# pull writes locales/en.json, de.json, ...
  2. 02

    クライアントを一度作成する

    モジュールスコープでインスタンス化し、必要な場所でインポートします。クライアントも設定も1つで、すべての呼び出しが同じインメモリキャッシュを共有します。

    i18n.js
    import { createTranslator } from 'lefine/strings'import messages from './locales/en.json' with { type: 'json' }// messages is { namespace: { key: text } }, as pull wrote itexport const t = createTranslator({  locale: 'en',  messages,  namespace: 'home',})
  3. 03

    Read a message

    getMessagesは単純なネストされたオブジェクトを返すので、4行のルックアップヘルパーがあれば十分なランタイムとなる。

    main.js
    import { t } from './i18n.js'document.querySelector('h1').textContent = t('title')// ICU values work too: "Hello, {name}"document.querySelector('p').textContent = t('greeting', { name: 'World' })
02/Two ways

クライアントで、またはfetchで

クライアントはキャッシュ、リトライ、フォールバックチェーンを追加する。自分で管理したければ、CDNは単純なJSONエンドポイントだ。

locale.js
import { createTranslator } from 'lefine/strings'let t = createTranslator({ locale: 'en', messages: await load('en'), namespace: 'home' })async function load(locale) {  const res = await fetch(`/locales/${locale}.json`) // the files pull wrote  return res.json()}export async function setLocale(next) {  t = createTranslator({ locale: next, messages: await load(next), namespace: 'home' })  document.documentElement.lang = next  render()}
03/Formatting

JavaScript Intl APIの実践

組み込みのIntlコンストラクタを使用して、あらゆる最新ブラウザまたはNode.jsランタイムでロケール固有の出力として通貨・日付・序数をフォーマットします。

format.js
// Using the built-in Intl APIconst formatter = new Intl.NumberFormat('de-DE', {  style: 'currency',  currency: 'EUR',});console.log(formatter.format(1234.56)); // "1.234,56 €"// Date formattingconst date = new Intl.DateTimeFormat('ja-JP', {  year: 'numeric',  month: 'long',  day: 'numeric',});console.log(date.format(new Date())); // "2026年3月2日"// Pluralizationconst plural = new Intl.PluralRules('en');const suffixes = { one: 'st', two: 'nd', few: 'rd', other: 'th' };function ordinal(n) {  return `${n}${suffixes[plural.select(n)]}`;}
04/Capabilities

JavaScript Intl APIの機能

  • コアi18n操作のための外部依存なしの組み込みIntl API
  • ロケール対応の通貨・パーセント・単位フォーマットのためのIntl.NumberFormat
  • ロケール固有の日付および時刻の表示形式のための Intl.DateTimeFormat
  • 100以上のロケールにおける序数・基数の複数形に関する国際的な規則
  • 複数形・セレクト・ネストのためのICU MessageFormat(intl-messageformat などのライブラリを使用。Intlには組み込まれていません)
  • 人間が読みやすい相対日付(例:「3日前」)のためのIntl.RelativeTimeFormat
  • ロケールに応じた結合リストおよび選択リストのための Intl.ListFormat
  • ロケール対応の文字列ソートと比較のためのIntl.Collator
  • 単語、文、および文字単位の境界検出用国際セグメンター
How it works

JavaScriptの文字列はどこから来るのか

ビルドステップやバンドルされたロケールファイルは不要:クライアントが実行時にロケールを解決し、エッジが応答する。

Read path — every locale load
Your JavaScript app

Reads from the plain object the pulled JSON file already gave.

0 network calls per render

lefine

getMessages(locale) — served from the in-memory cache if it is warm.

60 s cache · 0 deps

CDN edge

Cloudflare worker answers from the nearest edge cache.

max-age=60 · never a 5xx

R2 object store

The published translation files the sync worker wrote.

source of truth

Fallback chain — tried in order when a hop fails
  • 01In-memory TTL cache
  • 02CDN fetch, with timeout and one retry
  • 03Persistent storage, if configured
  • 04staticData bundled with the app
  • 05Throw — after everything above missed
Write path — dashboard to app
AI or translator

Proposal reviewed in the dashboard, glossary enforced.

MCP · dashboard · CLI

Publish

Sync worker writes the locale files to R2.

lefine strings publish

CDN purge

Fire-and-forget purge of the affected keys and the manifest.

non-critical by design

Live in the app

The next getMessages() past the TTL returns the new copy.

~5 min worst case

0dependencies in core
5 minSDK cache in production; edge 60s
200CDN status, even on failure
5fallback layers before an error
Get started

無料で始める。

今すぐJavaScriptの国際化を始めましょう

AIを活用したワークフロー、CLIによる同期、エッジCDN配信でJavaScriptの翻訳を管理できます。