0

Question: How do I export/import translation of Custom Menu Items

I have a module that takes all the .po files for custom strings and then uses locale to batch import them. This works great.

I was wanting to translate my custom menu links, but when I search for them in the translate interface (or searching the .po from exporting my translations) I am unable to find any of the menu items.

I've included my admin/config/regional/content-language settings below so you can see that Custom Menu Link (and it's menu link title) is ticked.

I am able to translate the menu items only by going to admin/structure/menu/item/#/edit/translations, but this isn't (seemingly) exportable in either the .po files (via locale export), nor the config management system (via drush cex).

Anyone know how to do this? Thanks in advance.

admin/config/regional/content-language settings

1 Answer 1

0

So what I ended up doing was to create a module that reads from a json file, and adds the translation. (Import controller + drush command)

a) Load up the custom menu items by menu name:

$main_menu_items = \Drupal::entityTypeManager()->getStorage('menu_link_content')->loadByProperties(array('menu_name' => $menu_name));

b) Go through your menu items, foreach of them check if your menu link title matches one in your json/po file/config.

c) Check if you already have a translation on that menu item, then remove the old translation or skip.

if ($menu_item->hasTranslation('fr')) {

d) Finally add the translation that exists in your file.

  $menu_item_fr = $menu_item->addTranslation('fr');
  $menu_item_fr->title = $translation_json[$menu_name][$link_title];
  $menu_item_fr->description = '';
  $menu_item_fr->save();

This is essentially what my json looked like:

{"menu_name": {"english_link_title":"french_link_title"}}

Do hope this helps someone else, or someone can think up a better answer.

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.