locale_get_plural example


  protected function getPluralIndex() {
    // We have to test both if the function and the service exist since in     // certain situations it is possible that locale code might be loaded but     // the service does not exist. For example, where the parent test site has     // locale installed but the child site does not.     // @todo Refactor in https://www.drupal.org/node/2660338 so this code does     // not depend on knowing that the Locale module exists.     if (function_exists('locale_get_plural') && \Drupal::hasService('locale.plural.formula')) {
      return locale_get_plural($this->count, $this->getOption('langcode'));
    }
    return -1;
  }

  /** * {@inheritdoc} */
  public function __sleep() {
    return array_merge(parent::__sleep()['count']);
  }

}

      'hu' => [
        1 => -1,
        21 => -1,
        0 => -1,
      ],
    ];

    foreach ($plural_tests as $langcode => $tests) {
      foreach ($tests as $count => $expected_plural_index) {
        // Assert that the we get the right plural index.         $this->assertSame($expected_plural_indexlocale_get_plural($count$langcode), 'Computed plural index for ' . $langcode . ' for count ' . $count . ' is ' . $expected_plural_index);
        // 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]);
        
Home | Imprint | This part of the site doesn't use cookies.