Exemplo n.º 1
0
 /**
  * Tests locale_get_plural() and format_plural() 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(format_plural($count, '1 hour', '@count hours', array(), array('langcode' => $langcode)), $expected_plural_string, 'Plural translation of 1 hours / @count hours for count ' . $count . ' in ' . $langcode . ' is ' . $expected_plural_string);
         }
     }
 }
Exemplo n.º 2
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);
         }
     }
 }
function format_plural($count, $singular, $plural, $args = array(), $langcode = NULL)
{
    $args['@count'] = $count;
    if ($count == 1) {
        return t($singular, $args, $langcode);
    }
    // Get the plural index through the gettext formula.
    $index = function_exists('locale_get_plural') ? locale_get_plural($count, $langcode) : -1;
    // Backwards compatibility.
    if ($index < 0) {
        return t($plural, $args, $langcode);
    } else {
        switch ($index) {
            case "0":
                return t($singular, $args, $langcode);
            case "1":
                return t($plural, $args, $langcode);
            default:
                unset($args['@count']);
                $args['@count[' . $index . ']'] = $count;
                return t(strtr($plural, array('@count' => '@count[' . $index . ']')), $args, $langcode);
        }
    }
}
 /**
  * Gets the plural index through the gettext formula.
  *
  * @return int
  */
 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;
 }
Exemplo n.º 5
0
 /**
  * Gets the plural index through the gettext formula.
  *
  * @return int
  */
 protected function getPluralIndex()
 {
     if (!isset(static::$localeEnabled)) {
         static::$localeEnabled = function_exists('locale_get_plural');
     }
     if (function_exists('locale_get_plural')) {
         return locale_get_plural($this->count, $this->getOption('langcode'));
     }
     return -1;
 }
Exemplo n.º 6
0
 /**
  * {@inheritdoc}
  */
 public function formatPluralTranslated($count, $translation, array $args = array(), array $options = array())
 {
     $args['@count'] = $count;
     $translated_array = explode(LOCALE_PLURAL_DELIMITER, $translation);
     if ($count == 1) {
         return SafeMarkup::format($translated_array[0], $args);
     }
     // Get the plural index through the gettext formula.
     // @todo implement static variable to minimize function_exists() usage.
     $index = function_exists('locale_get_plural') ? locale_get_plural($count, isset($options['langcode']) ? $options['langcode'] : NULL) : -1;
     if ($index == 0) {
         // Singular form.
         $return = $translated_array[0];
     } else {
         if (isset($translated_array[$index])) {
             // N-th plural form.
             $return = $translated_array[$index];
         } else {
             // If the index cannot be computed or there's no translation, use
             // the second plural form as a fallback (which allows for most flexibility
             // with the replaceable @count value).
             $return = $translated_array[1];
         }
     }
     return SafeMarkup::format($return, $args);
 }
 /**
  * {@inheritdoc}
  */
 public function formatPlural($count, $singular, $plural, array $args = array(), array $options = array())
 {
     $args['@count'] = $count;
     // Join both forms to search a translation.
     $translatable_string = implode(LOCALE_PLURAL_DELIMITER, array($singular, $plural));
     // Translate as usual.
     $translated_strings = $this->translate($translatable_string, $args, $options);
     // Split joined translation strings into array.
     $translated_array = explode(LOCALE_PLURAL_DELIMITER, $translated_strings);
     if ($count == 1) {
         return SafeMarkup::set($translated_array[0]);
     }
     // Get the plural index through the gettext formula.
     // @todo implement static variable to minimize function_exists() usage.
     $index = function_exists('locale_get_plural') ? locale_get_plural($count, isset($options['langcode']) ? $options['langcode'] : NULL) : -1;
     if ($index == 0) {
         // Singular form.
         $return = $translated_array[0];
     } else {
         if (isset($translated_array[$index])) {
             // N-th plural form.
             $return = $translated_array[$index];
         } else {
             // If the index cannot be computed or there's no translation, use
             // the second plural form as a fallback (which allows for most flexibility
             // with the replaceable @count value).
             $return = $translated_array[1];
         }
     }
     return SafeMarkup::set($return);
 }