Problem/Motivation

When extracting translations with the option "Include translations" checked, the translations for plural strings are not filled in the export.

Expected result:

#: templates/test.html.twig
msgid "This is a plural test string with a single result."
msgid_plural "This is a plural test string with @num results."
msgstr[0] "Dit is een test string met een enkel resultaat."
msgstr[1] "Dit is een test string met @num resultaten"

Actual result:

#: templates/test.html.twig
msgid "This is a plural test string with a single result."
msgid_plural "This is a plural test string with @num results."
msgstr[0] ""
msgstr[1] ""

Comments

BryanDeNijs created an issue. See original summary.

bryandenijs’s picture

StatusFileSize
new1.99 KB

I did some debugging and noticed that the single version and the plural version of the string are stored in the database in a single row, delimited with a "\03" character. By extracting the translation string, the module only search for the single or plural version of the string (not glued together).

Maybe not the most clean way to fix it, but this patch works for me.

bryandenijs’s picture

Status: Active » Needs review
bryandenijs’s picture

Title: Plural translations are missing translation. » Plural translation values are not exported.
idebr’s picture

Status: Needs review » Reviewed & tested by the community

Patch works as expected, thanks!

hctom’s picture

Patch applies cleanly against latest 8.x-1.x-dev and works as expected. But I guess this still needs some tests, doesn't it?

In addition to that, this patch collides with the automated Drupal 9 compatibility patch from #3148463: Automated Drupal 9 compatibility fixes. Perhaps there should be made some progress first, before getting this done here.

penyaskito’s picture

Related: #2918489: Plurals are not exported correctly when exporting source translations

+++ b/potx.inc
@@ -615,12 +615,23 @@ function _potx_translation_export($translation_export_langcode, $string, $plural
+        $string = $string . "\03" . $plural;
+        if ($translation = db_query("SELECT t.translation FROM {locales_source} s LEFT JOIN {locales_target} t ON t.lid = s.lid WHERE s.source = :source AND t.{$language_column} = :langcode", array(':source' => $string, ':langcode' => $translation_export_langcode))->fetchField()) {

What about when there are more plurals?

Can we use the constant PoItem::DELIMITER here instead of hardcoding "\03"?

That won't work for sites in a version older than 8.7 (https://www.drupal.org/node/3014611), but I think that's something we can assume?

gábor hojtsy’s picture

@penyaskito: re

What about when there are more plurals?

Here we are looking at source strings and not translations, and the source would always only ever have a singular and singular and one plural, right?

penyaskito’s picture

Sure, all sources should be English per Drupal standards, missed that :-)

gábor hojtsy’s picture

Status: Reviewed & tested by the community » Needs work

So I committed #3148463: Automated Drupal 9 compatibility fixes already which means this needs work because db_query() is not in Drupal 9 anymore and otherwise the code supports Drupal 9 and declares that it supports Drupal 9.

bryandenijs’s picture

I will try to write a new patch after the weekend!

bryandenijs’s picture

Status: Needs work » Needs review
StatusFileSize
new2.25 KB

Sorry it took some time, but here is a new patch based on the latest 8.x-1.x-dev branch.

gábor hojtsy’s picture

Looks good to me but another pair of eyes would be useful before commit :)

bryandenijs’s picture

StatusFileSize
new621 bytes
new2.25 KB

Whoops, someone notified me there was a problem in the query condition.
Here is a new fix.

idebr’s picture

Status: Needs review » Reviewed & tested by the community

The feedback in #7 and #10 have been adressed. Patch still works as expected, so setting to RTBC. Thanks Bryan :)

gngn’s picture

#14 worked for me. Thanks.

hoanns’s picture

Thanks #14 is good and should be merged

hoanns’s picture

StatusFileSize
new2.38 KB
new533 bytes

Whoops no there was still a bug, it didnt correctly add msgstr[0] "" msgstr[1] "" if there was no translation found. This created a broken po file.

Here is the patch

bryandenijs’s picture

@hoanns: Yes, you are right. Thanks for the fix!

bryandenijs’s picture

idebr’s picture

Patch #18 still applies to the latest dev after 1.0.0 was tagged

idebr’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new681 bytes
new2.36 KB

The source string for languages with more than 2 plural forms is incorrect in #18. It is always the English singular and plural string.

Attached patch fixed the source string for languages with more than 2 plural forms.