LoginSignup
2
2

More than 1 year has passed since last update.

Intl.PluralRules で序数詞表現

Last updated at Posted at 2019-10-02

JavaScript ではIntl.PluralRulesを使うといいかと思います。
Intl.PluralRules.prototype.selectメソッドは、数値を引数に取り、言語に応じたその数値のカテゴリ("zero", "one", "two", "few", "many","other")を返します。たとえば英語では、1は"one"、11は"other"が返ってきます。

「1は"st"だけど11は"th"」のような処理の部分を、「"one"は"st"、"other"は"th"」のように簡略化できます。
サンプル

const pluralRules = new Intl.PluralRules('en-US', { type: 'ordinal' });
const ordinalRules = { other:'th', one:'st', two:'nd', few:'rd' }
const ordinal = n => n + ordinalRules[ pluralRules.select(n) ];

ordinal(1); //1st
ordinal(2); //2nd
ordinal(3); //3rd
ordinal(4); //4th
ordinal(11); //11th
ordinal(21); //21st

参考:
Intl.PluralRules - JavaScript | MDN
cldr-numbers-full/numbers.json at master · unicode-cldr/cldr-numbers-full · GitHub

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2