如何通过单击链接(切换语言)来更改 WordPress 中的 WPLANG 值?

发布于 2024-09-16 13:36:11 字数 214 浏览 6 评论 0原文

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

据我所知,我可以通过在 wp-config.php 中定义 WPLANG 的值来更改语言(在本例中定义 ('WPLANG', 'es');)

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

这样做的最佳方法是什么?

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

简单气质女生网名 2024-09-23 13:36:11

我通过在 http:// www.treutech.com/files/wordpress/jLanguage.zip。 *作者的网站不再提供该插件,因此我正在托管我的更新版本。这允许您使用语法[english][/english]来格式化您的博客文章和页面。它将查询字符串传递给代码以了解要使用哪种语言。我首先修改代码,以便它使用标准的两字母语言代码。我还取消了代码用来表示各种语言的标志。然而,毕竟,该插件仍然只翻译页面或帖子。我希望根据用户的选择来翻译网站的其余部分。

WordPress 允许国际化;然而,一旦你选择了一种语言,你就会一直使用它,直到你手动更改它。所以我创建了wp-lang.php。它首先查看用户从可用语言链接中选择的语言,然后将其存储在 SESSION 变量中,以便状态得以持久。最后,如果没有做出选择并且没有 SESSION 变量,代码将查看浏览器的默认语言。

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

*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 声明之前添加以下行。

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

该页面将首先检查浏览器的默认语言,然后设置语言。如果没有,用户还可以根据他们从帖子中选择的语言来设置语言。语言在会话变量中设置以保存整个访问的状态。

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 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.

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

 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 - 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.

冰葑 2024-09-23 13:36:11

我的 2 美分:我做了类似的事情,但我也从 URL 中删除了 ?lang=XX 参数。

完成所有语言检测工作后,我在代码中执行 307 重定向,重定向到同一 URL(删除了 ?lang=xxx&lan=xxx来自它),然后是die()。这使得 URL 摆脱了 GET 变量,变得干净并且可以安全地添加书签。

我还将语言首选项存储在 cookie 中,以允许用户在下次访问时看到相同的语言。

My 2 cents: I do a similar thing, but I also remove the ?lang=XX parameter from URL.

After all work on language detection is done, I do the 307-redirection in the code, to the same URL (with ?lang=xxx or &lan=xxx removed from it), then die(). That makes the URL clean off the GET variable, clean and potentially safe for bookmarking.

Also I store the language preference in a cookie, to allow user see the same language upon next visit.

盗梦空间 2024-09-23 13:36:11

我最好的解决方案是......(session_start();在顶部)

if( !isset( $_GET['lang'] ) && isset($_SESSION['WPLANG']) ) {
        define ('WPLANG', $_SESSION['WPLANG']);
} else {

    if ( !isset( $_GET['lang'] ) ) {
      if ( isset( $_SERVER["HTTP_ACCEPT_LANGUAGE"] ) ) {
        $languages = strtolower( $_SERVER["HTTP_ACCEPT_LANGUAGE"] );
        $languages = explode( ",", $languages );
        $wplang = str_replace("-", "_", $languages[0]);
        $_GET['lang'] = substr($wplang,0,2);
      }
    }

    $language = isset( $_GET['lang'] ) ? htmlspecialchars($_GET['lang'], ENT_QUOTES) : 'es';
    switch ( $language ) {
        case 'en':
            define( 'WPLANG', 'en_US' );
            $_SESSION['WPLANG'] = 'en_US';
        break;

        case 'es':
        default:
            define( 'WPLANG', 'es_ES' );
            $_SESSION['WPLANG'] = 'es_ES';
    }

}

My best solution was... (session_start(); on top)

if( !isset( $_GET['lang'] ) && isset($_SESSION['WPLANG']) ) {
        define ('WPLANG', $_SESSION['WPLANG']);
} else {

    if ( !isset( $_GET['lang'] ) ) {
      if ( isset( $_SERVER["HTTP_ACCEPT_LANGUAGE"] ) ) {
        $languages = strtolower( $_SERVER["HTTP_ACCEPT_LANGUAGE"] );
        $languages = explode( ",", $languages );
        $wplang = str_replace("-", "_", $languages[0]);
        $_GET['lang'] = substr($wplang,0,2);
      }
    }

    $language = isset( $_GET['lang'] ) ? htmlspecialchars($_GET['lang'], ENT_QUOTES) : 'es';
    switch ( $language ) {
        case 'en':
            define( 'WPLANG', 'en_US' );
            $_SESSION['WPLANG'] = 'en_US';
        break;

        case 'es':
        default:
            define( 'WPLANG', 'es_ES' );
            $_SESSION['WPLANG'] = 'es_ES';
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文