dragunoff

wpquestions.com, ID = 2901

Aug 29th, 2011
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.23 KB | None | 0 0
  1. <?php
  2. /**
  3.  * the below code goes into functions.php of the current theme
  4.  * The first function registers WP nav menus for each locale on your site
  5.  * It's possible to get the locales with the function get_available_languages()
  6.  * or pass them manually as an array
  7.  */
  8. add_action( 'init', 'custom_register_nav_menus' );
  9.  
  10. function custom_register_nav_menus() {
  11.  
  12.     // get all languages in theme's languages folder (.mo files)
  13.     // $available_locales = get_available_languages( STYLESHEETPATH . '/languages' );
  14.     // or manually set available locales
  15.     $available_locales = array ( 'en', 'zh' ) ;
  16.    
  17.     // register a nav menu for each of the locales
  18.     foreach ( $available_locales as $locale ) {
  19.         // the theme locations would be called "primary-en" and "primary-zh"
  20.         register_nav_menu( "primary-{$locale}", "Main Menu ({$locale})" );
  21.     }
  22.  
  23. }
  24.  
  25.  
  26. /**
  27.  * The below functions changes the WP's active locale based on the request uri
  28.  * You must have pretty permalink enabled for this to work and the pages for
  29.  * each language need to be under a master page with the slug of 'en' or 'zh'
  30.  */
  31. add_filter( 'locale', 'swpl_change_locale' );
  32.  
  33. function swpl_change_locale( $locale ) {
  34.  
  35.     global $swpl_customized_locale;
  36.  
  37.     // If we've already done this, return the value we previously found
  38.     if ( ! empty( $swpl_customized_locale ) )
  39.         return $swpl_customized_locale;
  40.  
  41.     // get all languages in theme's languages folder (.mo files)
  42.     // $available_locales = get_available_languages( STYLESHEETPATH . '/languages' );
  43.     // or manually set available locales
  44.     $available_locales = array ( 'en', 'zh' ) ;
  45.    
  46.     // loop 'em
  47.     foreach ( $available_locales as $lang ) {
  48.  
  49.         // if using get_available_languages(), the returned locale would be in the format 'en_US'; use this to convert it to 2-letter locale
  50.         // $short_locale = strtolower ( substr( $lang, 0, 2 ) );
  51.        
  52.         // if request uri starts with one of the locales
  53.         if ( strpos( $_SERVER[ 'REQUEST_URI' ], '/' . $short_locale ) === 0) {
  54.        
  55.             // assign locale
  56.             $locale = $swpl_customized_locale = $lang;
  57.             break;
  58.        
  59.         }
  60.        
  61.     }
  62.  
  63.     return $locale;
  64.    
  65. }
  66.  
  67.  
  68.  
  69. /**
  70.  * Then call your menu like this in your template
  71.  */
  72. wp_nav_menu( array(
  73.     'theme_location' => 'primary-' . get_locale(),
  74.     )
  75. );
Advertisement
Add Comment
Please, Sign In to add comment