Only fallback to an existing singular or nth plural form of a translation

Created on 24 March 2017, about 7 years ago
Updated 18 March 2024, 3 months ago

Problem/Motivation

Bulk operations in multiples nodes have this notice:
Notice: Undefined offset: 1 en Drupal\Core\StringTranslation\PluralTranslatableMarkup->render() (lรญnea 136 de core/lib/Drupal/Core/StringTranslation/PluralTranslatableMarkup.php) #0

That is line,
$return = $translated_array[1];
in,

    $arguments = $this->getArguments();
    $arguments['@count'] = $this->count;
    $translated_array = explode(PoItem::DELIMITER, $this->translatedString);

    if ($this->count == 1) {
      return $this->placeholderFormat($translated_array[0], $arguments);
    }

    $index = $this->getPluralIndex();
    if ($index == 0) {
      // Singular form.
      $return = $translated_array[0];
    }
    else {
      if (isset($translated_array[$index])) {
        // N-th plural form.
        $return = $translated_array[$index];
      }
      else {
        // If the index cannot be computed or there's no translation, use the
        // second plural form as a fallback (which allows for most flexibility
        // with the replaceable @count value).
        $return = $translated_array[1];
      }
    }

    return $this->placeholderFormat($return, $arguments);

Could also appends going to the results page of the webforms ( #44 ๐Ÿ› Notice: Undefined offset: 1 en Drupal\Core\StringTranslation\PluralTranslatableMarkup->render() Needs work ).

According to #19 ๐Ÿ› Notice: Undefined offset: 1 en Drupal\Core\StringTranslation\PluralTranslatableMarkup->render() Needs work ,

This issue happens because a translator (typically the ones automatically pulled in externally by Drupal) puts in an entry for the singular version,
but doesn't do the same for the plural one.

Confirmed by viewing the translation file on https://localise.biz/free/poeditor.

[...] the patch makes sense [...] to work around the human-error element of things.

Steps to reproduce

  1. Standard installation of using hi (i.e. non-English) language.
  2. Created a few articles using Article content type.
  3. Performed the bulk operation (/admin/content)

Proposed resolution

If the index cannot be computed or there's no translation, use the second plural form as a fallback (which allows for most flexibility with the replaceable @count value) but if the second value not exists return the first value.

Remaining tasks

User interface changes

none

API changes

none

Data model changes

none

Release notes snippet

๐Ÿ› Bug report
Status

Fixed

Version

10.2 โœจ

Component
Language systemย  โ†’

Last updated about 18 hours ago

  • Maintained by
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany @sun
Created by

๐Ÿ‡ช๐Ÿ‡ธSpain jmaties

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.

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany sleitner
  • Merge request !5678Patch 2863629-30 as MR โ†’ (Open) created by sleitner
  • Pipeline finished with Failed
    6 months ago
    Total: 407s
    #59100
  • Status changed to Needs review 6 months ago
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany sleitner
  • Pipeline finished with Success
    6 months ago
    Total: 91363s
    #59104
  • Status changed to RTBC 6 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Hiding patches for clarity.

    Still seems feedback from #29 was added. Thanks for updating the issue summary.

  • Status changed to Needs review 6 months ago
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia rksyravi New Delhi, ๐Ÿ‡ฎ๐Ÿ‡ณ

    Hi @sleitner,
    Unable to reproduce the issue.

    Steps followed:
    1. Standard installation of using hi (i.e. non-English) language.
    2. Created a few articles using Article content type.
    3. Performed the bulk operation (/admin/content)

    Each time operation ended successfully.

  • Status changed to RTBC 6 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Hiding patches for clarity.

  • ๐Ÿ‡ซ๐Ÿ‡ทFrance Chris64 France

    1) To be clearer. Not explicit here, but the error comes probably from the line,
    $return = $translated_array[1];
    (Line 136 in core/lib/Drupal/Core/StringTranslation/PluralTranslatableMarkup.php is actually something else)
    2) What about rather (plus some comments)?,
    $return = $translated_array[$index] ?? $translated_array[1] ?? $translated_array[0];
    instead of,

        if ($index == 0) {
          // Singular form.
          $return = $translated_array[0];
        }
        else {
          if (isset($translated_array[$index])) {
            // N-th plural form.
            $return = $translated_array[$index];
          }
          else {
            // If the index cannot be computed or there's no translation, use the
            // second plural form as a fallback (which allows for most flexibility
            // with the replaceable @count value).
            $return = $translated_array[1];
          }
        }
  • Pipeline finished with Success
    6 months ago
    Total: 687s
    #61748
  • Status changed to Needs review 6 months ago
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany sleitner

    @Chris64: 1) $return = $translated_array[1]; was in line 136 in 2017

    2) I separated the singular form completely from the plural forms:

        $index = $this->getPluralIndex();
        // Singular form.
        if ($this->count == 1 || $index == 0 || count($translated_array) == 1) {
          return $this->placeholderFormat($translated_array[0], $arguments);
        }
    
        // N-th plural form, fallback to second plural form.
        $return = $translated_array[$index] ?? $translated_array[1];
    
  • Status changed to RTBC 6 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    I don't see any issue consolidating

  • ๐Ÿ‡น๐Ÿ‡ทTurkey makbay

    I can confirm the diff resolves the issue. Drupal 10.1.7 & PHP 8.1

  • Status changed to Needs work 6 months ago
  • ๐Ÿ‡ณ๐Ÿ‡ฟNew Zealand quietone New Zealand

    I'm triaging RTBC issues โ†’ . I read the IS.

    An issue summary should provide contributors with a clear explanation of the problem and how to reproduce it. I don't see that in the issue summary here. Without that I don't have an understanding of what is being fixed and can't continue. Since having an up to date issue summary is a step in setting an issue to RTBC โ†’ I am tagging this for an issue summary update. It may help to look at the issue summary examples โ†’ .

    I did skim the comments and yes, I see that alexpott, a person with many years experience, has reviewed. However, it is also good practice to have an accurate issue summary so that more people can contribute and learn.

    I also noticed that @rksyravi was not able to reproduce the problem and there was no response to that. So, I must ask, is this reproducible on a supported version of Drupal?

  • ๐Ÿ‡น๐Ÿ‡ทTurkey makbay

    For me it was not about the bulk operations. The exception was appearing when I go to the results page of the webforms. But the diff has resolved this.

  • ๐Ÿ‡ซ๐Ÿ‡ทFrance Chris64 France

    it is also good practice to have an accurate issue summary

    Absolutely.

    @makby in #42 ๐Ÿ› Notice: Undefined offset: 1 en Drupal\Core\StringTranslation\PluralTranslatableMarkup->render() Needs work and #44 ๐Ÿ› Notice: Undefined offset: 1 en Drupal\Core\StringTranslation\PluralTranslatableMarkup->render() Needs work means the problem could be reproduced. Maybe @makby could be more explicit about how he meets the problem.

    @quietone in #43 ๐Ÿ› Notice: Undefined offset: 1 en Drupal\Core\StringTranslation\PluralTranslatableMarkup->render() Needs work seems face the same problem than me in #39 1) ๐Ÿ› Notice: Undefined offset: 1 en Drupal\Core\StringTranslation\PluralTranslatableMarkup->render() Needs work , and the answer #40 1) ๐Ÿ› Notice: Undefined offset: 1 en Drupal\Core\StringTranslation\PluralTranslatableMarkup->render() Needs work of @sleitner should be in the IS, otherwise the problem is ununderstendable from it.

    #19 ๐Ÿ› Notice: Undefined offset: 1 en Drupal\Core\StringTranslation\PluralTranslatableMarkup->render() Needs work from @codebymikey allows to understand the origin of the problem.

  • Pipeline finished with Failed
    5 months ago
    Total: 258s
    #72220
  • Status changed to Needs review 5 months ago
  • ๐Ÿ‡ซ๐Ÿ‡ทFrance Chris64 France
  • ๐Ÿ‡ซ๐Ÿ‡ทFrance Chris64 France

    Some new changes append in the code. To have only one call to $this->placeholderFormat(*, $arguments)?
    By now the ternary operator use is possible,

        // Singular form, or, N-th plural form, fallback to second plural form.
        $return = ($this->count == 1 || $index == 0 || count($translated_array) == 1)
                    ? $translated_array[0]
                    : $translated_array[$index] ?? $translated_array[1];

    or,

        // Singular form, or, N-th plural form, fallback to second plural form.
        $return = ($this->count == 1 || $index == 0 || count($translated_array) == 1) ? $translated_array[0] : $translated_array[$index] ?? $translated_array[1];

    instead of,

        if ($this->count == 1 || $index == 0 || count($translated_array) == 1) {
          // Singular form.
          $return = $translated_array[0];
        }
        else {
          // N-th plural form, fallback to second plural form.
          $return = $translated_array[$index] ?? $translated_array[1];
        }
  • Status changed to Needs work 5 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    MR appears to have failures.

    Have not reviewed or tested

  • Status changed to Needs review 5 months ago
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany sleitner
    /scripts-127385-579080/step_script: line 272: /usr/bin/tr: Argument list too long
    /scripts-127385-579080/step_script: line 272: /usr/bin/yarn: Argument list too long
    

    No, that is a GitlabCI problem. When many files are changed (during reroll), the spell-checking job always fails.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Then MR needs to be rebased as that's been fixed.

  • Pipeline finished with Success
    5 months ago
    Total: 742s
    #86147
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany sleitner

    Now it's all green

  • Status changed to RTBC 4 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Ran the test-only feature

    There was 1 error:
    1) Drupal\Tests\Core\StringTranslation\PluralTranslatableMarkupTest::testMissingPluralTranslation
    Undefined array key 1
    /builds/issue/drupal-2863629/core/lib/Drupal/Core/StringTranslation/PluralTranslatableMarkup.php:128
    /builds/issue/drupal-2863629/core/tests/Drupal/Tests/Core/StringTranslation/PluralTranslatableMarkupTest.php:51
    /builds/issue/drupal-2863629/vendor/phpunit/phpunit/src/Framework/TestResult.php:728
    /builds/issue/drupal-2863629/vendor/phpunit/phpunit/src/Framework/TestSuite.php:684
    /builds/issue/drupal-2863629/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:651
    /builds/issue/drupal-2863629/vendor/phpunit/phpunit/src/TextUI/Command.php:144
    /builds/issue/drupal-2863629/vendor/phpunit/phpunit/src/TextUI/Command.php:97
    ERRORS!
    Tests: 3, Assertions: 2, Errors: 1.
    

    Issue summary appears complete and mirrors the proposed changed.

    Tested with MR, installing non-english languages and didn't receive any errors. Believe this is good to go.

  • Status changed to Needs work 4 months ago
  • ๐Ÿ‡ณ๐Ÿ‡ฟNew Zealand quietone New Zealand

    I'm triaging RTBC issues โ†’ . I read the title, the IS, comments and the MR.

    The title should state what is being fixed. This title is simply an error message so I am tagging for an title update.

    The steps to reproduce in the issue summary do not match the problem/motivation section. I found the complete steps to reproduce in #37 and I copied them to the issue summary.

    I made two suggestions in the MR. Setting to NW for that and an improved title. Remember, the title is especially important when digging through git history. So, just a few things.

    @rksyravi, remember to update the issue summary, as appropriate, for the work you have done. In this case, the steps to reproduce should have been updated.

    Thanks!

  • Pipeline finished with Canceled
    4 months ago
    #105394
  • Pipeline finished with Success
    4 months ago
    Total: 489s
    #105395
  • Status changed to Needs review 4 months ago
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany sleitner
  • Status changed to RTBC 3 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Title appears to be updated.

  • Status changed to Fixed 3 months ago
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom alexpott ๐Ÿ‡ช๐Ÿ‡บ๐ŸŒ

    This change looks good. Can someone open an issue to make a further optimisation and not call $this->getPluralIndex() when dealing with singular counts.

    The code can be refactored to

        // Use the singular form when count is 1 or there is only a one translation.
        $index = $this->count == 1 || count($translated_array) === 1 ? 0 : $this->getPluralIndex();
        // Fallback to second plural form.
        return $this->placeholderFormat($translated_array[$index] ?? $translated_array[1], $arguments);
      }
    

    Committed and pushed 47b08da6e0 to 11.x and 059e87def9 to 10.3.x and 1213c7c004 to 10.2.x. Thanks!

    • alexpott โ†’ committed 1213c7c0 on 10.2.x
      Issue #2863629 by sleitner, jmaties, armrus, ravi.shankar, yogeshmpawar...
    • alexpott โ†’ committed 059e87de on 10.3.x
      Issue #2863629 by sleitner, jmaties, armrus, ravi.shankar, yogeshmpawar...
    • alexpott โ†’ committed 47b08da6 on 11.x
      Issue #2863629 by sleitner, jmaties, armrus, ravi.shankar, yogeshmpawar...
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.69.0 2024