I have been trying to translate menu items programmatically but upon saving it just adds the english field value to all my translations. Below is the code I am trying to execute:

 if ($entity->hasTranslation($language->getId())) {
      //For update
    try {
      $entity->addTranslation($language->getId(), ['title' => 'abcd'])->save();
    } catch (EntityStorageException $e) {
      \Drupal::messenger()->addError(t('Failed to add translation for ' . $language->getName()));

I am debugging and I think the issue lies in the line :

$entity->addTranslation($language->getId(), ['title' => 'abcd'])->save();

Any help would be appreciated. Thankyou.

  • 1
    Just a sanity check - is the "Menu link title" field configured to be translatable at /admin/config/regional/content-language?
    – Clive
    Jun 7 at 19:05
  • How are you implementing localization? Menu items should have a 'translate' link (or something similar) in the menu UI (structure -> menus).
    – blur
    Jun 10 at 4:43

Your Answer

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

Browse other questions tagged or ask your own question.