I have a headless Drupal 9 solution and I'm currently working on i18n of the whole site.
I've managed to do everything except menus. The content translation core module is installed and setup correctly. I have all the menu items translated.
This a snippet of the code:
$parameters = new MenuTreeParameters();
$parameters->onlyEnabledLinks();
// Get drupal menu tree.
$menu_tree = \Drupal::menuTree();
// Load the menu tree.
$main_menu_tree = $menu_tree->load('main', $parameters);
...
$manipulators = [
['callable' => 'menu.default_tree_manipulators:checkNodeAccess'],
['callable' => 'menu.default_tree_manipulators:checkAccess'],
['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'],
];
$tree = $menu_tree->transform($main_menu_tree, $manipulators);
...
foreach ($tree as $key => $menu_tree_element) {
$menu_object = $this->generateSingleMenuObject($menu_tree_element, $max_depth);
array_push($menu_array, $menu_object);
}
The 'generateSingleMenuObject' function is where I'd expect to somehow get the objects in a certain language. Here's how the function starts
// Get link from menu tree element.
$menu_tree_element_link = $menu_tree_element->link;
// Get url object from link.
$menu_url_object = $menu_tree_element_link->getUrlObject();
// Create dummy object for storing data.
$menu_object = new stdClass();
// Get id and label.
$menu_object->id = $menu_tree_element_link->getDerivativeId();
$menu_object->label = $menu_tree_element_link->getTitle();
// Get href.
$menu_object->href = $menu_url_object->toString();
I've tried some of the standard options that worked on other entities but without luck.
This is basically the same question as this one.
The only thing I need is the translated menu label.
What am I missing? This really sounds like it should be a relatively simple ask. Thank you!