PHP 核心 date() 格式的语言翻译
好的,我有一个经常使用的大型且复杂的“即将发生的事件”PHP 模块,我目前正在一个多语言(法语/英语)网站上工作,我需要翻译我的 PHP 日期的语言。
我不想做的是在函数本身中嵌套大量条件 PHP。有人可以帮我想出一系列条件语句来检查是否
<?php if (ICL_LANGUAGE_CODE == 'fr') { } ?>
为真,然后在函数处理后简单地更改月份变量。请原谅我的无知,我只是在语法方面有点慢。有人可以告诉我检查特定输出是否得到回显然后翻译它会是什么样子。以下内容不起作用
<?php if (ICL_LANGUAGE_CODE == 'fr') { February == Fevrier } ?>
编辑:
我考虑过使用本地化框架,但是我正在 WordPress 上开发此网站并使用 WPML(wordpress 多语言插件)。这是一个真正的救星。我对于添加复杂的框架犹豫不决。这个是轻量级的吗?我宁愿只翻译我所期望的字符串。唯一的格式是 - Jan 01 // January 1, 2011 ... 日期名称不会出现在网站上,并且不需要其他字符串翻译。
OK, so I have a large and complicated "upcoming events" PHP module that I work with frequently, I'm currently working on a multi-lingual (French / English) site and I need to translate the language of my PHP dates.
What I DO NOT WANT TO DO is nest a ton of conditional PHP within the function itself. Can someone help me to come up with a series of conditionals statements that check if
<?php if (ICL_LANGUAGE_CODE == 'fr') { } ?>
is true and then simply change the month variables after the function has processed. Forgive my ingorance, I'm just a bit slow when it comes to syntax. Can someone tell me what it would look like to check if a specific output is echoed and then translate it. The following is not working
<?php if (ICL_LANGUAGE_CODE == 'fr') { February == Fevrier } ?>
EDIT:
I've considered using a localization framework, however I'm developing this site on WordPress and using the WPML (wordpress multi-lingual plugin). It's been a real life-saver. I'm hesitant to add complex frameworks. Is this one lightweight? I'd prefer to just translate the strings that I know to expect. The only formats are - Jan 01 // January 1, 2011 ... day names don't appear on the site and other string translations would be unnecessary.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用字符串创建数组,并使用每种受支持语言的键:
然后您可以像这样访问字符串:
...等等。
Create arrays with your strings, with keys for each supported language:
Then you can access the strings like this:
...and so on.
您上面得到的建议都是有效的并且效果很好。
WPML 包含一个字符串翻译工具,它也可以做到这一点并保持代码整洁。优点是您的代码将包含一种语言的文本,并且翻译将在 WPML 的字符串翻译编辑器中完成。
如果您选择添加更多语言,则无需更改代码中的任何内容。只需在 WPML 中添加更多翻译即可。
看看这里:
http://wpml.org/documentation/ support/translation-for-texts-by-other-plugins-and-themes/
您需要使用 *icl_register_string* 来注册可翻译字符串。然后输出的时候,通过*icl_t*。此机制与 GetText 类似,不同之处在于它同时支持静态和动态文本。
如果您这样做,我建议您为字符串选择一个新的上下文字段。这将使您可以轻松地在字符串翻译编辑器中找到它们。
WPML 缓存结果并将具有相同上下文的所有字符串一起加载,因此您不会感到任何性能受到影响。
The suggestions you got above are all valid and would work fine.
WPML includes a String Translation facility which can also do this and keep the code clean. The advantage would be that your code will include texts in one language and the translation will be done in WPML's String Translation editor.
If you ever choose to add more languages, you don't need to change anything in the code. Just add more translations in WPML.
Have a look here:
http://wpml.org/documentation/support/translation-for-texts-by-other-plugins-and-themes/
You'll need to use *icl_register_string* to register translatable strings. Then, when outputting, pass through *icl_t*. This mechanism is similar to GetText, with the exception that it supports both static and dynamic texts.
If you do this, I suggest that you choose a new context field for your strings. This would make it easy to locate them in the strings translation editor.
WPML caches results and loads all strings with the same context together, so you're not going to feel any performance hit by this.
这是我在 WordPress 中用来快速将日期翻译为法语的方法:
This is what I use to quickly translate date to french in wordpress:
如果你的项目负载不高,可以尝试这种方法,它足够灵活:
Try this approach if your project is not highload, which is enough flexible:
将 php 语言更改为 wp
时间字符串
安全编码
Changing php language to wp
String from time
Safe encoding