如何通过单击链接(切换语言)来更改 WordPress 中的 WPLANG 值?
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我通过在 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
*wp-config.php - 查找定义常量 WPLANG 的部分。在 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
*wp-config.php - Find the section where the constant WPLANG is defined. Add in the following line just before the WPLANG declaration.
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.
我的 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), thendie()
. That makes the URL clean off theGET
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.
我最好的解决方案是......(session_start();在顶部)
My best solution was... (session_start(); on top)