No ways to configure module-relative path for included module translation .po files

Created on 24 October 2024, 8 months ago

Problem/Motivation

Some modules want to include the built-in translations as .po files, especially those that aren't published on Drupal.org and don't have a project in localize.drupal.org.

Usually, they place them here [module_root]/translations/[langcode].po.

And we have special configuration keys in the my_module.info.yml file to automatically import those translations during the module install:

'interface translation project': my_module
'interface translation server pattern': translations/%language.po

But suddenly, they don't work as expected because the configured path `translations/%language.po` is considered the path from the Drupal root, not from the module root!

Steps to reproduce

1. Create a custom module my_module and put the translation info into the my_module.info.yml file:

'interface translation project': my_module
'interface translation server pattern': translations/%language.po

2. Place some translation files there, for example - translations/de.po with some translation strings.

3. Install the module.

4. See that no translation strings were imported during the installation.

Proposed resolution

To not break the previous behavior, I'd propose to introduce the module-relative path placeholder module:// and use it like this:

'interface translation project': my_module
'interface translation server pattern': module://translations/%language.po

And extend the current Drupal Core logic to replace it with the actual module path.

For now, we can use a workaround with a custom hook like this:

function my_module_locale_translation_projects_alter(&$projects) {
  $moduleName = 'my_module';
  $modulePath = \Drupal::service('module_handler')->getModule($moduleName)->getPath();
  $projects[$moduleName]['info']['interface translation server pattern'] =
  str_replace(
    'module://',
    $modulePath . '/',
    $projects[$moduleName]['info']['interface translation server pattern']
  );

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

πŸ› Bug report
Status

Active

Version

11.0 πŸ”₯

Component

locale.module

Created by

πŸ‡¦πŸ‡²Armenia murz Yerevan, Armenia

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024