Singular variant for plural string must contain @count

Created on 25 March 2020, about 5 years ago
Updated 28 December 2024, 4 months ago

All plural APIs recommend write singular value with "1" value instead of "@count".

   * @param string $singular
   *   The string for the singular case. Make sure it is clear this is singular,
   *   to ease translation (e.g. use "1 new comment" instead of "1 new"). Do not
   *   use @count in the singular string.
Do not use @count in the singular string.

This line is misleading and can cause problems.

This recommendation can be found in core:

  1. \Drupal\Core\StringTranslation\PluralTranslatableMarkup::__construct
  2. \Drupal\Core\StringTranslation\TranslationInterface::formatPlural
  3. Drupal.formatPlural (drupal.es6.js)

The problem is that not only "1" can have singular value in different languages.

E.g. in Russian "21", "31", "41" and so on is singular value: "51 товар" (51 products), but not "11".

This is handled by formula "Plural-Forms: nplurals=3; plural=((((n%10)==1)&&((n%100)!=11))?(0):(((((n%10)>=2)&&((n%10)<=4))&&(((n%100)<10)||((n%100)>=20)))?(1):2));\n".

The translation strings is:

msgid "1 product"
msgid_plural "@count products"
msgstr[0] "1 товар"
msgstr[1] "@count товара"
msgstr[2] "@count товаров"

So in that case new PluralTranslatableMarkup(21, '1 product', '@count products') will return "1 товар" (1 product).

The solution is easy — we need use "@count" for singular value as well. In that case problem is not exists. new PluralTranslatableMarkup(21, '@count product', '@count products') will return "21 товар" (21 products).

At least we need to change documentation and suggest to use "@count" for singular value. Also, it will require to change all plural strings.

🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component

locale.module

Created by

🇷🇺Russia niklan Russia, Perm

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • First commit to issue fork.
  • 🇩🇪Germany D34dMan Hamburg

    The MR builds on top of patch in Comment#17.

    - Added similar comment as in #1 for PluralTranslatableMarkup and drupal.js for "singular" variable.

    I think we should also trigger a depreciation if the singular argument doesn't include @count

    This seems like a documentation update only so far, as we are not testing against existing usage. Are you suggesting that we should add some checks during runtime to catch such strings?

    E.g. something like in the PluralTranslatableMarkup constructor which checks if incoming $singular variable do have "@count" in the text?

Production build 0.71.5 2024