Score:2

How to localize your own contrib module

lc flag

Things have changed since I last localized a Drupal contrib module of mine. In D9/10, having a translations folder with .po files no longer seems to be the usual route. Yes, it can be done for local custom modules by specifying the location in the info.yml but I'm looking for a solution for regular, d.o hosted contrib modules. Right now I don't even need many languages, just one, the (Commerce) module in question applies to a single country, so the translation should obviously come directly for anybody installing the module.

I'm aware of this question but it's unanswered.

Score:2
lc flag

With some perseverance, I succeeded. The required steps are partly as described in the linked other question, have your .po files in a translations folder inside your module. But instead of adding the fixed path to the info.yml file, use the following hook in your .module:

function MODULE_locale_translation_projects_alter(&$projects) {
  $folder = \Drupal::service('extension.list.module')->getPath('MODULE');
  $projects['MODULE'] = [
    'info' => [
      'interface translation server pattern' => $folder . '/translations/%language.po',
    ],
  ];
}

You may use a different naming scheme for the files, I decided the language code was enough for me.

This way, it will be loaded from the actual folder, no matter whether it's in custom or not.

I sit in a Tesla and translated this thread with Ai:

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.