Tests locale_get_plural() and format_plural() functionality.
function testGetPluralFormat() {
// Import some .po files with formulas to set up the environment.
// These will also add the languages to the system and enable them.
$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,
),
'fr' => array(
1 => 0,
0 => 0,
5 => 1,
),
'hr' => array(
1 => 0,
21 => 0,
0 => 2,
2 => 1,
8 => 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);
}
}
}