Exemplo n.º 1
0
 /**
  * Tests locale_get_plural() and \Drupal::translation()->formatPlural()
  * functionality.
  */
 public function testGetPluralFormat()
 {
     // Import some .po files with formulas to set up the environment.
     // These will also add the languages to the system.
     $this->importPoFile($this->getPoFileWithSimplePlural(), array('langcode' => 'fr'));
     $this->importPoFile($this->getPoFileWithComplexPlural(), array('langcode' => 'hr'));
     // Attempt to import some broken .po files as well to prove that these
     // will not overwrite the proper plural formula imported above.
     $this->importPoFile($this->getPoFileWithMissingPlural(), array('langcode' => 'fr', 'overwrite_options[not_customized]' => TRUE));
     $this->importPoFile($this->getPoFileWithBrokenPlural(), array('langcode' => 'hr', 'overwrite_options[not_customized]' => TRUE));
     // Reset static caches from locale_get_plural() to ensure we get fresh data.
     drupal_static_reset('locale_get_plural');
     drupal_static_reset('locale_get_plural:plurals');
     drupal_static_reset('locale');
     // Expected plural translation strings for each plural index.
     $plural_strings = array('en' => array(0 => '1 hour', 1 => '@count hours'), 'fr' => array(0 => '@count heure', 1 => '@count heures'), 'hr' => array(0 => '@count sat', 1 => '@count sata', 2 => '@count sati'), 'hu' => array(0 => '1 hour', -1 => '@count hours'));
     // Expected plural indexes precomputed base on the plural formulas with
     // given $count value.
     $plural_tests = array('en' => array(1 => 0, 0 => 1, 5 => 1, 123 => 1, 235 => 1), 'fr' => array(1 => 0, 0 => 0, 5 => 1, 123 => 1, 235 => 1), 'hr' => array(1 => 0, 21 => 0, 0 => 2, 2 => 1, 8 => 2, 123 => 1, 235 => 2), 'hu' => array(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->assertIdentical(locale_get_plural($count, $langcode), $expected_plural_index, '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->assertIdentical(\Drupal::translation()->formatPlural($count, '1 hour', '@count hours', array(), array('langcode' => $langcode))->render(), $expected_plural_string, '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' . PluralTranslatableMarkup::DELIMITER . '@count hours', array(), array('langcode' => $langcode));
             $plural = PluralTranslatableMarkup::createFromTranslatedString($count, $translated_string, array(), array('langcode' => $langcode));
             $this->assertIdentical($plural->render(), $expected_plural_string);
         }
     }
 }
Exemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function render(ResultRow $values)
 {
     $value = $this->getValue($values);
     if (!empty($this->options['set_precision'])) {
         $value = number_format($value, $this->options['precision'], $this->options['decimal'], $this->options['separator']);
     } else {
         $remainder = abs($value) - intval(abs($value));
         $value = $value > 0 ? floor($value) : ceil($value);
         $value = number_format($value, 0, '', $this->options['separator']);
         if ($remainder) {
             // The substr may not be locale safe.
             $value .= $this->options['decimal'] . substr($remainder, 2);
         }
     }
     // Check to see if hiding should happen before adding prefix and suffix.
     if ($this->options['hide_empty'] && empty($value) && ($value !== 0 || $this->options['empty_zero'])) {
         return '';
     }
     // 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');
 }