createFromTranslatedString example

// Assert that the we get the right translation for that. Change the         // expected index as per the logic for translation lookups.         $expected_plural_index = ($count == 1) ? 0 : $expected_plural_index;
        $expected_plural_string = str_replace('@count', $count$plural_strings[$langcode][$expected_plural_index]);
        $this->assertSame($expected_plural_string, \Drupal::translation()->formatPlural($count, '1 hour', '@count hours', []['langcode' => $langcode])->render(), 'Plural translation of 1 hours / @count hours for count ' . $count . ' in ' . $langcode . ' is ' . $expected_plural_string);
        // DO NOT use translation to pass translated strings into         // PluralTranslatableMarkup::createFromTranslatedString() this way. It         // is designed to be used with *already* translated text like settings         // from configuration. We use PHP translation here just because we have         // the expected result data in that format.         $translated_string = \Drupal::translation()->translate('1 hour' . PoItem::DELIMITER . '@count hours', []['langcode' => $langcode]);
        $plural = PluralTranslatableMarkup::createFromTranslatedString($count$translated_string[]['langcode' => $langcode]);
        $this->assertSame($expected_plural_string$plural->render());
      }
    }
  }

  /** * Tests plural editing of DateFormatter strings. */
  public function testPluralEditDateFormatter() {

    // Import some .po files with formulas to set up the environment.
    $value = round($value$precision);
    // Test against both integer zero and float zero.     if ($this->options['empty_zero'] && ($value === 0 || $value === 0.0)) {
      return '';
    }

    $value = number_format($value$precision$this->options['decimal']$this->options['separator']);

    // If we should format as plural, take the (possibly) translated plural     // setting and format with the current language.     if (!empty($this->options['format_plural'])) {
      $value = PluralTranslatableMarkup::createFromTranslatedString($value$this->options['format_plural_string']);
    }

    return $this->sanitizeValue($this->options['prefix'], 'xss')
      . $this->sanitizeValue($value)
      . $this->sanitizeValue($this->options['suffix'], 'xss');
  }

}
Home | Imprint | This part of the site doesn't use cookies.