Esempio n. 1
0
 /**
  * Tests that plural expressions are evaluated correctly.
  *
  * Validate that the given plural expressions is evaluated with the correct
  * plural formula.
  *
  * @param string $plural
  *   The plural expression.
  * @param array $expected
  *   Array of expected plural positions keyed by plural value.
  *
  * @dataProvider providerTestPluralsFormula
  */
 public function testPluralsFormula($plural, $expected)
 {
     $p = new PoHeader();
     $parsed = $p->parsePluralForms($plural);
     list($nplurals, $new_plural) = $parsed;
     foreach ($expected as $number => $plural_form) {
         $result = isset($new_plural[$number]) ? $new_plural[$number] : $new_plural['default'];
         $this->assertEquals($result, $plural_form, 'Difference found at ' . $number . ': ' . $plural_form . ' versus ' . $result);
     }
 }
 /**
  * Implements Drupal\Component\Gettext\PoMetadataInterface::setHeader().
  *
  * Sets the header and configure Drupal accordingly.
  *
  * Before being able to process the given header we need to know in what
  * context this database write is done. For this the options must be set.
  *
  * A langcode is required to set the current header's PluralForm.
  *
  * @param \Drupal\Component\Gettext\PoHeader $header
  *   Header metadata.
  *
  * @throws Exception
  */
 public function setHeader(PoHeader $header)
 {
     $this->header = $header;
     $locale_plurals = \Drupal::state()->get('locale.translation.plurals') ?: array();
     // Check for options.
     $options = $this->getOptions();
     if (empty($options)) {
         throw new \Exception('Options should be set before assigning a PoHeader.');
     }
     $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))) {
             list($nplurals, $formula) = $p;
             $locale_plurals[$langcode] = array('plurals' => $nplurals, 'formula' => $formula);
             \Drupal::state()->set('locale.translation.plurals', $locale_plurals);
         }
     }
 }
Esempio n. 3
0
 /**
  * Read the header from the PO stream.
  *
  * The header is a special case PoItem, using the empty string as source and
  * key-value pairs as translation. We just reuse the item reader logic to
  * read the header.
  */
 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;
 }
 /**
  * Test plural formatting setting on a numeric views handler.
  */
 function testNumericFormatPlural()
 {
     // Create a file.
     $file = $this->createFile();
     // Assert that the starting configuration is correct.
     $config = $this->config('views.view.numeric_test');
     $field_config_prefix = 'display.default.display_options.fields.count.';
     $this->assertEqual($config->get($field_config_prefix . 'format_plural'), TRUE);
     $this->assertEqual($config->get($field_config_prefix . 'format_plural_string'), '1' . LOCALE_PLURAL_DELIMITER . '@count');
     // Assert that the value is displayed.
     $this->drupalGet('numeric-test');
     $this->assertRaw('<span class="field-content">0</span>');
     // Assert that the user interface has controls to change it.
     $this->drupalGet('admin/structure/views/nojs/handler/numeric_test/page_1/field/count');
     $this->assertFieldByName('options[format_plural_values][0]', '1');
     $this->assertFieldByName('options[format_plural_values][1]', '@count');
     // Assert that changing the settings will change configuration properly.
     $edit = ['options[format_plural_values][0]' => '1 time', 'options[format_plural_values][1]' => '@count times'];
     $this->drupalPostForm(NULL, $edit, t('Apply'));
     $this->drupalPostForm(NULL, array(), t('Save'));
     $config = $this->config('views.view.numeric_test');
     $field_config_prefix = 'display.default.display_options.fields.count.';
     $this->assertEqual($config->get($field_config_prefix . 'format_plural'), TRUE);
     $this->assertEqual($config->get($field_config_prefix . 'format_plural_string'), '1 time' . LOCALE_PLURAL_DELIMITER . '@count times');
     // Assert that the value is displayed with some sample values.
     $numbers = [0, 1, 2, 3, 4, 42];
     foreach ($numbers as $i => $number) {
         \Drupal::service('file.usage')->add($file, 'views_ui', 'dummy', $i, $number);
     }
     $this->drupalGet('numeric-test');
     foreach ($numbers as $i => $number) {
         $this->assertRaw('<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->drupalPostForm('admin/config/regional/language/add', $edit, t('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();
     list($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->assertFieldByName('options[format_plural_values][0]', '1 time');
     $this->assertFieldByName('options[format_plural_values][1]', '@count times');
     $this->assertFieldByName('options[format_plural_values][2]', '');
     $this->assertFieldByName('options[format_plural_values][3]', '');
     // Assert that changing the settings will change configuration properly.
     $edit = ['options[format_plural_values][0]' => '@count time0', 'options[format_plural_values][1]' => '@count time1', 'options[format_plural_values][2]' => '@count time2', 'options[format_plural_values][3]' => '@count time3'];
     $this->drupalPostForm(NULL, $edit, t('Apply'));
     $this->drupalPostForm(NULL, array(), t('Save'));
     $config = $this->config('views.view.numeric_test');
     $field_config_prefix = 'display.default.display_options.fields.count.';
     $this->assertEqual($config->get($field_config_prefix . 'format_plural'), TRUE);
     $this->assertEqual($config->get($field_config_prefix . 'format_plural_string'), implode(LOCALE_PLURAL_DELIMITER, array_values($edit)));
     // The view should now use the new plural configuration.
     $this->drupalGet('sl/numeric-test');
     $this->assertRaw('<span class="field-content">0 time3</span>');
     $this->assertRaw('<span class="field-content">1 time0</span>');
     $this->assertRaw('<span class="field-content">2 time1</span>');
     $this->assertRaw('<span class="field-content">3 time2</span>');
     $this->assertRaw('<span class="field-content">4 time2</span>');
     $this->assertRaw('<span class="field-content">42 time3</span>');
     // Add an English configuration translation with English plurals.
     $english = \Drupal::languageManager()->getLanguageConfigOverride('en', 'views.view.numeric_test');
     $english->set('display.default.display_options.fields.count.format_plural_string', '1 time' . LOCALE_PLURAL_DELIMITER . '@count times')->save();
     // The view displayed in English should use the English translation.
     $this->drupalGet('numeric-test');
     $this->assertRaw('<span class="field-content">0 times</span>');
     $this->assertRaw('<span class="field-content">1 time</span>');
     $this->assertRaw('<span class="field-content">2 times</span>');
     $this->assertRaw('<span class="field-content">3 times</span>');
     $this->assertRaw('<span class="field-content">4 times</span>');
     $this->assertRaw('<span class="field-content">42 times</span>');
 }