14

Strings that I translate at "User interface translation" /admin/config/regional/translate are not exportable by "Configuration management" /admin/config/development/configuration for me. Am I missing something?

I would like to automate this process with hook_update_N or drush cim -y.

What is a proper way to automatically deploy "User interface translation" in Drupal 8?

  1. Should I do it in hook_update_N()?
  2. Should I create some custom mytranslations.po file in the language folder?

I see there is locale_translate_batch_import_files() which might be used.

5
  • What did you end up doing? Mar 3, 2016 at 8:24
  • @RelaXNow, I have not implement a solution yet. AFAICS import/export of user interface translation is not automated yet. I planned to go with a hook_update_N. Have a look at @Berdir's suggestion.
    – milkovsky
    Mar 7, 2016 at 8:28
  • Do you know about the new Drupal Console and its command translation:sync? Unfortunately I can't find any detailed information about this command but maybe it does what you are looking for. May 30, 2016 at 13:49
  • 1
    I rolled us a module for that: drupal.org/project/customtranslations Have fun. Apr 1, 2017 at 22:32
  • In our case we did a test with Potx (github.com/kgaut/drupal-potx) that is running only with Drush. To achieve this "easily" we did a .sh file who is executing the drush command to put translation file in modules/custom/translations for each module and each language. Then those file are added to the gitlab repository of the project and can be easily import in production during a deploy...
    – Promo
    Dec 18, 2017 at 16:09

6 Answers 6

7

You have to do this yourself. @rpayanm answered where you can do it manually, you will need to look at the code there, and for example implement drush commands that allow you to export and import it again. Automating that is then relatively easy, export, sync it to the other server, and import again.

Looks like a project that helps with that exists: Drush Language Commands.

You can put default translations in a module ,but those are only imported when you install that module. hook_update_N() is designed to only run once, so you would need to write a new function every time you want to do it (and you still need to export it first).

3
10

Helper function to add custom translations:

/**
 * Helper to manually add a single translation string.
 *
 * @param string $source_string
 *   Source string.
 * @param string $langcode
 *   The langcode.
 * @param string $translated_string
 *   Translated string.
 */
function MYMODULE_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();
}

Usage in an update hook:

/**
 * Add translations.
 */
function MYMODULE_update_8002() {
  MYMODULE_add_translation('Adjust', 'de', 'Anpassen');
}
5

You can export the translated strings used by your site A (admin/config/regional/translate/export) and then import them on the other site (admin/config/regional/translate/import).

1
  • 3
    Thx. I know this. But it is not an automatic deployment. I would like to automate this process with hook_update_N or drush cim -y. I will add this to the description.
    – milkovsky
    Mar 19, 2015 at 16:01
3

For our project, we wrote a custom module with .po files in a subfolder. These are loaded by the Drupal 8 localization system using a few lines of code in the .info.yml.

'interface translation project': CUSTOM_MODULE
'interface translation server pattern': modules/custom/CUSTOM_MODULE/%project-%version.%language.po

See https://api.drupal.org/api/drupal/core%21modules%21locale%21locale.api.php/group/interface_translation_properties/8.2.x

2

It looks like it should also be possible to do this by writing a script to run commands from Drush locale sync, although I haven't tried that module myself.

2
  • 2
    please use comments for this, it's not really the answer...
    – mixerowsky
    Aug 2, 2017 at 15:23
  • 1
    I wasn't sure if it was right or not, so I thought I should post it as an answer and let other people vote it up or down. Please vote down if you think its wrong.
    – bdsl
    Aug 2, 2017 at 20:38
1

A more simple option to deploy a few string translations would be to use the 'String overrides' feature available in Drupal 8.x and 9.x.

In short, it's possible to include string translations in your settings.php file (or settings.local.php).

This is the change record where it was introduced: https://www.drupal.org/node/2109883

And this is how it works:

/*
* String overrides:
*
* To override specific strings on your site with or without enabling the Locale
* module, add an entry to this list. This functionality allows you to change
* a small number of your site's default English language interface strings.
*
* Remove the leading hash signs to enable.
*
* The "en" part of the variable name, is dynamic and can be any langcode of
* any enabled language. (eg locale_custom_strings_de for german).
*/
 $settings['locale_custom_strings_en'][''] = array(
   'forum'      => 'Discussion board',
   '@count min' => '@count minutes',
 );

Your Answer

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

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