Score:2

How to use a module-relative path to translations in .info.yml

id flag

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.

leymannx avatar
ne flag
Try `drupal_get_path('module', 'MYMODULE') . '/translations/%language.po'`.
rickdenhaan avatar
id flag
@leymannx can I do that in my yaml file? Or would I still need to implement the hook to use that function?
leymannx avatar
ne flag
No, from the hook. I don't think you can have a module relative path in the info file. Only web-root relative.
rickdenhaan avatar
id flag
@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?
leymannx avatar
ne flag
I don't think so. What's wrong with the hook?
rickdenhaan avatar
id flag
The hook should not be necessary for something this trivial. It is also not executed when the module is not installed, so Drupal won't read the translated module name/description for the module list on the Extend page in the admin UI where I can select it and install it. (I actually haven't checked, but I *assume* it will read the translated name/description from the locale file specified in .info.yml for that page...)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.