Intl.PluralRules【複数形ルール】オブジェクト
メモ
概要
- 国際化対応の複数形ルール オブジェクト
基本操作
- new PluralRules【複数形ルール】でオブジェクト生成
- ロケール・type【種別】・その他オプションを指定
- select【カテゴリ取得】・selectRange【カテゴリ取得 (範囲)】 でカテゴリを取得
- カテゴリに応じて数値文字列を編集
関連
外部リンク
- ECMA-402 (英語)
PluralRules Objects ES2024 Intl (11) ES2023 Intl (10) ES2022 Intl (9)
コンストラクタ・メソッド
コンストラクタ | 備考 |
---|---|
new Intl.PluralRules ( [ locales [ , options ] ] ) | コンストラクタ |
メソッド | 備考 | |
---|---|---|
Intl.PluralRules.prototype. | resolvedOptions ( ) | ロケール・オプション取得 |
Intl.PluralRules.prototype. | select ( value ) | カテゴリ取得 |
Intl.PluralRules.prototype. | selectRange ( start, end ) | カテゴリ取得 (範囲) |
Intl.PluralRules. | supportedLocalesOf ( locales [, options ] ) | サポート ロケール取得 |
プロパティ
プロパティ | 備考 | |
---|---|---|
Intl.PluralRules.prototype | [ @@toStringTag ] | タグ (デフォルト:'Intl.PluralRules') 実装:PluralRules[ Symbol.toStringTag ] |
Intl.PluralRules.prototype. | constructor | コンストラクタ定義 |
Intl.PluralRules. | prototype | プロトタイプ |
const rules = new Intl.PluralRules();
// [ @@toStringTag ]
console.log( rules[Symbol.toStringTag] );
// 出力例:Intl.PluralRules
console.log( rules.constructor );
// 出力例:ƒ PluralRules() { [native code] }
console.log( Intl.PluralRules.prototype );
// 出力例:
// Intl.PluralRules {Symbol(Symbol.toStringTag): 'Intl.PluralRules', constructor: ƒ, resolvedOptions: ƒ, select: ƒ, selectRange: ƒ}
// constructor: ƒ PluralRules()
// resolvedOptions: ƒ resolvedOptions()
// select: ƒ select()
// selectRange: ƒ selectRange()
// Symbol(Symbol.toStringTag): "Intl.PluralRules"
// [[Prototype]]: Object
new Intl.PluralRules【コンストラクタ】
メモ
概要
- Intl.PluralRules【複数形ルール】 オブジェクトを生成
外部リンク
- ECMA-402 (英語)
Intl.PluralRules ( [ locales [ , options ] ] ) ES2024 Intl (11) ES2023 Intl (10) ES2022 Intl (9) - ISO 639-1コード一覧
- BCP 47 (Best Current Practice) [英語]
- Unicode Locale Data Markup Language (LDML) [英語]
構文
new Intl.PluralRules ([locales[, options]] )
localesロケール (BCP 47 の言語タグ等)〔実装依存〕
文字列:1ロケール指定
文字列の配列:複数指定可 (適切な1ロケールを自動選択)
optionsオプション の組合せ
locales (ロケール) 実装依存
BCP 47 の言語タグ (一例)
値 | 備考 |
---|---|
ja | 日本語 |
ja-JP | 日本語 (日本) |
en-US | 英語 (アメリカ) |
en-GB | 英語 (イギリス) |
de-DE | ドイツ語 (ドイツ) |
fr-FR | フランス語 (フランス) |
ISO 639-1・639-2 (言語コード) 一例
ISO 639-1 | ISO 639-2 | ISO 639-3 | 備考 |
---|---|---|---|
ja | jpn | jpn | 日本語 |
en | eng | eng | 英語 |
de | deu ger | deu | ドイツ語 |
fr | fra fre | fra | フランス語 |
options (オプション)
オプション | 値 (太字:デフォルト値) | 備考 |
---|---|---|
種別 | ||
type | 'cardinal':基数 'ordinal':序数 (例:1st・2nd に対応) | 種別 |
その他 | ||
localeMatcher | 'lookup':Lookupアルゴリズム 'best fit':最適アルゴリズム | ロケールマッチングアルゴリズム |
maximumFractionDigits | minimumFractionDigits ~ 20 | 小数部の最大桁数 |
maximumSignificantDigits | minimumSignificantDigits ~ 21 | 最大有効桁数 |
minimumFractionDigits | 0 ~ 20 | 整数部の最小桁数 |
minimumIntegerDigits | 1 (デフォルト) ~ 21 | 小数部の最小桁数 |
minimumSignificantDigits | 1 ~ 21 | 最小有効桁数 |
roundingIncrement | 1 (デフォルト) ~ 5000 (有効:1, 2, 5, 10, 20, 25, 50, 100, 200, 250, 500, 1000, 2000, 2500, 5000) | 丸め増分 (参考:通貨金額) 各種ブラウザは未対応 (2023-6 時点) |
roundingMode | 'ceil': 'floor': 'expand': 'trunc': 'halfCeil': 'halfFloor': 'halfExpand': 'halfTrunc': 'halfEven': | 丸めモード 各種ブラウザは未対応 (2023-6 時点) |
roundingPriority | ’auto': 'morePrecision': 'lessPrecision': | 丸め優先 各種ブラウザは未対応 (2023-6 時点) |
roundingType | 'fractionDigits': 'significantDigits': 'morePrecision': 'lessPrecision': | 各種ブラウザは未対応 (2023-6 時点) |
trailingZeroDisplay | auto: stripIfInteger: | 小数点以下のゼロ表示方法 各種ブラウザは未対応 (2023-6 時点) |
例
select【カテゴリ取得】の例 参照
Intl.PluralRules.prototype.select【カテゴリ取得】
Intl.PluralRules.prototype.selectRange【カテゴリ取得 (範囲)】
メモ
概要
- カテゴリを取得
- 日本語は全て 'other'
- selectRange【カテゴリ取得 (範囲)】:Firefox 114.0.2 (2023-6-20) は未対応
外部リンク
- ECMA-402 (英語)
Intl.PluralRules.prototype.select ( value ) ES2024 Intl (11) ES2023 Intl (10) ES2022 Intl (9) Intl.PluralRules.prototype.selectRange ( start, end ) ES2024 Intl (11) ES2023 Intl (10) ES2022 Intl (9)
構文
pluralRules.select( value )
pluralRules.selectRange( start, end )
カテゴリ文字列
value値
start開始値
end終了値
カテゴリ文字列 | 備考 |
---|---|
'zero' | 0 |
'one' | 1 |
'two' | 2 |
'few' | 少数 |
'many' | 多数 |
'other' | その他 |
例
const rule_en = new Intl.PluralRules('en-US', { type: 'ordinal' });
const suffixes = {
'one': 'st',
'two': 'nd',
'few': 'rd',
'other': 'th',
};
function ordinals(n) {
const category = rule_en.select(n);
const suffix = suffixes[ category ];
const str = `${n}${suffix}`;
console.log( str, n, category );
};
ordinals(0); // 出力:0th 0 other
ordinals(1); // 出力:1st 1 one
ordinals(2); // 出力:2nd 2 two
ordinals(3); // 出力:3rd 3 few
ordinals(4); // 出力:4th 4 other
// 十の位が1の場合、すべて th
ordinals(10); // 出力:10th 10 other
ordinals(11); // 出力:11th 11 other
ordinals(12); // 出力:12th 12 other
ordinals(13); // 出力:13th 13 other
ordinals(14); // 出力:14th 14 other
ordinals(21); // 出力:21st 21 one
ordinals(22); // 出力:22nd 22 two
ordinals(23); // 出力:23rd 23 few
ordinals(24); // 出力:24th 24 other
Intl.PluralRules.prototype.resolvedOptions【ロケール・オプション取得】
メモ
概要
- オプション オブジェクトを取得
- オプションを持ったオブジェクト
- デフォルト値がない未指定オプションは未定義
関連
外部リンク
- ECMA-402 (英語)
Intl.PluralRules.prototype.resolvedOptions () ES2024 Intl (11) ES2023 Intl (10) ES2022 Intl (9)
構文
pluralRules.resolvedOptions( )
オブジェクト ()
例
const rules = new Intl.PluralRules();
console.log( rules.resolvedOptions() );
// 出力:
// {locale: 'ja', type: 'cardinal', minimumIntegerDigits: 1, minimumFractionDigits: 0, maximumFractionDigits: 3, …}
// locale: "ja"
// maximumFractionDigits: 3
// minimumFractionDigits: 0
// minimumIntegerDigits: 1
// pluralCategories: ['other']
// type: "cardinal"
// [[Prototype]]: Object
const rules_en = new Intl.PluralRules('en-US', {type: 'ordinal'});
console.log( rules_en.resolvedOptions() );
// 出力:
// {locale: 'en', type: 'ordinal', minimumIntegerDigits: 1, minimumFractionDigits: 0, maximumFractionDigits: 3, …}
// locale: "en"
// maximumFractionDigits: 3
// minimumFractionDigits: 0
// minimumIntegerDigits: 1
// pluralCategories: (4) ['few', 'one', 'two', 'other']
// type: "ordinal"
// [[Prototype]]: Object
Intl.PluralRules.supportedLocalesOf【サポート ロケール取得】
メモ
概要
- 指定ロケールから、サポート対象のロケールを取得
外部リンク
- ECMA-402 (英語)
Intl.PluralRules.supportedLocalesOf ( locales [ , options ] ) ES2024 Intl (11) ES2023 Intl (10) ES2022 Intl (9)
構文
Intl.PluralRules.supportedLocalesOf( locales [, options])
サポートされるロケールの配列
localesBCP 47 言語タグの文字列 または その配列
(new PluralRules【コンストラクタ】の ロケール詳細 を参照)
optionsマッチングオプション (localeMatcher【ロケールマッチングアルゴリズム】)
(new PluralRules【コンストラクタ】の オプション詳細 を参照)
例
console.log( Intl.PluralRules.supportedLocalesOf('ja') );
// 出力例:['ja']
console.log( Intl.PluralRules.supportedLocalesOf('ng') );
// 出力例:[]
let locales = ['ng', 'ja', 'ja-JP', 'en', 'en-US', 'en-GB'];
console.log( Intl.PluralRules.supportedLocalesOf(locales) );
// 出力例:(5) ['ja', 'ja-JP', 'en', 'en-US', 'en-GB']
locales = ['ng', 'jpn', 'jpn-JP', 'eng', 'eng-US', 'eng-GB'];
console.log( Intl.PluralRules.supportedLocalesOf(locales) );
// 出力例:(5) ['ja', 'ja-JP', 'en', 'en-US', 'en-GB']
console.log( Intl.PluralRules.supportedLocalesOf(locales, { localeMatcher: 'lookup' }) );
// 出力例:(5) ['ja', 'ja-JP', 'en', 'en-US', 'en-GB']