2

I'm new to using Drupal and am building a custom module to learn how it all works.

I'm building my module in modules/custom/my_module. I have a Dutch locale file in modules/custom/my_module/translations/my_module-0.1.nl.po.

If I specify it like that in my .info.yml file, it works great:

name: My Module
description: Just testing
version: 0.1
package: Custom
type: module
core_version_requirement: ^8.8 || ^9
'interface translation project': my_module
'interface translation server pattern: modules/custom/my_module/translations/%project-%version.%language.po

But, now my .info.yml file would force users to install this module in modules/custom/my_module. I don't want that. I don't care where they put it. They can put this module in modules/contrib/my_module or sites/all/modules/my_module for all I care. Maybe someone will include it as a packaged dependency and it will end up in modules/custom/some_module/modules/my_module. It shouldn't matter and I don't want to hard-code the module path in my .info.yml.

What I want is to have my .info.yml file specify the server pattern using a module-relative path (relative to where my module's .info.yml file lives) like this:

'interface translation server pattern': translations/%project-%version.%language.po
# or perhaps as:                        ./translations/%project-%version.%language.po

But Drupal doesn't like this and checking for updates to translations will indicate the file could not be found.

I'm aware of the translations:// and public:// stream wrappers, but as far as I can tell this either requires the translation file to be hosted elsewhere, copied to the sites/default/files folder (which I could probably do in my_module_install() but is unhandy during development when I'm constantly adding strings) or (again) hardcoding the path to the module.

For now I've implemented the hook_locale_translation_projects_alter() hook in my my_module.module file to set an absolute path like this:

function my_module_locale_translation_projects_alter(&$projects) {
  $projects['my_module']['info']['interface translation server pattern'] =
    __DIR__ . '/translations/%project-%version.%language.po';
}

This works, but it feels like a nasty workaround and not how Drupal is intended to work.

What is the usual method to get module-relative paths to work for translations?


Edit: As suggested by @leymannx, instead of using __DIR__ in my hook, I can use:

function my_module_locale_translation_projects_alter(&$projects) {
  $projects['my_module']['info']['interface translation server pattern'] =
    drupal_get_path('module', 'my_module') . '/translations/%project-%version.%language.po';
}

This feels slightly better, but I still feel this should be possible without implementing a hook for this.

6
  • Try drupal_get_path('module', 'MYMODULE') . '/translations/%language.po'.
    – leymannx
    Sep 7, 2021 at 9:47
  • @leymannx can I do that in my yaml file? Or would I still need to implement the hook to use that function? Sep 7, 2021 at 9:48
  • No, from the hook. I don't think you can have a module relative path in the info file. Only web-root relative.
    – leymannx
    Sep 7, 2021 at 9:49
  • @leymannx that works and feels better than using __DIR__ but surely there's a way to do this from the yaml file without implementing the hook? Sep 7, 2021 at 9:51
  • I don't think so. What's wrong with the hook?
    – leymannx
    Sep 7, 2021 at 9:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.