View source
<?php
namespace Drupal\Tests\locale\Functional;
use Drupal\Component\Gettext\PoItem;
use Drupal\Core\Database\Database;
use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
use Drupal\Tests\BrowserTestBase;
class LocalePluralFormatTest extends BrowserTestBase {
protected $adminUser;
protected static $modules = [
'locale',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'administer languages',
'translate interface',
'access administration pages',
]);
$this
->drupalLogin($this->adminUser);
}
public function testGetPluralFormat() {
$this
->importPoFile($this
->getPoFileWithSimplePlural(), [
'langcode' => 'fr',
]);
$this
->importPoFile($this
->getPoFileWithComplexPlural(), [
'langcode' => 'hr',
]);
$this
->importPoFile($this
->getPoFileWithMissingPlural(), [
'langcode' => 'fr',
'overwrite_options[not_customized]' => TRUE,
]);
$this
->importPoFile($this
->getPoFileWithBrokenPlural(), [
'langcode' => 'hr',
'overwrite_options[not_customized]' => TRUE,
]);
drupal_static_reset('locale_get_plural');
drupal_static_reset('locale_get_plural:plurals');
drupal_static_reset('locale');
$plural_strings = [
'en' => [
0 => '1 hour',
1 => '@count hours',
],
'fr' => [
0 => '@count heure',
1 => '@count heures',
],
'hr' => [
0 => '@count sat',
1 => '@count sata',
2 => '@count sati',
],
'hu' => [
0 => '1 hour',
-1 => '@count hours',
],
];
$plural_tests = [
'en' => [
1 => 0,
0 => 1,
5 => 1,
123 => 1,
235 => 1,
],
'fr' => [
1 => 0,
0 => 0,
5 => 1,
123 => 1,
235 => 1,
],
'hr' => [
1 => 0,
21 => 0,
0 => 2,
2 => 1,
8 => 2,
123 => 1,
235 => 2,
],
'hu' => [
1 => -1,
21 => -1,
0 => -1,
],
];
foreach ($plural_tests as $langcode => $tests) {
foreach ($tests as $count => $expected_plural_index) {
$this
->assertSame($expected_plural_index, locale_get_plural($count, $langcode), 'Computed plural index for ' . $langcode . ' for count ' . $count . ' is ' . $expected_plural_index);
$expected_plural_index = $count == 1 ? 0 : $expected_plural_index;
$expected_plural_string = str_replace('@count', $count, $plural_strings[$langcode][$expected_plural_index]);
$this
->assertSame($expected_plural_string, \Drupal::translation()
->formatPlural($count, '1 hour', '@count hours', [], [
'langcode' => $langcode,
])
->render(), 'Plural translation of 1 hours / @count hours for count ' . $count . ' in ' . $langcode . ' is ' . $expected_plural_string);
$translated_string = \Drupal::translation()
->translate('1 hour' . PoItem::DELIMITER . '@count hours', [], [
'langcode' => $langcode,
]);
$plural = PluralTranslatableMarkup::createFromTranslatedString($count, $translated_string, [], [
'langcode' => $langcode,
]);
$this
->assertSame($expected_plural_string, $plural
->render());
}
}
}
public function testPluralEditDateFormatter() {
$this
->importPoFile($this
->getPoFileWithSimplePlural(), [
'langcode' => 'fr',
]);
$this
->config('system.site')
->set('default_langcode', 'fr')
->save();
$this->adminUser
->set('created', time() - 1)
->save();
$this
->drupalGet('user');
$this
->assertSession()
->pageTextContains("seconde");
$path = 'admin/config/regional/translate/';
$search = [
'langcode' => 'fr',
'translation' => 'translated',
];
$this
->drupalGet($path);
$this
->submitForm($search, 'Filter');
$this
->assertSession()
->pageTextContains('@count seconde');
$this
->assertSession()
->pageTextContains('@count secondes');
\Drupal::translation()
->formatPlural(1, '1 second', '@count seconds', [], [
'langcode' => 'fr',
])
->render();
$lid = Database::getConnection()
->select('locales_source', 'ls')
->fields('ls', [
'lid',
])
->condition('source', "1 second" . PoItem::DELIMITER . "@count seconds")
->condition('context', '')
->execute()
->fetchField();
$search = [
'string' => '1 second',
'langcode' => 'fr',
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($search, 'Filter');
$edit = [
"strings[{$lid}][translations][0]" => '1 seconde updated',
"strings[{$lid}][translations][1]" => '@count secondes updated',
];
$this
->drupalGet($path);
$this
->submitForm($edit, 'Save translations');
$this
->assertSession()
->pageTextContainsOnce('@count seconds');
$this->adminUser
->set('created', time() - 2)
->save();
$this
->drupalGet('user');
$this
->assertSession()
->pageTextContains("secondes updated");
}
public function testPluralEditExport() {
$this
->importPoFile($this
->getPoFileWithSimplePlural(), [
'langcode' => 'fr',
]);
$this
->importPoFile($this
->getPoFileWithComplexPlural(), [
'langcode' => 'hr',
]);
$this
->drupalGet('admin/config/regional/translate/export');
$this
->submitForm([
'langcode' => 'fr',
], 'Export');
$this
->assertSession()
->pageTextContains('# French translation of Drupal');
$this
->assertSession()
->responseContains("msgid \"Monday\"\nmsgstr \"lundi\"");
$this
->assertSession()
->responseContains("msgid \"1 hour\"\nmsgid_plural \"@count hours\"\nmsgstr[0] \"@count heure\"\nmsgstr[1] \"@count heures\"");
$this
->drupalGet('admin/config/regional/translate/export');
$this
->submitForm([
'langcode' => 'hr',
], 'Export');
$this
->assertSession()
->pageTextContains('# Croatian translation of Drupal');
$this
->assertSession()
->responseContains("msgid \"Monday\"\nmsgstr \"Ponedjeljak\"");
$this
->assertSession()
->responseContains("msgid \"1 hour\"\nmsgid_plural \"@count hours\"\nmsgstr[0] \"@count sat\"\nmsgstr[1] \"@count sata\"\nmsgstr[2] \"@count sati\"");
$this
->drupalGet('admin/config/regional/translate');
$this
->assertSession()
->pageTextContains("1 hour");
$this
->assertSession()
->pageTextContains("@count hours");
$path = 'admin/config/regional/translate/';
$search = [
'langcode' => 'hr',
];
$this
->drupalGet($path);
$this
->submitForm($search, 'Filter');
$this
->assertSession()
->pageTextContains('Singular form');
$this
->assertSession()
->pageTextContains('First plural form');
$this
->assertSession()
->pageTextContains('2. plural form');
$this
->assertSession()
->pageTextNotContains('3. plural form');
$this
->assertSession()
->pageTextContains('@count sat');
$this
->assertSession()
->pageTextContains('@count sata');
$this
->assertSession()
->pageTextContains('@count sati');
$connection = Database::getConnection();
$lid = $connection
->select('locales_source', 'ls')
->fields('ls', [
'lid',
])
->condition('source', "1 hour" . PoItem::DELIMITER . "@count hours")
->condition('context', '')
->execute()
->fetchField();
$edit = [
"strings[{$lid}][translations][1]" => '@count sata edited',
];
$this
->drupalGet($path);
$this
->submitForm($edit, 'Save translations');
$search = [
'langcode' => 'fr',
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($search, 'Filter');
$this
->assertSession()
->pageTextContains('@count heure');
$this
->assertSession()
->pageTextContains('@count heures');
$this
->assertSession()
->pageTextNotContains('2. plural form');
$edit = [
"strings[{$lid}][translations][0]" => '@count heure edited',
];
$this
->drupalGet($path);
$this
->submitForm($edit, 'Save translations');
\Drupal::translation()
->formatPlural(1, '1 day', '@count days', [], [
'langcode' => 'fr',
])
->render();
$lid = $connection
->select('locales_source', 'ls')
->fields('ls', [
'lid',
])
->condition('source', "1 day" . PoItem::DELIMITER . "@count days")
->condition('context', '')
->execute()
->fetchField();
$search = [
'string' => '1 day',
'langcode' => 'fr',
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($search, 'Filter');
$edit = [
"strings[{$lid}][translations][0]" => '1 jour',
"strings[{$lid}][translations][1]" => '@count jours',
];
$this
->drupalGet($path);
$this
->submitForm($edit, 'Save translations');
$search = [
'string' => '1 day',
'langcode' => 'hr',
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($search, 'Filter');
$edit = [
"strings[{$lid}][translations][0]" => '@count dan',
"strings[{$lid}][translations][1]" => '@count dana',
"strings[{$lid}][translations][2]" => '@count dana',
];
$this
->drupalGet($path);
$this
->submitForm($edit, 'Save translations');
$this
->drupalGet('admin/config/regional/translate/export');
$this
->submitForm([
'langcode' => 'fr',
], 'Export');
$this
->assertSession()
->responseContains("msgid \"1 hour\"\nmsgid_plural \"@count hours\"\nmsgstr[0] \"@count heure edited\"\nmsgstr[1] \"@count heures\"");
$this
->assertSession()
->responseContains("msgid \"1 day\"\nmsgid_plural \"@count days\"\nmsgstr[0] \"1 jour\"\nmsgstr[1] \"@count jours\"");
$this
->drupalGet('admin/config/regional/translate/export');
$this
->submitForm([
'langcode' => 'hr',
], 'Export');
$this
->assertSession()
->responseContains("msgid \"1 hour\"\nmsgid_plural \"@count hours\"\nmsgstr[0] \"@count sat\"\nmsgstr[1] \"@count sata edited\"\nmsgstr[2] \"@count sati\"");
$this
->assertSession()
->responseContains("msgid \"1 day\"\nmsgid_plural \"@count days\"\nmsgstr[0] \"@count dan\"\nmsgstr[1] \"@count dana\"\nmsgstr[2] \"@count dana\"");
}
public function importPoFile($contents, array $options = []) {
$file_system = \Drupal::service('file_system');
$name = $file_system
->tempnam('temporary://', "po_") . '.po';
file_put_contents($name, $contents);
$options['files[file]'] = $name;
$this
->drupalGet('admin/config/regional/translate/import');
$this
->submitForm($options, 'Import');
$file_system
->unlink($name);
}
public function getPoFileWithSimplePlural() {
return <<<EOF
msgid ""
msgstr ""
"Project-Id-Version: Drupal 8\\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=UTF-8\\n"
"Content-Transfer-Encoding: 8bit\\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\\n"
msgid "1 hour"
msgid_plural "@count hours"
msgstr[0] "@count heure"
msgstr[1] "@count heures"
msgid "1 second"
msgid_plural "@count seconds"
msgstr[0] "@count seconde"
msgstr[1] "@count secondes"
msgid "Monday"
msgstr "lundi"
EOF;
}
public function getPoFileWithComplexPlural() {
return <<<EOF
msgid ""
msgstr ""
"Project-Id-Version: Drupal 8\\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=UTF-8\\n"
"Content-Transfer-Encoding: 8bit\\n"
"Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;\\n"
msgid "1 hour"
msgid_plural "@count hours"
msgstr[0] "@count sat"
msgstr[1] "@count sata"
msgstr[2] "@count sati"
msgid "Monday"
msgstr "Ponedjeljak"
EOF;
}
public function getPoFileWithMissingPlural() {
return <<<EOF
msgid ""
msgstr ""
"Project-Id-Version: Drupal 8\\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=UTF-8\\n"
"Content-Transfer-Encoding: 8bit\\n"
msgid "Monday"
msgstr "lundi"
EOF;
}
public function getPoFileWithBrokenPlural() {
return <<<EOF
msgid ""
msgstr ""
"Project-Id-Version: Drupal 8\\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=UTF-8\\n"
"Content-Transfer-Encoding: 8bit\\n"
"Plural-Forms: broken, will not parse\\n"
msgid "Monday"
msgstr "Ponedjeljak"
EOF;
}
}