When the user wants to extract a .po file with translations, if there are two or more identical strings with different contexts and different translations, the translation fetched from database is the same for each string source.
The reason for this is, in the query to obtain the translation, in the WHERE clause:
- the context isn't checked.

I've attached a patch with a fix.

Issue fork potx-1299966

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Status: Needs review » Needs work

The last submitted patch, potx.inc_.patch, failed testing.

Alex.Enciu’s picture

StatusFileSize
new2.15 KB
Alex.Enciu’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, potx.inc_.patch, failed testing.

torotil’s picture

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

I redid the patch with git diff - this should work better with our test-bot.

pbuyle’s picture

I made the same changes before finding this issue. Works for me.

gábor hojtsy’s picture

Status: Needs review » Needs work

Looks like a great patch. We are attempting to make the potx.inc file be a library that can be reused among different major Drupal versions. Putting this change in would not allow us to do that. So it would be great to add some conditions on the major Drupal version being run and only add the context if it is >= 7.

jacobfriis’s picture

Patch based on Alex.Enciu and torotil's work (#5), which attempts to accomplish the Drupal version conditional requirement (Gábor #7).

Created against current dev (7.x-1.x 8ac54a17480f9fa41ce07fd38642bca809e49a8a) but also appears to work against 7.x-1.0.

jacobfriis’s picture

Version: 7.x-1.0 » 7.x-1.x-dev
Status: Needs work » Needs review

For some reason neither of these patches get applied (in my context ;-) when added to the .make of an install profile; whereas other patches get applied just fine. And no error is logged, drush (5.8) pretends that everything is swell and adds a PATCHES.txt in potx' dir. However they do work with drush 6.0 (on a Windows box!).
But that's another story ;-)

idebr’s picture

StatusFileSize
new4.94 KB

Reroll against the latest 7.x-3.x-dev release.

idebr’s picture

Version: 7.x-1.x-dev » 7.x-3.x-dev
PascalAnimateur’s picture

I'm trying to extract the translations for various features modules I've created (mostly with custom content types), but I can't get a single msgctxt out of this module, even with patch #10 applied to clean 3.x-dev.

This is probably due to the fact that I'm using title / entity_translation / localize_fields to translate my content types.

Anybody know if this could be the case? In what scenarios are you getting context properly extracted?

jacobfriis’s picture

@PascalAnimateur
Did you
drush localize-fields sites/all/modules/my_features_module
?
A Features module export doesn't supply contextual translation of field labels, descriptions, allowed values.
The Localize Fields drush script fixes that.

PascalAnimateur’s picture

@jacobfriis hum... I should have RTFM !! Maybe I'll go back to using localize_fields instead of i18n_field after all...

duaelfr’s picture

Patch in #10 do the job! Thank you! You saved me hours of headache!

The patch looks good. I'm surprised that there is not an easier way to find out which core version we are running on. What about reading the system module info file?

gábor hojtsy’s picture

@DuaelFr: there is no version information in that file: http://cgit.drupalcode.org/drupal/tree/modules/system/system.info?h=7.x

duaelfr’s picture

@Gábor Hojtsy
There is at least core = 7.x that can help finding the major version.
More, if the version has been downloaded from d.o, the buildbot also adds version = "7.38" but I'm not sure we need to be so precise.

If we really need to be backward compatible with versions older than Drupal 6 that can be a problem as this "core" key has been introduced in D6.

idebr’s picture

Note: the patch does not fix the extract for plural translations.

dmsmidt’s picture

It does work for me in D7.

idebr’s picture

Version: 7.x-3.x-dev » 8.x-1.x-dev
StatusFileSize
new4.54 KB

Issue still occurs in 8.x-1.x as well. Attached patch applies to 8.x-1.x.

super_romeo’s picture

Status: Needs review » Needs work

Patch Failed to Apply

spadxiii’s picture

StatusFileSize
new4.58 KB

Did a quick re-roll of the patch against latest dev.

gábor hojtsy’s picture

+++ b/potx.inc
@@ -444,6 +444,23 @@ function potx_finish_processing($save_callback = '_potx_save_string', $api_versi
+  // Get Drupal core major version, to establish translation context support.
+  $core_version_major = 0;
+  if (defined('VERSION')) {
+    // 4.7, 5, 6, 7.
+    $core_version_major = VERSION;
+  }
+  elseif (class_exists('\\Drupal') && defined('\\Drupal::VERSION')) {
+    // 8.
+    $core_version_major = \Drupal::VERSION;
+  }
+  if ($core_version_major) {
+    if (($pos = strpos($core_version_major, '-'))) {
+      $core_version_major = substr($core_version_major, 0, $pos);
+    }
+    $core_version_major = (int) floor($core_version_major);
+  }
+

This assumes potx is run inside Drupal with the given version. That may or may not be true depending on how you run potx (it can be run as an independent project and can be run against codebases of other projects etc).

I would simply look at the SQL table if it has a context column or not and then carry that info over to later.

Re @idebr: Plurals are handled in #3013103: Plural translation values are not exported..

dimiter’s picture

I have re-rolled patch #22 from @SpadXIII against the current 7.x-3.x (dev) of potx (90afb499264672b34cb28a433aa38bf880263706). I did not implement the suggested change of Gábor Hojtsy from #23.

timohuisman’s picture

StatusFileSize
new4.6 KB

I've updated the patch from #22 to 8.x-1.0 to add Drupal 9 compatibility, see https://www.drupal.org/node/3148463

gngn’s picture

#25 worked for me (with potx 8.x-1.0-alpha1).

fernly’s picture

I tried patch #25 for a twig translation with context such as {{ "Text to translate"|t({}, {'context' : 'Context name'}) }}
The context was not picked up in this case.

idebr’s picture

Status: Needs work » Needs review
StatusFileSize
new623 bytes
new4.59 KB

Attached fixed the test failures with the patch from #25

#23 still needs to be addressed
#27 still needs to be addressed

mpp’s picture

Status: Needs review » Needs work

Marking as needs work due to
#23 still needs to be addressed
#27 still needs to be addressed

gngn’s picture

#28 worked for me (with potx 8.x-1.0).

geek-merlin made their first commit to this issue’s fork.

geek-merlin’s picture

Assigned: Unassigned » geek-merlin
Priority: Normal » Critical
Issue tags: +DrupalWTF

Ran into this when a wrong translation made it into prod. Setting critical as of data loss. (If s.o. bikesheds it to major, i won't argue.)

Notes:
- Code concerning Drupal 6 is not needed anymore (i.e. while D6 modules can still be extracted, the patch is for D8+ and can be as-is backported to D7, and no localization site should run D6 today.)
- Which obsoletes #23.
- #27 is obviously a different bug. Please file it as such.
- Also, the current patch misses one query (w/ plurals code path)

geek-merlin’s picture

Assigned: geek-merlin » Unassigned
Status: Needs work » Needs review

- Added MR with the simplified code to review and test.
- Verified that the patch applies and fixes the issue for me.

geek-merlin’s picture

Status: Needs review » Needs work

Ups, merge error? The MR seems to be onto 7.x? I guess this issue was re-purposed 7 / 8. Or whatever.

geek-merlin’s picture

Status: Needs work » Needs review

So ignore that MR !23, it's MR !24 ftw.

geek-merlin’s picture

OK, tests are green, off for today.

espurnes’s picture

Hello,

Any update on the #27 issue?
Is there any other issue thread addressing that?

Thanks

rcodina’s picture

I have tested MR !24 and it works like a charm.

I confirm that #27 is still an issue (literal gets extracted but with no context) but this other way works fine:

{% trans with {'context': 'Context name'} %}
    Text to translate
{% endtrans %}
theanisotropic’s picture

Hello! I've created a patch in issue 2816601, a reroll of a previous patch in the same issue, which attempts to fix the problem mentioned in #27. It is for the current dev version 8.x-1.x.