--- title: Intl.PluralRules slug: Web/JavaScript/Reference/Global_Objects/Intl/PluralRules translation_of: Web/JavaScript/Reference/Global_Objects/Intl/PluralRules --- <div>{{JSRef}}</div> <p>该<strong><code>Intl.PluralRules</code></strong>对象是用于启用多种敏感格式和多种语言规则的构造函数。</p> <h2 id="句法">句法</h2> <pre class="syntaxbox"><code>new Intl.PluralRules([<var>locales</var>[, <var>options</var>]]) Intl.PluralRules.call(<var>this</var>[, <var>locales</var>[, <var>options</var>]]) </code></pre> <h3 id="参数">参数></h3> <dl> <dt><code>locales</code></dt> <dd> <p>可选的。一个带有BCP 47语言标签的字符串或这种字符串的数组。有关参数的一般形式和解释<code>locales</code>,请参阅{{jsxref("Intl", "Intl page", "#Locale_identification_and_negotiation", 1)}}。</p> </dd> <dt><code>options</code></dt> <dd> <p>可选的,具有部分或全部以下属性的对象:</p> <dl> <dt><code>localeMatcher</code></dt> <dd>要使用的语言环境匹配算法。可能的值是<code>"lookup"</code>和<code>"best fit"</code>; 默认是<code>"best fit"</code>。有关此选项的信息,请参阅{{jsxref("Global_Objects/Intl", "Intl page", "#Locale_negotiation", 1)}}。</dd> <dt><code>type</code></dt> <dd>要使用的类型。可能的值是: <ul> <li><code>"cardinal"</code>对于基数(指的是事物的数量)。这是默认值。</li> <li><code>"ordinal"</code> 对于序号(指的是事物的排序或排名,例如英文中的“1st”,“2nd”,“3rd”)。</li> </ul> </dd> </dl> </dd> </dl> <h2 id="描述">描述</h2> <h3 id="属性">属性</h3> <dl> <dt>{{jsxref("PluralRules.prototype", "Intl.PluralRules.prototype")}}</dt> <dd>允许为所有对象添加属性。</dd> </dl> <h3 id="方法">方法</h3> <dl> <dt>{{jsxref("PluralRules.supportedLocalesOf", "Intl.PluralRules.supportedLocalesOf()")}}</dt> <dd>返回一个数组,其中包含提供的语言环境的支持,而不必回退到运行时的默认语言环境。</dd> </dl> <h2 id="PluralRules_实例"><code>PluralRules</code> 实例</h2> <h3 id="属性_2">属性</h3> <p><code>PluralRules</code> 实例从其原型继承了以下属性:</p> <div>{{page('/en-US/docs/Web/JavaScript/Reference/Global_Objects/PluralRules/prototype', 'Properties')}}</div> <h3 id="方法_2">方法></h3> <p><code>PluralRules</code> 实例从它们的原型继承了以下方法:</p> <div>{{page('/en-US/docs/Web/JavaScript/Reference/Global_Objects/PluralRules/prototype', 'Methods')}}</div> <h2 id="例子">例子</h2> <h3 id="基本用法">基本用法</h3> <p>在没有指定语言环境的基本使用中,返回默认语言环境中的格式化字符串和默认选项。这有助于区分单数和复数形式,例如“dog”和“dogs”。</p> <pre class="brush: js">var pr = new Intl.PluralRules(); pr.select(0); // → 'other' if in US English locale pr.select(1); // → 'one' if in US English locale pr.select(2); // → 'other' if in US English locale</pre> <h3 id="运用_locales">运用 <code>locales</code></h3> <p>这个例子展示了局部复数规则的一些变化。为了获得应用程序用户界面中使用的语言格式,请确保使用<code>locales</code>参数指定该语言(可能还有一些备用语言)> <pre class="brush: js">// Arabic has different plural rules new Intl.PluralRules('ar-EG').select(0); // → 'zero' new Intl.PluralRules('ar-EG').select(1); // → 'one' new Intl.PluralRules('ar-EG').select(2); // → 'two' new Intl.PluralRules('ar-EG').select(6); // → 'few' new Intl.PluralRules('ar-EG').select(18); // → 'many'</pre> <h3 id="运用_options">运用</h3> <p>可以查询查询结果使用><code>options</code>参数进行自定义,该参数具有一个<code>type</code>您可以设置的属性<code>ordinal</code>。这对计算序数指标很有用,例如“第一”,“第二”,“第三”,“第四”,“42”等。</p> <pre class="brush: js">var pr = new Intl.PluralRules('en-US', { type: 'ordinal' }); pr.select(0); // → 'other' pr.select(1); // → 'one' pr.select(2); // → 'two' pr.select(3); // → 'few' pr.select(4); // → 'other' pr.select(42); // → 'two'</pre> <h2 id="规范">规范</h2> <table class="standard-table"> <tbody> <tr> <th scope="col">规范</th> <th scope="col">状态</th> <th scope="col">评论</th> </tr> <tr> <td><a href="https://rawgit.com/caridy/intl-plural-rules-spec/master/index.html">国际复数规则草案</a></td> <td>{{Spec2('ES Int Draft')}}</td> <td>初始定义</td> </tr> </tbody> </table> <h2 id="浏览器兼容性">浏览器兼容性</h2> <div> <p>{{Compat("javascript.builtins.Intl.PluralRules")}}</p> </div> <h2 id="也可以看看">也可以看看</h2> <div>{{page('/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl', 'See_also')}}</div>