Wordpress:带有语言代码的 qTranslate X 语言切换器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29649898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:04:51  来源:igfitidea点击:

Wordpress : qTranslate X language switcher with language code

wordpressqtranslate-x

提问by enguerranws

I'm trying to make a basic language switcher with qTranslate X, something like :

我正在尝试使用 qTranslate X 制作一个基本的语言切换器,例如:

FR | EN

There's a function to achieve that : qtranxf_generateLanguageSelectCode('text');but it can only accept 'text', 'image' or 'both', so it doesn't fit to my needs : 'text' is the full name of the language.

有一个功能可以实现:qtranxf_generateLanguageSelectCode('text');但它只能接受“文本”、“图像”或“两者”,因此它不符合我的需要:“文本”是语言的全名。

How can I just show the language code ? Any idea to make a filter to do that ?

我怎样才能只显示语言代码?任何想法制作过滤器来做到这一点?

回答by Ash Patel

Try to add following script below translate code.

尝试在翻译代码下方添加以下脚本。

echo qtranxf_generateLanguageSelectCode('text');
<script>jQuery(document).ready(function(){ jQuery('.lang-en a span').html('EN'); jQuery('.lang-fr a span').html('FR'); })</script>

Serverside Solution:

服务器端解决方案:

Please find below Code which modify language name to language code without change in plugin code and you can do it by word press filter.

请在下面找到将语言名称修改为语言代码而不更改插件代码的代码,您可以通过 wordpress 过滤器来完成。

Paste below code into function.php file.

将以下代码粘贴到 function.php 文件中。

add_filter('template_include','start_buffer_EN',1);
function start_buffer_EN($template) {
  ob_start('end_buffer_EN');  
  return $template;
}
function end_buffer_EN($buffer) {
  return str_replace('<span>English</span>','<span>EN</span>',$buffer);  
}

add_filter('template_include','start_buffer_FR',1);
function start_buffer_FR($template) {
  ob_start('end_buffer_FR');
  return $template;
}
function end_buffer_FR($buffer) {  
  return str_replace('<span>Fran?ais</span>','<span>FR</span>',$buffer);
}

You can change language name from wp-admin by edit language name directly..

您可以通过直接编辑语言名称从 wp-admin 更改语言名称..

Q-translate-x-change-image-name-from-admin

Q-translate-x-change-image-name-from-admin

回答by lassemt

Inspecting the plugin I found that generateLanguageSelectCode have more types than documented. So to use language codes you can simply just use the type 'short', like this:

检查插件我发现 generateLanguageSelectCode 的类型比记录的多。因此,要使用语言代码,您可以简单地使用“short”类型,如下所示:

qtranxf_generateLanguageSelectCode('short');

This might be a feature added since last answer.

这可能是自上次回答以来添加的功能。

Here is a overview of all the switcher types: 'text', 'image', 'both', 'short', 'css_only', 'custom', and 'dropdown'. I havn't looked into how the different types works, but you'll find them in qtranslate_widget.php in the plugin folder.

以下是所有切换器类型的概述:“text”、“image”、“both”、“short”、“css_only”、“custom”和“dropdown”。我还没有研究不同类型的工作原理,但您可以在插件文件夹的 qtranslate_widget.php 中找到它们。

回答by darkem

You could use widget for that

您可以为此使用小部件

<?php the_widget('qTranslateXWidget', array('type' => 'custom', 'format' => '%c') );?>

(%c - Language 2-Letter Code)

(%c - 语言 2 字母代码)

It should be noted that if you would like to use dropdown type and 2-Letter Code - this won't work because format argument works only with 'custom' type. In this case I would go with Yehuda Tiram answer (especially if you have many languages and you don't know which languages your client will want to use).

应该注意的是,如果您想使用下拉类型和 2 字母代码 - 这将不起作用,因为格式参数仅适用于“自定义”类型。在这种情况下,我会选择 Yehuda Tiram 的答案(特别是如果您有多种语言并且您不知道您的客户想要使用哪种语言)。

More documentation here

更多文档在这里

回答by enguerranws

A friend helped me with that and it's based on Ash Patel answer but in a cleaner way (IMHO) :

一位朋友帮助我解决了这个问题,它基于 Ash Patel 的回答,但以更简洁的方式(恕我直言):

function my_qtranxf_generateLanguageSelectCode($style='', $id='') {
    ob_start();  
    qtranxf_generateLanguageSelectCode($style, $id);
    $o = ob_get_contents();
    ob_end_clean();
    return str_replace(array('English', 'Fran?ais'),array('EN', 'FR'), $o); 
}

回答by Yehuda Tiram

Why don't you just change the language name as per your needs? It's possible in the language edit and does not affect anything.

为什么不根据需要更改语言名称?在语言编辑中是可能的,并且不会影响任何事情。

回答by Muhammad Awais Iqbal

I've done it using the following query and it is working fine for me.

我已经使用以下查询完成了它,它对我来说工作正常。

<?php if (qtranxf_getLanguage() == 'ar') { ?>
<script>
    jQuery(document).ready(function () {
        var current_URL = jQuery(location).attr('href');
        url = current_URL.replace('/ar/', '/en/')
        jQuery('.languages-selection ul li a').attr('href', url)
    });
</script>
<?php } elseif (qtranxf_getLanguage() == 'en') { ?>
<script>
    jQuery(document).ready(function() {
        var current_URL = jQuery(location).attr('href');
        url = current_URL.replace('/en/', '/ar/')
        jQuery('.languages-selection ul li a').attr('href', url)
    });
</script>
<?php } ?>

回答by Alex

https://qtranslatexteam.wordpress.com/faq/

https://qtranslatexteam.wordpress.com/faq/

For example, to show flag only in the top language menu item, enter #qtransLangSw?title=none, if in addition to this current language is not needed to be shown, enter #qtransLangSw?title=none&current=hidden, and so on.

例如,只在顶部语言菜单项中显示标志,输入#qtransLangSw?title=none,如果除了当前语言不需要显示,输入#qtransLangSw?title=none¤t=hidden,依此类推。