简体   繁体   English

在PHP中实现多语言的最佳方法

[英]Best way to implement multilanguage in PHP

I have multilanguage PHP system with over 30 languages. 我有30多种语言的多语言PHP系统。 Each definition is made with define(): 每个定义都是通过define()进行的:

define('LN_loader_alt', 'Loading...');

And there is over 400 definitions. 并且有超过400个定义。

I made a little comparison and created abstract class Lang with over 400 constants and these constant are instead of define() of course, example: 我进行了一些比较,并创建了具有400多个常量的抽象类Lang,这些常量当然代替了define(),例如:

abstract class Lang{
    const LN_loader_alt = 'Loading';
    ...
}

Next I required both files and called 1 define and 1 const from class Lang. 接下来,我需要两个文件,并从Lang类调用了1 define和1 const。 Next I made analysis with webgrind and results surprised for me: 接下来,我使用webgrind进行了分析,结果令我惊讶:

1) requiring file with 400 defines() took over 70% times of executing script 2) Using abstract class with constant is much faster than define() 1)要求包含400个define()的文件占用了执行脚本的70%以上的时间2)使用具有常量的抽象类比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? 我想与您分享这个分析,然后问您:将我的系统从“ define()”多语言重写为“具有常量的抽象类”是最佳且明智的做法吗? Or maybe has it any disadvantages? 也许有什么缺点?

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. 我自发的猜测是define()是具有一定开销的运行时函数调用,但是类定义及其常量是在编译时进行解析的。 Hence the difference. 因此区别。

Having said that, both solutions are terrible. 话虽如此,这两种解决方案都是可怕的。 I18n is a solved problem that does not need to be reinvented yet again. I18n是一个已解决的问题,无需再次提出。 The biggest problem in i18n is the workflow, and you need tools that support that workflow. i18n中最大的问题是工作流程,您需要支持该工作流程的工具。 That workflow is: 该工作流程是:

  • markup of translatable strings in the source 源中可翻译字符串的标记
  • extraction of said strings into a source-code neutral format 将所述字符串提取为源代码中性格式
  • translation of those strings into various languages 将这些字符串翻译成各种语言
  • all of the above asynchronously with parallel changes happening done by separate people, and keeping everything synched up between all your languages 上述所有操作都是异步的,并且由不同的人员进行并行更改,并使所有语言之间的所有内容保持同步

There are tools that already do all this, the most established one being gettext. 已经有工具可以完成所有这些工作,其中最成熟的一种是gettext。 Use it (or something similar), don't use constants. 使用它(或类似的东西),不要使用常量。

See: 看到:

I believe that the ideal is to use helpers and perform the translation only on demand. 我认为理想的情况是使用助手并仅按需执行翻译。

Example: 例:

<?php
// View.php
//...
echo translate('Hello World');

The function search for the corresponding of expression in the current language dictionary or just echo "Hello World" if not found one translation. 该函数在当前语言词典中搜索表达式的对应项,如果未找到翻译,则仅回显“ Hello World”。

I consider good you rewrite your system, since 400 items is not so much so. 我认为您最好重写系统,因为400项不是那么多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM