在PHP中实现多语言的最佳方法 [英] Best way to implement multilanguage in PHP
问题描述
我有30多种语言的多语言PHP系统.每个定义都是通过define()进行的:
I have multilanguage PHP system with over 30 languages. Each definition is made with define():
define('LN_loader_alt', 'Loading...');
有超过400个定义.
我进行了一些比较,并创建了具有400多个常量的抽象类Lang,这些常量当然代替了define(),例如:
I made a little comparison and created abstract class Lang with over 400 constants and these constant are instead of define() of course, example:
abstract class Lang{
const LN_loader_alt = 'Loading';
...
}
接下来,我需要两个文件,并从Lang类调用了1 define和1 const.接下来,我使用webgrind进行了分析,结果令我惊讶:
Next I required both files and called 1 define and 1 const from class Lang. Next I made analysis with webgrind and results surprised for me:
1)要求具有400个define()的文件执行脚本的时间超过了70% 2)使用带有常量的抽象类比define()快得多
1) requiring file with 400 defines() took over 70% times of executing script 2) Using abstract class with constant is much faster than define()
我想与您分享这个分析并询问您:将我的系统从"define()"多语言重写为具有常量的抽象类"是最佳且明智的做法吗?也许有任何不利之处?
I'd like to share with You this analysis and ask You: is it optimal and smart to rewrite my system from "define()" multilanguage to "abstract class with constant" multilanguage? Or maybe has it any disadvantages?
推荐答案
我自发地猜测是define()
是具有一定开销的运行时函数调用,但是在编译时会解析类定义及其常量.因此有所不同.
My spontaneous guess would be that define()
is a runtime function call with a certain overhead, but a class definition and its constants are parsed at compile time. Hence the difference.
话虽如此,这两种解决方案都很糟糕. I18n 是一个已解决的问题,无需再次提出. i18n中最大的问题是工作流程,您需要支持该工作流程的工具.该工作流程是:
Having said that, both solutions are terrible. I18n is a solved problem that does not need to be reinvented yet again. The biggest problem in i18n is the workflow, and you need tools that support that workflow. That workflow is:
- 源中可翻译字符串的标记
- 将所述字符串提取为源代码中性格式
- 将这些字符串翻译成各种语言
- 上述所有内容都是异步的,并且由不同的人员进行并行更改,并保持所有语言之间的所有同步
已经有一些工具可以完成所有这些工作,其中最成熟的一个是gettext.使用它(或类似的东西),不要使用常量.
There are tools that already do all this, the most established one being gettext. Use it (or something similar), don't use constants.
请参阅:
- http://www.gnu.org/software/gettext/
- http://pology.nedohodnik.net//doc/user/en_US/ch-poformat.html
这篇关于在PHP中实现多语言的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!