首页 文章

如何在wordpress上使用qtranslate翻译导航菜单的LINKS?

提问于
浏览
4

我有一个双语(英语/阿拉伯语)wordpress网站 . 我能够成功翻译导航菜单项 . 但是,阿拉伯语网站上的菜单链接链接到英语的默认语言 .

我怎么能告诉wordpress我需要在阿拉伯网站上更改菜单链接(我需要阿拉伯网站上的链接包含/ ar,例如:www.talalonline.com/ar而不是www.talalonline.com)

谢谢

5 回答

  • 1

    @maha,我搜索了很多关于这个并找到了解决方案here,但答案有点模糊......

    由于您不想弄乱WP核心文件,所有更改都在主题中 . 您的主题位于 wp-content/themes/your-theme-name/

    找到你的主题 function.php 并在文件末尾添加上面的代码,在php结束标记( ?> )之前:

    class CustomLinkModifierWalker extends Walker_Nav_Menu {
        function __( $text ) {
            if ( preg_match_all('~(.*?)\|(\w{2,})\|~', $text, $matches) ) {
                $text = '';
                foreach ($matches[1] as $i => $match) {
                    $text .= "[:{$matches[2][$i]}]$match";
                }
                $text = __( $text );
            }
            return $text;
        }
        function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
            global $wp_query;
            $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
    
            $class_names = $value = '';
    
            $classes = empty( $item->classes ) ? array() : (array) $item->classes;
            $classes[] = 'menu-item-' . $item->ID;
    
            $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
            $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
    
            $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
            $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
    
            $output .= $indent . '<li' . $id . $value . $class_names .'>';
    
            $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
            $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
            $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
            $attributes .= ! empty( $item->url )    ? ' href="' . esc_attr( $this->__( $item->url )  ) .'"' : '';
    
            $item_output = $args->before;
            $item_output .= '<a'. $attributes .'>';
            $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
            $item_output .= '</a>';
            $item_output .= $args->after;
    
            $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
        }
    }
    

    然后,你必须找到's your menu view inside your theme. The theme I' m在 header.php 中使用工具的位置 . 也许你的使用另一个文件名,比如 header-fancy-theme.php .

    我的 Headers 视图代码是这样的:

    <?php
    $nav_sec_menu_params = array(
        'depth' => 0,
        'theme_location' => 'sec-menu',
        'container_class' => 'menu-topmenu-container',
        'menu_class' => 'menus menu-topmenu',
        'fallback_cb' => 'block_sec_menu'
    );
    wp_nav_menu($nav_sec_menu_params);
    ?>
    

    您所要做的就是在param数组中添加Walker实现:

    <?php
    $nav_sec_menu_params = array(
        'walker' => new CustomLinkModifierWalker(),
        'depth' => 0,
        'theme_location' => 'sec-menu',
        'container_class' => 'menu-topmenu-container',
        'menu_class' => 'menus menu-topmenu',
        'fallback_cb' => 'block_sec_menu'
    );
    wp_nav_menu($nav_sec_menu_params);
    ?>
    

    然后,在您的菜单中,您将在语言URL之后使用 |lang| ,如下所示:

    enter image description here

    我知道这不是您使用自动语言链接的确切用途,但这可以解决您的问题 .

  • 10
    //tested and worked for me.
    //use native wordpress filter wp_setup_nav_menu_item
    
     function qtrans_menuitem( $menu_item ) {
    
    // convert local URLs in custom menu items
       if ($menu_item->type == 'custom' && stripos($menu_item->url, get_site_url()) !== false){
        $menu_item->url = qtrans_convertURL($menu_item->url);
    }     
    return $menu_item;
     }
    
     add_filter('wp_setup_nav_menu_item', 'qtrans_menuitem', 0);
    
  • 4

    qtranslate的fork开箱即用:

    https://wordpress.org/plugins/qtranslate-x/

    在“导航标签”字段的菜单项中,只提供两种语言的字符串:

    [:en]English Text[:de]Deutsch Text
    

    用qTranslate-X 2.7.8,Wordpress 4.1和Twenty Fifteen 1.0主题测试

  • 0

    WP中有四个函数可以使用默认或插件选项翻译菜单文本 .

    四个函数中的每一个都需要至少一个参数,即要翻译的文本 . 功能是:

    • __() - (两个下划线)大部分时间都将使用的基本功能 . 它以正确的语言返回文本 .

    • e() - 与_()相同,除了它回显文本而不是返回文本 .

    • _n() - 当文本有可能是复数时使用,例如,如果要显示已做出多少注释,则可能需要输出“X注释”或“X注释”,具体取决于多少你有评论 .

    • _x() - 当单词的翻译取决于上下文时有用 . “发布”可能意味着“帖子(名词)”或“发布(动词)”取决于上下文 . 翻译者在翻译时要知道你的意思是准确的,这一点很重要 . _x()主要用于使用单个单词的地方 .

    echo __( 'Menu Text' );
    
  • 0

    我也在使用qTranslate和双语阿拉伯语/英语网站 .

    您可以将 .htaccess 视为以下内容:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    #RewriteRule ^ar[\/]?$ index.php?lang=ar
    #RewriteRule ^en[\/]?$ index.php?lang=en 
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . index.php [L]
    </IfModule>
    

    并从qTranslate插件设置页面的 Advanced settings 部分选择"Use Pre-Path Mode (Default, puts /en/ in front of URL)"选项 .

相关问题