Skip to content

Latest commit

 

History

History
117 lines (86 loc) · 3.94 KB

I18n.md

File metadata and controls

117 lines (86 loc) · 3.94 KB

国際化

Day.js は国際化を手厚くサポートしています。

また、使用しないロケールをビルドに含みません。

デフォルトでは Day.js は英語 (US) ロケールを使用します。

複数のロケールの読み込みと切り替えも容易にできます。

サポートされているロケールの一覧

pull request によるロケールの追加は大歓迎です。 👍

API

グローバルロケールの変更

  • ロケール文字列を返します
import 'dayjs/locale/es'
import de from 'dayjs/locale/de'
dayjs.locale('es') // 読み込んだロケールをグローバルに適用
dayjs.locale('de-german', de) // ロケールを使用し、デフォルトの名前文字列を更新
const customizedLocaleObject = { ... } // 詳細は、以下のカスタマイズの項を参照
dayjs.locale(customizedLocaleObject) // カスタムロケールを適用
dayjs.locale('en') // switch back to default English locale globally
  • グローバルロケールを変更しても、既存のインスタンスには影響しません。

局所的なロケールの変更

  • 指定したロケールを適用した、新しい Dayjs オブジェクトを返します

dayjs#locale と同じですが、特定のインスタンスにのみロケールを適用します。

import 'dayjs/locale/es'
dayjs().locale('es').format() // 読み込んだロケールを特定のインスタンスに適用
dayjs('2018-4-28', { locale: es }) // コンストラクタを通して適用

インストール

  • NPM を使う場合:
import 'dayjs/locale/es' // 必要に応じて読み込み
// require('dayjs/locale/es') // CommonJS
// import locale_es from 'dayjs/locale/es' -> locale_es ロケールオブジェクトの読み込み

dayjs.locale('es') // ロケールをグローバルに適用
dayjs().locale('es').format() // ロケールを特定のインスタンスにのみ適用
  • CDN を使う場合:
<script src="https://unpkg.com/dayjs"></script>
<!-- プラグインを window.dayjs_plugin_NAME として読み込み -->
<script src="https://unpkg.com/dayjs/locale/zh-cn"></script>
<script>
  dayjs.locale('zh-cn');
  dayjs().locale('zh-cn').format()
  // ロケールオブジェクトを取得
  var customLocale = window.dayjs_locale_zh_cn // zh-cn -> zh_cn
</script>

カスタマイズ

独自のロケールを作成することもできます。

あなたのプラグインを共有する pull request を是非送ってみてください。

以下は Day.js ロケールオブジェクトのテンプレートです。

const localeObject = {
  name: 'es', // ロケール名を表す文字列
  weekdays: 'Domingo_Lunes ...'.split('_'), // 曜日の配列
  weekdaysShort: 'Sun_M'.split('_'), // OPTIONAL, short weekdays Array, use first three letters if not provided
  weekdaysMin: 'Su_Mo'.split('_'), // OPTIONAL, min weekdays Array, use first two letters if not provided
  months: 'Enero_Febrero ... '.split('_'), // 月の配列
  monthsShort: 'Jan_F'.split('_'), // OPTIONAL, short months Array, use first three letters if not provided
  ordinal: n => `${n}º`, // 序数 Function (number) => return number + output
  relativeTime: { // relative time format strings, keep %s %d as the same
    future: 'in %s', // e.g. in 2 hours, %s been replaced with 2hours
    past: '%s ago',
    s: 'a few seconds',
    m: 'a minute',
    mm: '%d minutes',
    h: 'an hour',
    hh: '%d hours', // e.g. 2 hours, %d been replaced with 2
    d: 'a day',
    dd: '%d days',
    M: 'a month',
    MM: '%d months',
    y: 'a year',
    yy: '%d years'
  }
}

以下は Day.js ロケールファイルのテンプレートです。

import dayjs from 'dayjs'

const locale = { ... } // Day.js ロケールオブジェクト

dayjs.locale(locale, null, true) // 後で使用するためにロケールを読み込み

export default locale