WordPress.org

Ready to get started?Download WordPress

Forums

Bogo
[resolved] How to translate menus? (7 posts)

  1. HrvojeKC
    Member
    Posted 1 year ago #

    Hi,
    how do I translate the menu with this plugin?

    Testing this plugin and translating posts and pages works great, but I ca't figure out how to translate menus built using WP custom menus.

    http://wordpress.org/extend/plugins/bogo/

  2. Takayuki Miyoshi
    Member
    Plugin Author

    Posted 1 year ago #

    Unfortunately translating for menus isn't implemented yet. In my site I'm switching nav menu based on the current locale by using this additional code in my custom theme's functions.php:

    add_action( 'init', 'cf7com_register_additional_menus', 11 );
    
    function cf7com_register_additional_menus() {
    	register_nav_menu( 'primary_ja', _x( 'Primary (Japanese)', 'nav menu location', 'cf7com' ) );
    }
    
    add_filter( 'wp_nav_menu_args', 'cf7com_nav_menu_args' );
    
    function cf7com_nav_menu_args( $args ) {
    	$locale = get_locale();
    
    	if ( 'ja' == $locale )
    		$args['theme_location'] = 'primary_ja';
    
    	return $args;
    }

    Hope this helps.

  3. HrvojeKC
    Member
    Posted 1 year ago #

    Thanks for the tip!

    I just wondered if there is something I fail to notice.

  4. webbrewers
    Member
    Posted 1 year ago #

    With a little tweaking this worked well for me. Thanks Takayuki!

  5. bartslot
    Member
    Posted 1 year ago #

    I am quite new to php and would love a little help...
    How can I set up menu switching with 3 different languages (Dutch, English and French). I use pagelines framework that uses hooks to place code in the right area. Can you help me with the code for 3 languages and also if its possible what the hooks are in your code. Thanks a lot!

  6. jensnilsson
    Member
    Posted 1 year ago #

    Hello, this is how we solved switching menu based on what language is currently being displayed:

    I have 3 menus registerred:

    register_nav_menu( 'sv_SE_top-menu', 'Top Menu Sv' );
    register_nav_menu( 'de_DE_top-menu', 'Top Menu De' );
    register_nav_menu( 'en_US_top-menu',  'Top Menu En' );

    In header.php (or where you render you navigation):

    wp_nav_menu( array( 'theme_location' => apply_filters( 'language_prefix_menu', 'top-menu' ) ) );

    In functions.php:

    function add_language_prefix($menu_identifier) {
        global $post;
        $language_prefix = get_post_meta( $post->ID, '_locale', true );
        return $language_prefix . '_' . $menu_identifier;
    }
    add_filter('language_prefix_menu', 'add_language_prefix');
  7. webbrewers
    Member
    Posted 1 year ago #

    The above is probably cleaner but here's another way to slice it using 2 languages and 4 menus:
    functions.php

    function register_my_menus() {
      register_nav_menus(
        array(
          'primary-en' => __( 'Primary En' ),
          'secondary-en' => __( 'Secondary En' )
        )
      );
    }
    add_action( 'init', 'register_my_menus' )

    header.php

    <?php $locale = get_locale();
    
    	if ('es_ES' == $locale  ) { ?>
                        <?php $theme->hook('menu_primary'); ?>
    <?php } else { ?>
    <?php wp_nav_menu( array( 'theme_location' => 'primary-en', 'container_class' => 'menu-primary-container' ) ); ?>
    <?php } ?>

    Repeat if clause for additional menus.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic