多语言PHP网站的最有效方法 [英] Most efficient approach for multilingual PHP website

查看:75
本文介绍了多语言PHP网站的最有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个大型的多语言网站上工作,并且我正在考虑使它成为多语言网站的不同方法.我能想到的替代方法是:

I am working on a large multilingual website and I am considering different approaches for making it multilingual. The possible alternatives I can think of are:

  1. Gettext 函数可生成.po文件
  2. >
  3. 一个MySQL表,其中包含翻译和每个文本的唯一字符串ID
  4. 带有数组的PHP文件,这些数组包含具有唯一字符串ID的不同翻译
  1. The Gettext functions with generation of .po files
  2. One MySQL table with the translations and a unique string ID for each text
  3. PHP-files with arrays containing the different translations with unique string IDs

据我了解,Gettext函数应该是最高效的,但是我的要求是应该可以使用原始参考语言(英语)更改文本字符串,而该字符串的其他翻译不自动恢复为英语只是因为几个单词发生了变化. Gettext有可能吗?

As far as I have understood the Gettext functions should be most efficient, but my requirement is that it should be possible to change a text string in the original reference language (English) without the other translations of that string automatically reverting back to English just because a couple of words changed. Is this possible with Gettext?

什么是最少的资源需求解决方案?
是使用Gettext函数或带有数组的PHP文件或多或少相等地需要资源吗?
还有其他建议来寻求更有效的解决方案吗?

What is the least resource demanding solution?
Is using the Gettext functions or PHP files with arrays more or less equally resource demanding?
Any other suggestions for more efficient solutions?

推荐答案

一些注意事项:

1.翻译
谁来做翻译?那些也连接到该站点的人?翻译公司?使用Gettext时,将使用"pot"(.po)文件.这些文件包含消息ID和消息字符串(翻译).示例:

1. Translations
Who will be doing the translations? People that are also connected to the site? A translation agency? When using Gettext you'll be working with 'pot' (.po) files. These files contain the message ID and the message string (the translation). Example:

msgid "A string to be translated would go here"  
msgstr ""

现在,对于任何需要翻译的人来说,这看起来都很好并且可以理解.但是,当您使用麦克建议的关键字而不是完整的句子时,会发生什么?如果有人需要翻译一个称为"address_home"的msgid,则他或她不知道这应该是标头家庭住址"还是完整的句子.在这种情况下,请确保在调用gettext函数之前,立即在文件中添加注释,如下所示:

Now, this looks just fine and understandable for anyone who needs to translate this. But what happens when you use keywords, like Mike suggests, instead of full sentences? If someone needs to translate a msgid called "address_home", he or she has no clue if this is should be a header "Home address" or that it's a full sentence. In this case, make sure to add comments to the file right before you call on the gettext function, like so:

/// This is a comment that will be included in the pot file for the translators
gettext("ready_for_lost_episode");

在创建.po文件时使用xgettext --add-comments=///将添加这些注释.但是,我认为Gettext不能以这种方式使用.另外,如果您需要添加要显示的每个文本注释,则a)可能会在某个时候出错,b)您的整个脚本仍然会被文本填充,仅在注释形式中,c)注释需要直接放在Gettext函数的上方,但并不总是很方便,具体取决于函数在代码中的位置.

Using xgettext --add-comments=/// when creating the .po files will add these comments. However, I don't think Gettext is ment to be used this way. Also, if you need to add comments with every text you want to display you'll a) probably make an error at some point, b) you're whole script will be filled with the texts anyway, only in comment form, c) the comments needs to be placed directly above the Gettext function, which isn't always convient, depending on the position of the function in your code.

2.维护
一旦您的网站发展壮大(甚至更进一步),并且您的语言文件也随之增长,以这种方式维护所有不同的翻译可能会变得非常困难.每次添加文本时,都需要创建新文件,将文件发送给翻译人员,再接收文件,确保结构仍然完整(急切的翻译人员也总是乐于翻译语法,从而制作出完整的文件)无法使用:)),然后导入新的翻译.可以,当然可以,但是要注意,大型站点和许多不同的语言可能会为此带来问题.

2. Maintenance
Once your site grows (even further) and your language files along with it, it might get pretty hard to maintain all the different translations this way. Every time you add a text, you need to create new files, send out the files to translators, receive the files back, make sure the structure is still intact (eager translators are always happy to translate the syntax as well, making the whole file unusable :)), and finish with importing the new translations. It's doable, sure, but be aware with possible problems on this end with large sites and many different languages.


另一种选择:结合第二种和第三种选择:

我个人认为,使用(简单的)CMS管理翻译,将变量和翻译保存在数据库中并将相关文本自动导出到语言文件中会更加有用:

Personally, I find it more useful to manage the translation using a (simple) CMS, keeping the variables and translations in a database and export the relevent texts to language files yourself:

  1. 将变量添加到数据库中(例如:id,页面,变量);
  2. 将翻译添加到这些变量中(例如:id,varId,语言,翻译);
  3. 选择相关变量和翻译,将它们写入文件;
  4. 在您的网站中包括相关语言文件;
  5. 创建您自己的函数以显示变量文本:

text('var');或类似__('faq','register','lost_password_text');

第3点可以很简单,就像从数据库中选择所有相关变量和转换,将它们放入数组并将序列化的数组写入文件一样.

Point 3 can be as simple as selecting all the relevant variables and translations from the database, putting them in an array and writing the serlialized array to a file.

优势:

  1. 维护.对于大型项目,维护文本会容易得多.您可以通过简单地在数据库中添加一列来定义该变量属于网站的哪个部分,从而按网站中的页面,部分或网站的其他部分对变量进行分组.这样一来,您就可以快速提取例如常见问题页面.

  1. Maintenance. Maintaining the texts can be a lot easier for big projects. You can group variables by page, sections or other parts within your site, by simply adding a column to your database that defines to which part of the site this variable belongs. That way you can quickly pull up a list of all the variables used in e.g. the FAQ page.

翻译.您可以在单个页面上显示变量以及所有不同语言的所有译文.这对于可以同时将文本翻译成多种语言的人可能很有用.看到其他翻译以了解上下文可能会很有用,以使翻译尽可能地好.您还可以查询数据库,以查找已翻译的内容和未翻译的内容.也许添加时间戳以跟踪可能的过时翻译.

Translating. You can display the variable with all the translations of all the different languages on a single page. This might be useful for people who can translate texts into multiple languages at the same time. And it might be useful to see other translations to get a feel for the context so that the translation is as good as possible. You can also query the database to find out what has been translated and what hasn't. Maybe add timestamps to keep track of possible outdated translations.

访问.这取决于谁进行翻译.您可以使用简单的登录名将CMS包装起来,以在需要时授予翻译机构人员的访问权限,并且仅允许他们更改某些语言甚至网站的某些部分.如果不是这个选项,您仍然可以将数据输出到可以手动转换并稍后导入的文件中(尽管这可能会遇到与前面提到的相同的问题.).您可以添加已经存在的一种翻译(英语或另一种主要语言)作为翻译的上下文.

Access. This depends on who will be translating. You can wrap the CMS with a simple login to grant access to people from a translation agency if need be, and only allow them to change certain languages or even certain parts of the site. If this isn't an option you can still output the data to a file that can be manually translated and import it later (although this might come with the same problems as mentioned before.). You can add one of the translations that's already there (English or another main language) as context for the translator.

总的来说,我认为您会发现以这种方式可以更好地控制翻译,特别是从长远来看.与本机gettext函数相比,我无法告诉您有关此方法的速度或效率的任何信息.但是,根据语言文件的大小,我认为这不会有太大的区别.如果按页面或部分对变量进行分组,则始终只能包括必需的部分.

All in all I think you'll find that you'll have a lot more control over the translations this way, especially in the long run. I can't tell you anything about speed or efficiency of this approach compared to the native gettext function. But, depending on the size of the language files, I don't think it'll be a big difference. If you group the variables by page or section, you can alway include only the required parts.

这篇关于多语言PHP网站的最有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆