You are here

public function LocalePluralFormatTest::testGetPluralFormat in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/locale/src/Tests/LocalePluralFormatTest.php \Drupal\locale\Tests\LocalePluralFormatTest::testGetPluralFormat()

Tests locale_get_plural() and \Drupal::translation()->formatPlural() functionality.

File

core/modules/locale/src/Tests/LocalePluralFormatTest.php, line 41
Contains \Drupal\locale\Tests\LocalePluralFormatTest.

Class

LocalePluralFormatTest
Tests plural handling for various languages.

Namespace

Drupal\locale\Tests

Code

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(
    // English is not imported in this case, so we assume built-in text
    // and formulas.
    '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',
    ),
    // Hungarian is not imported, so it should assume the same text as
    // English, but it will always pick the plural form as per the built-in
    // logic, so only index -1 is relevant with the plural value.
    '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);
    }
  }
}