9

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.

  • What did you end up doing? – Boy Baukema Mar 3 '16 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 '16 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. – Philipp Michael May 30 '16 at 13:49
  • 1
    I rolled us a module for that: drupal.org/project/customtranslations Have fun. – geek-merlin Apr 1 '17 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 '17 at 16:09
4

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, but it doesn't have a Drupal 8 version yet, but you could start there and port that: https://www.drupal.org/project/drush_language.

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).

  • 1
    drupal.org/project/drush_language now has a D8 (dev) version. – Hugo Apr 2 '17 at 6:55
  • You can check the form which import translations from /admin/config/regional/translate/import on core/modules/locale/src/Form/ImportForm.php. – rpayanm Mar 21 '18 at 19:45
3

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).

  • 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 '15 at 16:01
2

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');
}
0

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.

  • please use comments for this, it's not really the answer... – mixerowsky Aug 2 '17 at 15:23
  • 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 '17 at 20:38
0

As aexl mentioned on the comments on the question, you can use the Custom Translations module. With that you should just need to include drush locale-update in your deployment script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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