Multilingual Menus
-
Good Day –
First of all, great plugin. Neat and clean. I am using english and french languages.
I have been able (through this forum) to figure out menu language switching, and it works perfect. The issue is that when using the french menu, it will default back to the english links, rather then appending /fr/ in the url string.
Has anyone else seen this problem or figured out a way to fix it?
Cheers,
-
How have you set up the menu switching?
I am using the method that jensnilsson posted here:
https://www.ads-software.com/support/topic/how-to-translate-menus?replies=7The switching works fine, but having the menu keep me in the selected language has me stumped, and it links back to the default (eng). I would need this to hold true for subnav’s which would be child based.
Any help would be much appreciated.
I added a small functionality in my twentythirteen template child which generates extra menus for each installed bogo language and switches them.
If you want to go that way, add this to functions.php in your template:
function my_bogo_menu_setup() { $installed_locales = get_available_languages(); $installed_locales[] = bogo_get_default_locale(); $installed_locales[] = 'en_US'; $installed_locales = array_unique( $installed_locales ); $installed_locales = array_filter( $installed_locales ); $bogo_languages = bogo_languages(); foreach ( $installed_locales as $locale ) { register_nav_menu( 'menu_'.$locale, $bogo_languages[$locale] ); } unregister_nav_menu('primary'); }
and then change your custom header.php this way (important is the line with wp_nav_menu call):
<div id="navbar" class="navbar"> <nav id="site-navigation" class="navigation main-navigation" role="navigation"> <h3 class="menu-toggle"><?php _e( 'Menu', 'twentythirteen' ); ?></h3> <a class="screen-reader-text skip-link" href="#content" title="<?php esc_attr_e( 'Skip to content', 'twentythirteen' ); ?>"><?php _e( 'Skip to content', 'twentythirteen' ); ?></a> <?php wp_nav_menu( array( 'theme_location' => 'menu_'.get_locale(), 'menu_class' => 'nav-menu' ) ); ?> <?php get_search_form(); ?> </nav><!-- #site-navigation --> </div><!-- #navbar -->
if you want to automatically add pages to the correct menu by the language, you can add something like this to your functions.php:
function _my_auto_add_pages_to_menu( $new_status, $old_status, $post ) { if ( 'publish' != $new_status || 'publish' == $old_status || 'page' != $post->post_type ) return; if ( ! empty( $post->post_parent ) ) return; $auto_add = get_option( 'nav_menu_options' ); if ( empty( $auto_add ) || ! is_array( $auto_add ) || ! isset( $auto_add['auto_add'] ) ) return; $auto_add = $auto_add['auto_add']; if ( empty( $auto_add ) || ! is_array( $auto_add ) ) return; $args = array( 'menu-item-object-id' => $post->ID, 'menu-item-object' => $post->post_type, 'menu-item-type' => 'post_type', 'menu-item-status' => 'publish', ); foreach ( $auto_add as $menu_id ) { if ( 'menu_'.get_post_meta($post->ID, '_locale', true) == $menu_id ) { $items = wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) ); if ( ! is_array( $items ) ) continue; foreach ( $items as $item ) { if ( $post->ID == $item->object_id ) continue 2; } wp_update_nav_menu_item( $menu_id, 0, $args ); } } } remove_action( 'transition_post_status', '_wp_auto_add_pages_to_menu', 10, 3 ); add_action( 'transition_post_status', '_my_auto_add_pages_to_menu', 9, 3 );
It was a little hack I needed to do quite quickly so I did not play with it very much. Therefore I’m not sure whether it will run in all conditions. But it worked for me ??
Hope it helps somebody.
It would be nice to find a way how to integrate this into the plugin itself.
one more – if you want to have menu dynamically generated from pages that are in the actual locale only:
function my_wp_nav_menu_args( $args = '' ) { $args['fallback_cb'] = 'my_nav_menu_fallback'; return $args; } add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' ); /** * Display or retrieve list of pages with optional home link - shows only pages * with current locale given by the Bogo plugin. * * Overrides wp_page_menu function * * @param array|string $args * @return string html menu */function my_nav_menu_fallback( $args = array() ) { $defaults = array('sort_column' => 'menu_order, post_title', 'menu_class' => 'menu', 'echo' => true, 'link_before' => '', 'link_after' => ''); $args = wp_parse_args( $args, $defaults ); $args = apply_filters( 'wp_page_menu_args', $args ); $menu = ''; $list_args = $args; // Show Home in the menu if ( ! empty($args['show_home']) ) { if ( true === $args['show_home'] || '1' === $args['show_home'] || 1 === $args['show_home'] ) $text = __('Home'); else $text = $args['show_home']; $class = ''; if ( is_front_page() && !is_paged() ) $class = 'class="current_page_item"'; $menu .= '<li ' . $class . '><a href="' . home_url( '/' ) . '" title="' . esc_attr($text) . '">' . $args['link_before'] . $text . $args['link_after'] . '</a></li>'; // If the front page is a page, add it to the exclude list if (get_option('show_on_front') == 'page') { if ( !empty( $list_args['exclude'] ) ) { $list_args['exclude'] .= ','; } else { $list_args['exclude'] = ''; } $list_args['exclude'] .= get_option('page_on_front'); } } $list_args['echo'] = false; $list_args['title_li'] = ''; // filter pages with current locale only $list_args['meta_key'] = '_locale'; $list_args['meta_value'] = get_locale(); $menu .= str_replace( array( "\r", "\n", "\t" ), '', wp_list_pages($list_args) ); if ( $menu ) $menu = '<ul>' . $menu . '</ul>'; $menu = '<div class="' . esc_attr($args['menu_class']) . '">' . $menu . "</div>\n"; $menu = apply_filters( 'wp_page_menu', $menu, $args ); if ( $args['echo'] ) echo $menu; else return $menu; }
Again, it is not thoroughly testet so you should be careful when using this. But it can at least give you an idea which way to go.
Feel free to use these source codes. I would be happy if it could become a part of the plugin.
The new Bogo has an inbuilt menu translation.
- The topic ‘Multilingual Menus’ is closed to new replies.