2

I have a custom module whose interface strings have all been written in English. This module is not (and will not be) hosted on drupal.org, so I'd like to pack the translations for those texts directly with the module, and for those translations to be automatically imported by Drupal when the module is installed (provided the corresponding language is activated in Drupal, of course).

I've translated those texts in French and exported all those translations in a my_module-fr.po file. I've put this file in the "translations" folder inside my module folder.

As suggested in this answer, I used the interface translation properties in the my_module.info.yml, as follows:

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

But when I install my module from scratch, those translations are not automatically imported.

In admin/reports/translations, it says "Missing translations for one project - my_module (1). File not found at modules/custom/my_module/translations/my_module-fr.po".

I've tried several variations for the interface translation server pattern value in the info.yml file, among them:

  • path relative to the module folder (translations/my_module-%language.po) instead of the Drupal folder
  • full path to the drupal install folder (since I'm on localhost and Drupal is in a subfolder) (_tests/drupal8/www/modules/custom/my_module/translations/my_module-%language.po)

But I always get the same "file not found" message in the admin report page, and nothing is imported.

What am I missing?


Side question: in the YML declaration above, is it allowed to use a path pattern relative to the module folder, as listed above? This would make a lot more sense to me, as I cannot predict whether the people will install my module in the "modules/contrib" folder, or in another subfolder, or directly in the "modules" folder (and if they install it from the Drupal UI, they may not even know it themselves).

1 Answer 1

1

Per the documentation: https://api.drupal.org/api/drupal/core%21modules%21locale%21locale.api.php/group/interface_translation_properties/8.4.x

Streamwrappers can be used in the server pattern definition. The interface translations directory (Configuration > Media > File system) can be addressed using the "translations://" streamwrapper. But also other streamwrappers can be used.

There are several examples given for valid server pattern definitions using different stream wrappers.

EDIT

I see what you mean about not wanting to use the translations:// stream wrapper.

Have you tried using the pattern definition exactly as given in the answer you cited?

modules/custom/example_module/translations/%language.po

If you do that, you should also take the name of your module out and just name the file fr.po.

Then make sure that you update translations with Drush:

drush locale-check && drush locale-update && drush cr

If that doesn't work, hopefully someone else can help, because I'm stumped.

3
  • Yes I've read this page. The first example given is the type of pattern I'm trying to use, so I still don't understand why it says "file not found". Using the translations:// stream wrapper is not really an option because it requires the user to first copy the translation file in the public "translations" folder (e.g. sites/default/files/translations). I want the translations to be imported directly from the my_module/translations folder. The user shouldn't have to copy some files in another folder for the translations to be imported.
    – s427
    Commented Nov 30, 2018 at 17:52
  • I added to my answer. If that doesn't help, I don't have the answer. Good luck! Commented Nov 30, 2018 at 18:39
  • Thanks. Yes this path (without the module name) is the first I tried actually. I haven't tried Drush because I can't really tell my users to use Drush when they want to install my module (it's an incredibly technical requirement). Surely there's a way to make it work without Drush... (Other CMS manage it.)
    – s427
    Commented Dec 1, 2018 at 10:14

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.