PoHeader 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');
    
'customized' => FALSE,
      'not_customized' => FALSE,
      'not_translated' => FALSE,
    ];
    $this->options = $options;
  }

  /** * {@inheritdoc} */
  public function getHeader() {
    return new PoHeader($this->getLangcode());
  }

  /** * Implements Drupal\Component\Gettext\PoMetadataInterface::setHeader(). * * @throws \Exception * Always, because you cannot set the PO header of a reader. */
  public function setHeader(PoHeader $header) {
    throw new \Exception('You cannot set the PO header in a reader.');
  }

  

  private function readHeader() {
    $item = $this->readItem();
    // Handle the case properly when the .po file is empty (0 bytes).     if (!$item) {
      return;
    }
    $header = new PoHeader();
    $header->setFromString(trim($item->getTranslation()));
    $this->header = $header;
  }

  /** * Reads a line from the PO stream and stores data internally. * * Expands $this->current_item based on new data for the current item. If * this line ends the current item, it is saved with setItemFromArray() with * data from $this->current_item. * * An internal state machine is maintained in this reader using * $this->context as the reading state. PO items are in between COMMENT * states (when items have at least one line or comment in between them) or * indicated by MSGSTR or MSGSTR_ARR followed immediately by an MSGID or * MSGCTXT (when items closely follow each other). * * @return bool|null * FALSE if an error was logged, NULL otherwise. The errors are considered * non-blocking, so reading can continue, while the errors are collected * for later presentation. */

  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.