This is a rather new bug as things were working pretty normally not long ago when I was testing installing Drupal in a non-English language. I noticed it while trying to test an unpatched, up-to-date Drupal 8.x checkout to see if we still had the "many notices" we were seeing in this issue: #1751326: When locale import happens on module enable, many notices are thrown.

Symptoms and steps to replicate:

  • Added a few .po files to sites/default/files/translations (imho, this directory should not need to be created by the user, should exist already and have README file there, but that's another issue, perhaps).
  • Installed in non-English language (German) and saw no real notices, but almost nothing was displayed in German after the installation process completed.
  • Enabled some other core modules to see if I could replicate the aforementioned issue
  • Observed a notice [Undefined index: langcode in locale_translate_batch_import() (line 456 of core/modules/locale/locale.bulk.inc) ]
  • Didn't see any additional interface translation happen, nor any other list of notices.

I decided to take a look at that line and think I figured out why it was causing this notice, so made a small tweak and then enabled another core module to see if anything changed. After doing that, I observed normal import of translations, so my change may have fixed this issue (needs review). Now the interface is mostly in German, as expected.

The one-line tweak (simple enough I can paste it here)

diff --git a/core/modules/locale/locale.bulk.inc b/core/modules/locale/locale.bulk.inc
index 02eb3f1..91e8fd1 100644
--- a/core/modules/locale/locale.bulk.inc
+++ b/core/modules/locale/locale.bulk.inc
@@ -453,7 +453,7 @@ function locale_translate_batch_import($filepath, $options, &$context) {
     $file = entity_create('file', array('filename' => $basename, 'uri' => 'translations://'. $basename));
     // We need only the last match, but only if the langcode is not explicitly
     // specified in the $options array.
-    if (!$options['langcode'] && is_array($matches)) {
+    if ((!isset($options['langcode']) || !$options['langcode']) && is_array($matches)) {
       $options['langcode'] = array_pop($matches);

See screenshots:

1) Installed in German, but almost nothing was shown in German after complete:
Not much translated when installing in German

2) Notice after enabling more core modules:
 langcode

3) Minor code tweak and enabled another core module and translation imports finally took place:
Translations imported successfully

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

LoMo’s picture

Status: Active » Needs review
Issue tags: +D8MI
FileSize
737 bytes

And here is the very simple patch... maybe there is more that needs to be done?

Note that I've marked this issue as "major" since this really does seem to block normal import of UI translations so this is a pretty big deal for people who want to have a non-English Drupal installation.

Gábor Hojtsy’s picture

Issue tags: +sprint, +language-ui

Tagging for sprint and language-ui, reproduction welcome :)

YesCT’s picture

I can confirm I see what LoMo saw. I have not tried the patch yet.

I saw a flash of another error, too quick for a screen shot. I'll check the logs, and I was running a vid recording, so I can check that too later.

side note: the translation directory was already there for me.
and I did this while testing:

831 git checkout 8.x
832 history | grep git
833 git reset --hard
834 git pull --rebase
835 git status
836 rm -r core/modules/language/config/language.detection.yml
837 git status
838 git pull --rebase
839 git reset --hard
840 git status
841 history | grep drush
842 drush sql-drop
...
509 cd ~/foo/d8-git
511 cd sites/default/files/
513 cd translations/
515 curl -O http://ftp.drupal.org/files/translations/7.x/drupal/drupal-7.15.de.po

LoMo’s picture

Thank you, YesCT. I look forward to hearing your update re. the patch and if it fully resolves the issue without introducing any others. It's simple enough and the logic that was broken is basic enough that I'm reasonably confident that the one-line (changed) patch, as is, might be all we need. :-)

YesCT’s picture

Summary: I admit, I went through testing like 4 times. I got confused, deleted most of my notes here from being confused... :) (but if people really wanted to see me being confused, there are some vids in my blip.tv of parts of the previous testing of this issue) Finally I had to be very careful to start completely from scratch, and do two sites side by side. [ http://blip.tv/cathytheys/undef-langcode-try4-testing-patch-very-careful... ] I did exactly the same steps and cleared caches occasionally. The difference seems to be that the site with the patch does not have the undef langcode error. (good news!) But the interfaces are translated exactly the same, and they have the same other errors. (kind of bad news: so I didn't see an effect of the patch other than just not seeing the error)

Details:
quick flash of an error (same on site without patch and with patch):

quick-undef-langcode-1785250-2012-09-15_1443.png

Error importing interface translations
Please continue to -link-the error page-link-
An AJAX HTTP request terminated abnormally. Debugging information follows. Path: /d8-git/batch?id=4&op=do_nojs&op=do StatusText: error ResponseText: ReadyState: 0

Errors in the log (might not have anything to do with this issue/patch as they were there before the patch and also afterward):
Referrer http://localhost:8888/d8-git/core/install.php?langcode=de&profile=standa...
Message Warning: fopen() [function.fopen]: Filename cannot be empty in Drupal\Component\Gettext\PoStreamReader->open() (line 168 of /Users/ctheys/foo/d8-git/core/lib/Drupal/Component/Gettext/PoStreamReader.php).

Referrer http://localhost:8888/d8-git/core/install.php?langcode=de&profile=standa...
Message Warning: ftell() expects parameter 1 to be resource, boolean given in Drupal\Component\Gettext\PoStreamReader->open() (line 169 of /Users/ctheys/foo/d8-git/core/lib/Drupal/Component/Gettext/PoStreamReader.php).

Referrer http://localhost:8888/d8-git/core/install.php?langcode=de&profile=standa...
Message Warning: fgets() expects parameter 1 to be resource, boolean given in Drupal\Component\Gettext\PoStreamReader->readLine() (line 263 of /Users/ctheys/foo/d8-git/core/lib/Drupal/Component/Gettext/PoStreamReader.php).

Site with no patch, after enable module shows the error:

Notice: Undefined index: langcode in locale_translate_batch_import() (line 465 of core/modules/locale/locale.bulk.inc).
undef-langcode-nopatch-afterenablemod-2012-09-15_1841.png

With patch: no error there:
undef-langcode-withpatch-afterenablemod-2012-09-15_1841.png

Other notes:
without patch: error log:
undef-langcode-nopatch-langcode-error=2012-09-15_1850.png

with patch: error log:
(can see the undef langcode error is gone, but other fopen, etc, errors the same)
undef-langcode-withpatch-noundeferror-2012-09-15_1851.png

And, just to be clear, the sites had the same strings translated in the no patch and patch cases. (but I had to be very careful to do each step the same, with clearing caches, etc.)

no patch:
[edit: ... missed uploading the right file to show the home page on the no patch site. ]
undef-langcode-nopatch-translatedhome-2012-09-15_1854.png

patch:
undef-langcode-withpatch-samehome-2012-09-15_1855.png

YesCT’s picture

missed one of the files... here it is attached. I'll edit the previous post to put it in the right spot there.

LoMo’s picture

I think you need to try activating some modules... then the descriptions of them will get imported. I saw they weren't imported till I'd "patched" that line... but maybe that was just a fluke?

I'm glad you were able to get a screenshot of that AJAX error message. I do remember seeing it flash by, but didn't have time to see if it was relevant to i18n concerns or just what it was about at all.

I think this issue needs more testing and (probably) more work.

das-peter’s picture

From just a visual review I suggest to use:

if (empty($options['langcode']) && is_array($matches)) {

instead:

if ((!isset($options['langcode']) || !$options['langcode']) && is_array($matches)) {

I think empty() should just work fine and the line would look more clean.
I hope to be able to give a functional review asap.

LoMo’s picture

Re #8: That looks better to me, too, Peter. It was late and I was pretty tired when I first made this change. ;-)
I'm attaching a new patch with your suggested modification.

And since YesCT is not in IRC now, I'll respond here to her comment:

my guess is that you didn't see the strings translated right away because of the cache. And that when you changed the code, and enabled additional modules, that caused the initial batch of strings to translate.

I enabled a couple of modules trying to reproduce the initial issue: #1751326: When locale import happens on module enable, many notices are thrown

Strings were not imported when I saved (see screenshot #2). Then I made my change to the code and enabled another module and that's only when the whole batch of strings was imported. You can see that in my third screenshot (above) "4400 Zeichenketten wurden neu übersetzt" (means "4400 strings were translated"). No notice like that was displayed before the code change, although I had already enabled modules which should have triggered translations.

I'm puzzled by why it looks like you got more string translations on initial installation than I did, but I do think this change (or something similar) is necessary for most string translations to be imported.

LoMo’s picture

(Follow-up)...

I can confirm that the patch does not fix the Ajax error, which I see when enabling new core modules without the patch and with it, too. But still, not much is translated when turning on a group of core modules (without patching) and 4400 strings are imported (including the front-page strings, main toolbar menu, etc) when enabling more modules after applying the patch.

My personal take is that this patch (or something similar) is necessary for this code-block to be entered and we need it. The Ajax error we are seeing might be related, but could probably be a new issue.

I'd like to hear from others regarding reproducing this issue... and I'll also try reproducing it on another machine that has a somewhat different set-up.

Gábor Hojtsy’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

I think we should focus writing an automated test for this fix itself and then look at the AJAX error elsewhere. The fix itself looks good :)

YesCT’s picture

In general, where is the priority on this issue? I could focus on something else since the feature freeze is coming soon. Happy to help where ever.

Yeah, the ajax (and maybe the fopen erros) need their own issues. I can open them unless someone beats me to it.

For the automated test, we need to precisely know what to test (which will also help with the manual testing). :) I need to figure out how to write tests, so I'm up for tackling that too, but it'll be a bit slow since it will be my first automatic testing to write.

One of the things I was thinking that might help testing also is to have a 8.x .po file, so it would be more obvious what english is there because they are new strings in 8.x vs just not having been imported because of a bug.

Ideas of stuff to test to see if the translation happened... maybe a log entry?
Or in the admin bar Structure -> Struktur
Or the description of the block module on the modules page ->
Steuert die visuellen Blöcke aus der eine Seite gebaut wurde. Blöcke sind zusätzliche Inhalte, die in bestimmte Bereiche, Regionen einer Webseite angezeigt werden.

Without the patch, the strings do get translated. ... and I think with the patch they dont get translated any earlier in the process.
So I'm trying to find an exact reproducible series of steps that show the problem and show the patch helps with the problem...

here are the steps (the sXX lines are where in the process I took those screen shots, and some lines are just saying what I did in the interface on the site):
1006 git status
1007 chmod -R u+w sites
1008 rm -r sites
1009 checkout sites
1010 git checkout sites
1011 git status
1012 git diff 8.x
1013 git checkout 8.x
1014 git status
1015 git pull --rebase
1016 git reset --hard
1017 git status
1018 git checkout -b undef-2
1021 mkdir sites/default/files
1022 mkdir sites/default/files/translations
1027 curl -O http://ftp.drupal.org/files/translations/7.x/drupal/drupal-7.15.de.po
1030 mv *.po sites/default/files/translations/
1031 chmod a+w sites/default
install site
s01
s02
s03
s04
1032 drush cc all
s05
aggreg and book module page strings in english.
s06
1033 drush cc all
s07
enable book and contact core modules
s08
enable book and content does trigger translating
s09
s09b

das-peter’s picture

I've just created a spin-off issue that takes care about the failed translation import on installation: #1787520: Translations not imported on installation

Gábor Hojtsy’s picture

So this one only needs tests written. Anybody up for that?

LoMo’s picture

At least in my workflow, this was installing in a non-English language... I thought Simpletests didn't support that. If that hasn't changed, how would we test? (Also, I'm a bit rusty with Simpletests now since I haven't written any in a loooong time and it looks like some things have changed since then; I've been doing a fair bit with PHPUnit lately, but no Simpletests on the projects I've been working on.) So if tests really *are* feasible for this situation, I could help, perhaps, but it would be nice to get some advice.

Gábor Hojtsy’s picture

@LoMo: looking at the patch, locale_translate_batch_import() looks like it would be used as well when enabling modules (when langcode is not passed either). Is it not reproducible in that case?

LoMo’s picture

Hi Gábor,

I'll have to experiment some more. And I think it might also be possible to "install" in English, but change the default language and then add modules and see the import happen. Is that right? But I'm a bit perplexed about why YesCT and I don't have even more similar results from a similar install process. My installations seem to be repeatable (similar), but I want to also see if it's the same on my Macbook. On my "home system" I'm running MAMP (that's where this testing has been so far) and on the Macbook I'm running native Apache/MySQL/PHP installations. I'm wondering if my results might differ on the other machine.

So I'll spend a bit more time manually testing to see what makes sense in terms of automatic tests, then look at some of the other automatic tests and figure out where the logic for this might belong.

LoMo’s picture

Priority: Major » Normal

Update: I need more time to verify (e.g. again on another system), but I think some of the recent patches to the translation import functionality seems to have also fixed the issue that translations were not being imported. The last time I tested, after pulling new code changes, things worked without this patch, although it seemed there was a notice at the top of the page for each new core module enabled (repeats of the "undefined index: langcode"). I also was not able to replicate the issue when installing in English and switching to German or enabling German and importing from the de.po file, etc, so I don't know if this is really testable, even if the issue were still "major" (I would say it is rather minor now, since it seems that all strings are importing without this patch).

Gábor Hojtsy’s picture

Well, there is this issue for translations import in installation: #1787520: Translations not imported on installation. It is not yet resolved. So I'm wondering how come it works for you. Maybe posting some feedback there would be great.

LoMo’s picture

Hi Gábor,

I've been stretched a bit thin on time for pushing along the issues the past couple of weeks, but I'll try to make some time this week to work on better documenting the current state of this issue and seeing if I can reproduce that one. Seems to me that one is likely a duplicate of this one and unless things have changed since my last tests, I think these both might be more-or-less resolved by recent commits (except for the notices).

Gábor Hojtsy’s picture

Status: Needs work » Closed (cannot reproduce)
Issue tags: -sprint

Ok, looks like you cannot reproduce anymore, so setting this accordingly. Please reopen if you can. I have not heard about people with similar problems.

LoMo’s picture

Priority: Normal » Minor
Status: Closed (cannot reproduce) » Postponed

From the title, I thought this (link you posted) *did* sound similar. But I'll see (hopefully this evening) if I can reproduce any of my initial experience or what they describe in the (similar sounding) issue. I suspect that other work has resolved the worst of this issue, though, so while I wouldn't yet mark this "closed (cannot reproduce), based on my last experience (where I still had one instance of the notice for each new module enabled), I *would* say that if the imports are happening now, this becomes a low priority issue (e.g. something that can wait till after feature-freeze to clean up).
#1787520: Translations not imported on installation

So I'm marking this "postponed" and "minor" priority. If we still have notices in December, we can resolve this then, but I think the functionality is no longer broken. (Again, I'll try to quickly verify my latest experience tonight and make sure I still think I'm setting this issue's status appropriately).

mgifford’s picture

Looks like this issue can probably be closed too.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

mpp’s picture

Status: Postponed » Closed (outdated)

But I'll see (hopefully this evening) if I can reproduce any of my initial experience or what they describe in the (similar sounding) issue.

That was seven years ago :-) We should have badges for these :p

Regards to you all!