如何通过单击链接(切换语言)来更改Wordpress中的WPLANG值? [英] How to change the value WPLANG in Wordpress by clicking a link (switch language)?

查看:141
本文介绍了如何通过单击链接(切换语言)来更改Wordpress中的WPLANG值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我成功使用Gettext(称为es.po和es.mo)制作了用于本地化的文件.

I successfully made files for localization using Gettext (called es.po and es.mo).

据我所知,可以通过在 wp-config.php 中定义WPLANG的值来更改语言(在这种情况下,请定义('WPLANG','es'); )

So far as I know I can change the language by defining the value of WPLANG in wp-config.php (in this case define ('WPLANG', 'es');)

我希望用户通过单击链接来更改他/她的首选语言.

I would like the user to change his/her preferred language by clicking a link.

做到这一点的最佳方法是什么?

What's the best way of doing this?

推荐答案

我通过在

I accomplished this by building upon the jLanguage Plugin found at http://www.treutech.com/files/wordpress/jLanguage.zip. *The plugin is no longer available from the author's site, so I'm hosting my updated version. This allows you to format your blog posts and pages using the syntax [english][/english]. It passes a querystring to the code to know which language to use. I started by modifiying the code so that it would use the standard two-letter language codes. I also did away with the flags that the code used to represent the various languages. However, after all this, the plugin still only translates the pages or posts. I wanted the rest of the site to be translated based on the user's choice.

WordPress允许国际化;但是,一旦您选择了一种语言,就会一直受其困扰,直到您手动更改它为止.所以我创建了wp-lang.php.它首先查看用户从可用语言链接中选择的语言,然后将其存储在SESSION变量中,以便状态保持不变.最后,如果没有选择,并且没有SESSION变量,那么代码将查看浏览器的默认语言.

WordPress allows for internationalization; however, once you pick a language you are stuck with it till you change it manually. So I created wp-lang.php. It looks first at what language the user chose from the available language links, It then stores that in a SESSION variable so that state will be persistent. Finally, if no choice was made and there is not SESSION variable, the code will look at the default languages for the browser.

现在,所有这些更改取决于您是否下载了与网站上语言选择相对应的语言包.语言MO文件包含WordPress中所有函数名称的翻译.因此,如果用户登录到管理"面板,则将翻译所有内容.如果您的主题编码正确,那么您的菜单标题和元信息也将被翻译.毕竟,我修改了wp-config文件以包括wp-lang.现在,该站点的设置将在西班牙语和英语之间切换.

Now, all these changes are dependent upon whether you have downloaded a language package that corresponds with the choices of languages on the site. The language MO file contains translations of all the function names in WordPress. So if the user logins into the Admin panel, everything will be translated. If your theme is coded properly, then your menu headings and meta information will be translated as well. After all that, I modified the wp-config file to include wp-lang. Now the site this is set up on will switch between Spanish and English.

* wp-lang.php

*wp-lang.php

 session_start();
 if ( isset( $_GET['lang'] ) ) {
    $_SESSION['WPLANG'] = $_GET['lang'];
    define ('WPLANG', $_SESSION[WPLANG]);
 } else {
    if(isset($_SESSION['WPLANG'])) {
        define ('WPLANG', $_SESSION['WPLANG']);
        $_GET['lang'] = $_SESSION['WPLANG'];
    } else {
        if ( isset( $_SERVER["HTTP_ACCEPT_LANGUAGE"] ) ) {
            $languages = strtolower( $_SERVER["HTTP_ACCEPT_LANGUAGE"] );
             $languages = explode( ",", $languages );
            $_SESSION['WPLANG'] = $languages[0];
            $_SESSION['WPLANG'] = str_replace("-", "_", $_SESSION['WPLANG']);
            $_GET['lang'] = substr($_SESSION['WPLANG'],0,2);
            define ('WPLANG', $_SESSION[WPLANG]);
        } else {
            define ('WPLANG', '');
        }
    }
 }

* wp-config.php-查找定义了常量WPLANG的部分.在WPLANG声明之前添加以下行.

*wp-config.php - Find the section where the constant WPLANG is defined. Add in the following line just before the WPLANG declaration.

 require_once(dirname(__FILE__).'/wp-lang.php');
 define ('WPLANG', ''); 

此页面将首先检查浏览器的默认语言,然后设置语言.如果不是,用户还可以根据他们从帖子中选择的语言来设置语言.在会话变量中将语言设置为在整个访问过程中保持状态.

This page will first check for the default language of the browser and then set the language. If not, the user can also set the language based on the the one they pick from the posts. The language is set in a session variable to hold state for the entire visit.

这篇关于如何通过单击链接(切换语言)来更改Wordpress中的WPLANG值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆