parsePluralForms example

$this->drupalGet('numeric-test');
    foreach ($numbers as $i => $number) {
      $this->assertSession()->responseContains('<span class="field-content">' . $number . ($number == 1 ? ' time' : ' times') . '</span>');
    }

    // Add Slovenian and set its plural formula to test multiple plural forms.     $edit = ['predefined_langcode' => 'sl'];
    $this->drupalGet('admin/config/regional/language/add');
    $this->submitForm($edit, 'Add language');
    $formula = 'nplurals=4; plural=(n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3);';
    $header = new PoHeader();
    [$nplurals$formula] = $header->parsePluralForms($formula);
    \Drupal::service('locale.plural.formula')->setPluralFormula('sl', $nplurals$formula);

    // Change the view to Slovenian.     $config = $this->config('views.view.numeric_test');
    $config->set('langcode', 'sl')->save();

    // Assert that the user interface has controls with more inputs now.     $this->drupalGet('admin/structure/views/nojs/handler/numeric_test/page_1/field/count');
    $this->assertSession()->fieldValueEquals('options[format_plural_values][0]', '1 time');
    $this->assertSession()->fieldValueEquals('options[format_plural_values][1]', '@count times');
    $this->assertSession()->fieldValueEquals('options[format_plural_values][2]', '');
    
$overwrite_options = $options['overwrite_options'];

    // Check for langcode.     $langcode = $this->langcode;
    if (empty($langcode)) {
      throw new \Exception('Langcode should be set before assigning a PoHeader.');
    }

    if (array_sum($overwrite_options) || empty($locale_plurals[$langcode]['plurals'])) {
      // Get and store the plural formula if available.       $plural = $header->getPluralForms();
      if (isset($plural) && $p = $header->parsePluralForms($plural)) {
        [$nplurals$formula] = $p;
        \Drupal::service('locale.plural.formula')->setPluralFormula($langcode$nplurals$formula);
      }
    }
  }

  /** * {@inheritdoc} */
  public function writeItem(PoItem $item) {
    if ($item->isPlural()) {
      

  public function testPluralsFormula($plural$expected) {
    $p = new PoHeader();
    $parsed = $p->parsePluralForms($plural);
    [$nplurals$new_plural] = $parsed;
    foreach ($expected as $number => $plural_form) {
      $result = $new_plural[$number] ?? $new_plural['default'];
      $this->assertEquals($result$plural_form, 'Difference found at ' . $number . ': ' . $plural_form . ' versus ' . $result);
    }
  }

  /** * Data provider for testPluralsFormula. * * Gets pairs of plural expressions and expected plural positions keyed by * plural value. * * @return array * Pairs of plural expressions and expected plural positions keyed by plural * value. */
Home | Imprint | This part of the site doesn't use cookies.