Hi!

How to use the D8 .po translations in my module after module install?

# mymodule.info.yml
name: mymodule
core: 8.x
version: 1
type: module
package: mymodule
description: My module.
interface translation project: mymodule
interface translation server pattern: modules/mymodule/translations/%project-%language.p

The lang file is:

mymodule/translations/mymodule-hu.po

The drush did not use it!

drush locale-check
drush locale-update
drush cr

When does the D8 load the translations (.po files) during installation?

(In D7 it worked.)

Comments

gaborm’s picture

is it the solution?

I tried using the hook_locale_translation_projects_alter() to add my module to the project array and it worked!
Here the doc link : https://api.drupal.org/api/drupal/core!modules!locale!locale.api.php/function/hook_locale_translation_projects_alter/8.2.x

How to use it?

// mymodule.module
function mymodule_locale_translation_projects_alter(&$projects) {

  // The translations
  $projects['mymodule'] = array(
    'info' => array(
      'interface translation server pattern' => 'modules/mymodule/translations/%project-%language.po',
    ),
  );
}
gaborm’s picture

it's not working.

I removed the module and reinstall it. But the translation file (.po) did not load.

The drush command did not work too!

I need to import it (.po file). (Only import function works.)

what is missing?

gaborm’s picture

it seems it is working.

https://drupal.stackexchange.com/questions/152160/how-can-i-deploy-user-...

for example:

    /**
     * Add a user interface translation to the translation storage.
     *
     * @param $source_string - English string.
     * @param $langcode - Language to translate to.
     * @param $translated_string - Translated string.
     */
    function MODULE_add_translation($source_string, $langcode, $translated_string) {
      // Find existing source string.
      $storage = \Drupal::service('locale.storage');
      $string = $storage->findString(['source' => $source_string]);
      if (is_null($string)) {
        $string = new SourceString();
        $string->setString($source_string);
        $string->setStorage($storage);
        $string->save();
      }
      // Create translation. If one already exists, it will be replaced.
      $translation = $storage->createTranslation([
        'lid' => $string->lid,
        'language' => $langcode,
        'translation' => $translated_string,
      ]);
      $translation->save();
    }
     
    /**
     * Import all translations for this module.
     */
    function MODULE_import_translations($path, $langcode) {
     
      // Parse the file.
      $translations = Yaml::parse(file_get_contents($path));
     
      // Save each translation.
      foreach ($translations as $english => $translation) {
        MODULE_add_translation($english, $langcode, $translation);
      }
    }

It can to call in:

hook_install()

hook_updateX()

Jaypan’s picture

That's a good follow up, thank you.

Schoenef’s picture

Just to have it complete:

if translations are defined via 'interface translation server pattern' in the mymodule.info.yml, they will be picked up by `/admin/reports/translations` and can then be imported there kind of automatically. One usually has to do this anyway after module updates.

Best wishes
Andreas

jigarius’s picture

Too late to the party, but there seems to be a missing "o" in the ".po" in the "interface translation server pattern" value.