WordPress clean internationalization for menus?

What are nav menu items?

When you’re translating post, pages & custom post types, then i guess you’ll be also translating the titles. In general nav menu items are only posts, pages, etc. and therefore (should) come with the full content & data the post provides (if not, then you can still call the post meta).

The filter callback function

  • Pro: there’s a filter for the output of the nav menu items.
  • Contra: It can cause problems, like the nav menu not appearing on the admin UI screen, so pay attention to test this properly.

The “check nav menu items” callback function:

    function wpse18880_check_nav_menu_items( $items, $menu, $args )
    {
        if ( is_admin() )
            return;

        echo '<pre>';
            # Show what we got:
            echo '<h3>THE MENU</h3>';
            print_r( $menu );
            echo '<hr /><h3>THE ITEMS</h3>';
            print_r( $items );
            echo '<hr /><h3>THE ARGUMENTS</h3>';
            print_r( $args );
        echo '</pre>';

        return $items;
    }
    add_filter( 'wp_get_nav_menu_items', 'wpse18880_check_nav_menu_items', 10, 3 );

Modifying the menu items:

So, when looking at the output of $item, you’ll see that every item has it’s post-data (“or whatever it is”-data) attached. So calling the additional post data (via get_post_meta()) like in the follwing example should give you access to your language specific content:

// This example is for 'post' post_type menu items:
foreach ( $items as $item )
{
    $post_ID = $item->ID;
    // now let's overwrite the original post_title
    $item->post_title = get_post_meta( $post_ID, 'i18n_title', true );
}

NOTE: I only tested the check nav menu items function, not overwriting the single items data. Please update this answer with the final code if you got it working. Thank you.

tech