---
title: "Intl.PluralRules:复数规则与本地化文案"
keywords:
- Intl.PluralRules
- 复数类别
- CLDR
- 本地化
- 文案
description: "说明复数规则的本地化处理,使用 Intl.PluralRules 获取数量对应的类别(zero/one/other 等),结合模板渲染正确文案,提供示例与建议。"
categories:
- 文章资讯
- 编程技术
---
概述
不同语言的复数规则差异显著,Intl.PluralRules 提供根据数量返回类别的能力,辅助选择正确的本地化文案,提高国际化体验。
示例
const pr = new Intl.PluralRules('ru')
const category = pr.select(2) // 'few'
const messages = { one: '1 сообщение', few: '{n} сообщения', many: '{n} сообщений', other: '{n} сообщений' }
function t(n){ const c = pr.select(n); return messages[c].replace('{n}', n) }
工程建议
- 语言覆盖:为目标语言准备类别映射;避免仅处理
one/other。 - 与框架:结合 i18n 库(ICU/CLDR 数据);确保占位符与语法一致。
- 性能:缓存规则实例;避免频繁创建。
参考与验证
- MDN Intl.PluralRules 文档:https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Intl/PluralRules
- ECMA-402 规范:https://tc39.es/ecma402/#sec-intl-pluralrules-objects

发表评论 取消回复