Commit 33a1baaf authored by catch's avatar catch

Issue #2600396 by mpdonadio, justAChris, tstoeckler, skyredwang, jhodgdon,...

Issue #2600396 by mpdonadio, justAChris, tstoeckler, skyredwang, jhodgdon, Gábor Hojtsy, Matze202: DateFormatter::formatDiff passes an invalid language code TranslationInterface::formatPlural() leading to duplicated translations and untranslatable strings
parent e51d9dec
......@@ -134,6 +134,11 @@ public function translateString(TranslatableMarkup $translated_string) {
* The translated string.
*/
protected function doTranslate($string, array $options = array()) {
// If a NULL langcode has been provided, unset it.
if (!isset($options['langcode']) && array_key_exists('langcode', $options)) {
unset($options['langcode']);
}
// Merge in options defaults.
$options = $options + [
'langcode' => $this->defaultLangcode,
......
......@@ -144,6 +144,63 @@ public function testGetPluralFormat() {
}
}
/**
* Tests plural editing of DateFormatter strings
*/
public function testPluralEditDateFormatter() {
// 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',
));
// Set French as the site default language.
$this->config('system.site')->set('default_langcode', 'fr')->save();
// Visit User Info page before updating translation strings.
$this->drupalGet('user');
// Member for time should be translated.
$this->assertText("seconde", "'Member for' text is translated.");
$path = 'admin/config/regional/translate/';
$search = array(
'langcode' => 'fr',
);
$this->drupalPostForm($path, $search, t('Filter'));
// Plural values for the langcode fr.
$this->assertText('@count seconde');
$this->assertText('@count secondes');
// Inject a plural source string to the database. We need to use a specific
// langcode here because the language will be English by default and will
// not save our source string for performance optimization if we do not ask
// specifically for a language.
\Drupal::translation()->formatPlural(1, '1 second', '@count seconds', array(), array('langcode' => 'fr'))->render();
$lid = db_query("SELECT lid FROM {locales_source} WHERE source = :source AND context = ''", array(':source' => "1 second" . LOCALE_PLURAL_DELIMITER . "@count seconds"))->fetchField();
// Look up editing page for this plural string and check fields.
$search = array(
'string' => '1 second',
'langcode' => 'fr',
);
$this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
// Save complete translations for the string in langcode fr.
$edit = array(
"strings[$lid][translations][0]" => '1 seconde updated',
"strings[$lid][translations][1]" => '@count secondes updated',
);
$this->drupalPostForm($path, $edit, t('Save translations'));
// User interface input for translating seconds should not be duplicated
$this->assertUniqueText('@count seconds', 'Interface translation input for @count seconds only appears once.');
// Member for time should be translated.
$this->drupalGet('user');
$this->assertText("seconde", "'Member for' text is translated.");
}
/**
* Tests plural editing and export functionality.
*/
......@@ -308,6 +365,11 @@ public function getPoFileWithSimplePlural() {
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;
......
......@@ -43,6 +43,8 @@ public function providerTestFormatPlural() {
// @todo support locale_get_plural
[2, 'Singular', '@count @arg', array('@arg' => '<script>'), array(), '2 &lt;script&gt;'],
[2, 'Singular', '@count %arg', array('%arg' => '<script>'), array(), '2 <em class="placeholder">&lt;script&gt;</em>'],
[1, 'Singular', '@count plural', array(), array('langcode' => NULL), 'Singular'],
[1, 'Singular', '@count plural', array(), array('langcode' => 'es'), 'Singular'],
);
}
......@@ -50,12 +52,15 @@ public function providerTestFormatPlural() {
* @dataProvider providerTestFormatPlural
*/
public function testFormatPlural($count, $singular, $plural, array $args = array(), array $options = array(), $expected) {
$langcode = empty($options['langcode']) ? 'fr' : $options['langcode'];
$translator = $this->getMock('\Drupal\Core\StringTranslation\Translator\TranslatorInterface');
$translator->expects($this->once())
->method('getStringTranslation')
->will($this->returnCallback(function ($langcode, $string) {
->with($langcode, $this->anything(), $this->anything())
->will($this->returnCallback(function ($langcode, $string, $context) {
return $string;
}));
$this->translationManager->setDefaultLangcode('fr');
$this->translationManager->addTranslator($translator);
$result = $this->translationManager->formatPlural($count, $singular, $plural, $args, $options);
$this->assertEquals($expected, $result);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment