Verified Commit 1213c7c0 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2863629 by sleitner, jmaties, armrus, ravi.shankar, yogeshmpawar,...

Issue #2863629 by sleitner, jmaties, armrus, ravi.shankar, yogeshmpawar, smustgrave, Chris64, quietone, alexpott: Only fallback to an existing singular or nth plural form of a translation

(cherry picked from commit 47b08da6)
parent d93860ee
Loading
Loading
Loading
Loading
Loading
+3 −16
Original line number Diff line number Diff line
@@ -107,28 +107,15 @@ public function render() {
    $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) {
    if ($this->count == 1 || $index == 0 || count($translated_array) == 1) {
      // 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];
      }
      // Nth plural form, fallback to second plural form.
      $return = $translated_array[$index] ?? $translated_array[1];
    }

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

+8 −0
Original line number Diff line number Diff line
@@ -43,4 +43,12 @@ public function providerPluralTranslatableMarkupSerialization() {
    ];
  }

  /**
   * Tests when the plural translation is missing.
   */
  public function testMissingPluralTranslation() {
    $markup = PluralTranslatableMarkup::createFromTranslatedString(2, 'There is no plural delimiter @count');
    $this->assertEquals('There is no plural delimiter 2', $markup->render());
  }

}