I have an own module with the translation file in the module's translation folder. I install it during installation as a dependency of the install profile. l10n_update is also a dependency.
I have an install task to import the translation:
/**
* Implement hook_install_tasks().
*/
function zenede_profile_install_tasks($install_state) {
return array(
'zenede_profile_install_import_translation' => array(
'display_name' => st('Set up translations'),
'display' => TRUE,
'run' => INSTALL_TASK_RUN_IF_NOT_COMPLETED,
'type' => 'batch',
),
);
}
/**
* Installation step callback.
*
* @param $install_state
* An array of information about the current installation state.
*/
function zenede_profile_install_import_translation(&$install_state) {
// Enable installation language as default site language.
include_once DRUPAL_ROOT . '/includes/locale.inc';
$install_locale = $install_state['parameters']['locale'];
locale_add_language($install_locale, NULL, NULL, NULL, '', NULL, 1, TRUE);
// Build batch with l10n_update module.
$history = l10n_update_get_history();
module_load_include('check.inc', 'l10n_update');
$available = l10n_update_available_releases();
$updates = l10n_update_build_updates($history, $available);
module_load_include('batch.inc', 'l10n_update');
$updates = _l10n_update_prepare_updates($updates, NULL, array());
$batch = l10n_update_batch_multiple($updates, LOCALE_IMPORT_KEEP);
return $batch;
}
However, it only imports from the remote server, not from the local folders. I took a look at the code and it seems to me that this is not the intended behaviour.
Comments
Comment #1
gábor hojtsyl10n_update would import .po files from one central directory, not from under a module's translation folder. The later method was deprecated by the drupal.org git deployment and is not supported in Drupal 8 anymore. Just ship those files in a central translations directory and configure the module to use that.
Comment #2
czigor commentedI've created a /translations directory and copied all the po files there. But how do I configure my module to search for its translation files there?
Comment #3
gábor hojtsyYou can just variable_set() the translations directory in your profile. Check the l10n_update setting page for the variable name.
Comment #4
czigor commentedThanks for your answers! I have managed to get it work.
I have put all my translation files in sites/all/translations. It is important that the module "mymodule" should have the
project = "mymodule"
version = "mymodule-7.x-1.0"version = "7.x-1.0"
lines in its info file. Without these the module does not appear in the admin/config/regional/translate/update list. The po file should be "sites/all/translations/mymodule-7.x-1.0.hu.po".
I have added the following lines to my profile (these are all the variables I have found that start with l10n_update):
variable_set('l10n_update_download_store', 'sites/all/translations');
variable_set('l10n_update_check_disabled', '0');
variable_set('l10n_update_check_frequency', '0');
variable_set('l10n_update_check_mode', '3');
variable_set('l10n_update_import_mode', '1');
Comment #5
czigor commentedThe problem with this solution is that po files don't get imported from the sites/all/translations folder on module install.
So what is the right way to do this? Are we supposed to keep the po files of the modules that are enabled from the profile in sites/all/translations and those of other modules in mymodule/translations?
Comment #7
gábor hojtsy@czigor: you should not have .po files under modules anymore, all .po files for a site should be all in one directory and identified and tied to projects by their names.
Comment #8
gábor hojtsy@czigor: if the problem is that when you enable random modules, their .po files are not imported right away, that would possible need a hook implementation in l10n_update (if its not already there) to do an import which would then look for all the localization files again with the new module list in mind.
Comment #9
czigor commented#8: Yes, that is the problem.
So this means that we should implement hook_modules_enabled in l10n_update? (According to a quick 'grep modules_enabled' it is not implemented yet.)
In this case I don't really understand why and how the translations of d.o. contrib modules are downloaded when we enable them. That is: what's the difference between enabling a contrib and a random module? Is there a list of po files in sites/all/translations/ that needs to be refreshed?
Comment #10
gábor hojtsyWell, currently they are downloaded via cron or manual updates only. This looks like a very important feature request, yes. Retitling for that.
Comment #11
sutharsan commentedTranslations are downloaded using an additional submit handler on the Modules page. This patch changes that to an implementation of
hook_modules_enabled(). Have a try with this.Comment #12
sutharsan commentedFixed a faulty function name.
Tested to work with enabling modules from both the module page and with drush.
Comment #13
czigor commentedThanks Sutharsan! It's working with enabling modules from module page, drush and install profile too.
Comment #14
czigor commentedNow it seems to me that it is not working. When the patch is applied I get a completely white page after the translations import phase of my custom install profile. However, I can go to the site as if the install had succeeded but I see this notice:
Notice: Undefined index: naplart_subject_hacks l10n_update_project_refresh() függvényben (/home/czigor/public_html/zenede/sites/all/modules/patched/l10n_update/l10n_update.project.inc 139 sor).
The naplart_subject_hacks has neither a project line in the naplart_subject_hacks.info file nor a po file anywhere.
Other translations seem to get imported just right.
When the patch is not applied the translations are not imported but I don't have the aformentioned completely white page.
Comment #15
sutharsan commentedThis patch should do better.
Comment #16
sutharsan commented@czigor, can you test again with the last patch?
Comment #17
czigor commentedWorks:
1. Site-install-time import of translations for modules that are install profile dependencies (both contrib and custom modules).
2. Module-enable-time import of translations for any module.
What does not work:
I have modules that I enable in my install profile NOT via dependency but by calling module_enable(). (If it's interesting why I do this I can go into details but I think it's irrelevant.) The translations of these modules do not get imported. These are custom modules with po files in sites/all/translations.
I can import these translations at admin/config/regional/translate/update after site install without any problem.
Thanks a lot for working on this!
Comment #18
czigor commentedWhen the patch is applied and
drush en views_ui
I get the following warning:
Invalid argument supplied for foreach() l10n_update.check.inc:56 [warning]
Probably just a misconfigured files folder.
Comment #19
sutharsan commentedIf you want to import translations of custom modules, you have to do the following:
Note that I fixed some relevant documentation typos just an hour ago.
I'm not sure if your method of enabling module will be supported. Enabling a module during the enable cycle of another module sounds awkward.
Comment #20
czigor commentedIt's not inside another enable cycle but an install task in which I call module_enable().
(Sidenote, not sure if it's relevant: I need to do this for modules that depend on a feature that would be overriden if I just made them as a dependecy of the install profile, see
http://kybest.hu/en/blog/installation-profile-default-features-instead-o...)
Comment #21
sutharsan commentedAh, that clarifies the situation. I've created a tiny custom module. In the install file the code:
Works as advertized. Enable the custom module and Search 404 and Module Filter are enabled and translations are downloaded and installed.
To import local translations, just follow the instruction in #23. No need for
zenede_profile_install_tasks().Comment #22
czigor commentedI can still see 2 problems:
1. My local translations do not get imported if the module is enabled in an install profile task. I'm not importing the translations by hand in the install profile, just calling module_enable().
My translations are in sites/all/translations folder, the corresponding modules' info files have the necessary project and version lines which is confirmed by the fact that after install they appear on the admin/config/regional/translate/update page and from here the translations get imported correctly. (I'm not sure what #23 in the previous comment is referring to.)
2. On localhost my install profile is running just fine. However, on a server where safe mode is enabled I'm getting the following error just after the install profile dependencies are enabled:
EDIT: Without the patch in #15 this error message does not appear. I'm using the latest dev.
This core patch is applied: http://drupal.org/node/1240256#comment-5335164
Comment #23
sutharsan commentedRe. 1
I meant the instructions in #19. Check these or debug the code. Not much I can help.
Re 2
Is this the same problem or another one? "localhost my install profile is running just fine" do you mean with #15 applied?
Safe mode is not supported. See http://drupal.org/requirements
The core patch is not likely to change anything for this issue since import of local po files does not involve any copy or write operation.
I can't help you further since I can not reproduce the situation. And since your localhost works correctly, I advice you to debug the drupal site on the server and let us know what solution you have found.
Comment #24
sutharsan commentedPatch re-rolled for latest dev release. Patch committed.
No response on my latest comment, I assume you fixed it. If the problem persists feel free to re-open the issue.
Comment #25.0
(not verified) commentedChange
to