native-dates

サンプル / ローカライズ

アラビア語の複数形・数字・ヒジュラ暦

アラビア語の6種類の複数形による期間表記、東部アラビア数字と西洋数字の切り替え、地域別の月名、ウンム・アルクラー暦のヒジュラ日付をフォーマットします。

試してみよう

  1. 1、2、5、11、100 を順にクリックし、アラビア語の単数形・双数形 يومان・複数形・対格形・100単位の名詞変化を確認します。
  2. 同じ日付で ar-EG と ar-MA を比較し、東部アラビア数字と西洋数字の違いやモロッコ固有の月名を確認します。
  3. 別の日付を選び、ar-SA-u-ca-islamic-umalqura が ISO 日付をウンム・アルクラー暦のヒジュラ日付へ変換する様子を確認します。
import { configure, date, days } from 'native-dates';

configure({ locale: 'ar-EG' });

const dayInput = document.querySelector('#day');
const count = document.querySelector('#count');
const output = document.querySelector('#output');

// isolate each right-to-left run so digits and range dashes stay in Arabic order
const rtl = (text) => `<bdi dir="rtl">${text}</bdi>`;

function render() {
  const total = Number(count.value);
  if (!dayInput.value || !(total >= 1)) { return; }
  const day = date(dayInput.value);
  const span = day.to(day.plus(4, 'days'));
  const maghreb = day.locale('ar-MA');

  const rows = [
    ['Six-way plural', '.text', rtl(days(total).text)],
    [
      'Eastern digits',
      ".format('long')",
      `${rtl(day.format('long'))} <span class="muted">·</span> ${rtl(span.format('long'))}`,
    ],
    [
      'Maghrebi locale',
      ".locale('ar-MA')",
      `${rtl(maghreb.format('long'))} <span class="muted">·</span> ${rtl(maghreb.for(5, 'days').format('long'))}`,
    ],
    [
      'Umm al-Qura Hijri',
      'ar-SA-u-ca-islamic-umalqura',
      `${rtl(day.format('long', 'ar-SA-u-ca-islamic-umalqura'))} <span class="muted">·</span> ${rtl(day.monthDay.format('long'))}`,
    ],
  ];

  output.innerHTML = rows.map(([label, locale, value]) => `
    <li>
      <span class="label">${label} <code>${locale}</code></span>
      <b>${value}</b>
    </li>`).join('');
}

for (const button of document.querySelectorAll('[data-count]')) {
  button.addEventListener('click', () => {
    count.value = button.dataset.count;
    render();
  });
}

dayInput.addEventListener('input', render);
count.addEventListener('input', render);
render();

起動中…

仕組み

configure({ locale: 'ar-EG' }) を一度呼び出すだけで、すべての値にアラビア語の複数形と東部アラビア数字が適用されます。days(2).text を読み取ると、数字を一切付けずに双数形の名詞だけで出力されます。

アプリ全体の既定ロケールは維持したまま、特定の値とそこから派生する範囲だけを西洋数字とモロッコの月名にしたい場合は、.locale('ar-MA') をチェーンします。

'ar-SA-u-ca-islamic-umalqura' を渡すと、任意の西暦日付をサウジアラビアのウンム・アルクラー暦(ヒジュラ暦)へ変換できます。同時に day.monthDay を使えば、西暦の月日も並べて表示できます。

左から右へ読む(LTR)ページ内にアラビア語テキストを埋め込む際は、フォーマット結果を <bdi> で囲むと、数字や範囲のダッシュが右から左の語順を保てます。