← 中文资源

简体中文版本

选择正确的复数形式

按英文、法文与中文的复数规则,从 "单数|复数" 模板中选择形式并替换计数。

阅读英文原文

任务

按语言规则选择模板分支:英文只有 1 用单数,法文 0 与 1 用单数,中文不区分单复数,最后替换 {count}。

契约

  • 输入:JSON 对象:{ locale, count, template: “singular|plural” }
  • 输出:JSON 字符串:选中的形式,其中 {count} 已替换

样例

  1. 输入 {"locale":"en","count":1,"template":"{count} file|{count} files"} -> 期望 "1 file"。英文只有恰好为 1 时使用单数。
  2. 输入 {"locale":"fr","count":0,"template":"{count} fichier|{count} fichiers"} -> 期望 "0 fichier"。法文把零当作单数。

参考实现

function solve({ locale, count, template }) {
      const [one, other] = template.split('|');
      const useSingular = locale === 'fr' ? count <= 1 : count === 1;
      const form = locale === 'zh' ? other : useSingular ? one : other;
      return form.replace('{count}', String(count));
    }

为什么有效

复数规则因语言而异:中文没有数量一致性,法文把零视为单数。硬编码英文规则会把翻译直接做坏。

中文不存在单复数区分,因此必须始终使用复数形式。