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

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.

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

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

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.