如何根据站点的 PHP lang 制作 php if/switch 语句?
我的网站有一种更改语言的方法,每次更改它时,我都会在源代码顶部看到类似的内容:
<html dir="ltr" lang="en-US">
<html dir="ltr" lang="zh-TW">
我认为使用 URL 也可能有效:
http://alexchen.info/en
http://alexchen.info/tw
或者也许是这样:
?lang=es
?lang=en
因为这也有效:
http://alexchen.info/?lang=es
http://alexchen.info/?lang=en
我想了解使用它来制作 php if 语句(或 switch 语句)的最佳方法是什么。 例如:
if(lang=en) {
// do this
}
if(lang=tw) {
// do this
}
My site has a way of changing its language, and each time I change it I see something like this at the top of the source code:
<html dir="ltr" lang="en-US">
<html dir="ltr" lang="zh-TW">
I think using the URL may also work:
http://alexchen.info/en
http://alexchen.info/tw
or maybe this:
?lang=es
?lang=en
because this works too:
http://alexchen.info/?lang=es
http://alexchen.info/?lang=en
I would like to know what's the best way of using that to make a php if statement (or switch statement).
For instance:
if(lang=en) {
// do this
}
if(lang=tw) {
// do this
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您有
?lang=en
,您只需通过$_GET
全局变量即可获取。但是,您应该首先将逻辑封装在函数中。If you have
?lang=en
you can simply get via the$_GET
global variable. However, you should first encapsulate the logic within a function.编写一个开关(可以在他们的文档中轻松查找) ):
尽管这可能不是进行网站翻译的最佳方法。我自己没有做过太多的多语言工作,但我看到很多框架将文本块包装在函数中
,然后
T
函数会用翻译后的文本替换该文本。这样你就不会让整个网站都充斥着 switch 语句。翻译可以存储在数据库中。如果缺少翻译,可以将其记录或作为空白条目插入数据库中,以便您知道稍后需要填写/翻译什么,而无需深入您的网站尝试查找所有需要翻译文本的位置。To write a switch (which could have easily been looked up in their documentation):
Although this probably isn't the best approach to doing site translation. I haven't done much multi-language stuff myself, but I see a lot of frameworks wrap blocks of text in functions like
And then the
T
function would replace that text with the translated text. That way you don't have you entire site littered with switch statements. The translations can be stored in a database. If there's a missing translation, that can be logged or inserted as a blank entry into your DB so that you know what you need to fill in/translate later without digging through your site trying to find all the places where text needs to be translated.