在PHP应用程序中实现国际化(语言字符串) [英] Implementing internationalization (language strings) in a PHP application

查看:71
本文介绍了在PHP应用程序中实现国际化(语言字符串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想构建一个可以处理获取区域设置字符串以支持国际化的CMS.我计划将字符串存储在数据库中,然后在数据库和应用程序之间放置键值/值缓存(如内存缓存),以防止由于每次访问数据库而导致性能下降.

I want to build a CMS that can handle fetching locale strings to support internationalization. I plan on storing the strings in a database, and then placing a key/value cache like memcache in between the database and the application to prevent performance drops for hitting the database each page for a translation.

这比使用带有字符串数组的PHP文件要复杂得多,但是当您有2,000条翻译行时,该方法效率极低.

This is more complex than using PHP files with arrays of strings - but that method is incredibly inefficient when you have 2,000 translation lines.

我考虑过使用 gettext ,但是我不确定CMS的用户将可以轻松使用gettext文件.如果字符串存储在数据库中,则可以设置一个不错的管理系统以允许它们随时进行更改,并且RAM中的缓存将确保这些字符串的获取与gettext一样快或更快.考虑到

I thought about using gettext, but I'm not sure that users of the CMS will be comfortable working with the gettext files. If the strings are stored in a database, then a nice administration system can be setup to allow them to make changes whenever they want and the caching in RAM will insure that the fetching of those strings is as fast, or faster than gettext. I also don't feel safe using the PHP extension considering not even the zend framework uses it.

这种方法有什么问题吗?

Is there anything wrong with this approach?

我想也许我会多加思考.字符串翻译的问题之一是它们不支持日期,金额或条件语句.但是,由于有了 intl ,PHP现在有了

I thought perhaps I would add more food for thought. One of the problems with string translations it is that they doesn't support dates, money, or conditional statements. However, thanks to intl PHP now has MessageFormatter which is what really needs to be used anyway.

// Load string from gettext file
$string = _("{0} resulted in {1,choice,0#no errors|1#single error|1<{1, number} errors}");

// Format using the current locale
msgfmt_format_message(setlocale(LC_ALL, 0), $string, array('Update', 3));

另一方面,关于gettext,我不喜欢的一件事是将文本完全嵌入到应用程序中.这意味着负责主要翻译(通常是英语)的团队必须有权访问项目源代码,以在放置默认语句的所有位置进行更改.这几乎与到处都是SQL意大利面条代码的应用程序一样糟糕.

On another note, one of the things I don't like about gettext is that the text is embedded into the application all over the place. That means that the team responsible for the primary translation (usually English) has to have access to the project source code to make changes in all the places the default statements are placed. It's almost as bad as applications that have SQL spaghetti-code all over.

因此,使用诸如_('error.404_not_found')之类的键是有意义的,然后使内容编写者和翻译者只担心PO/MO文件而不会弄乱代码.

So, it makes sense to use keys like _('error.404_not_found') which then allow the content writers and translators to just worry about the PO/MO files without messing in the code.

但是,如果给定键的gettext转换不存在,那么就没有办法退回到默认值(就像使用自定义处理程序一样).这意味着您要么在代码中弄乱了代码,要么向没有语言环境翻译的用户显示了"error.404_not_found"!

However, in the event that a gettext translation doesn't exist for the given key then there is no way to fall back to a default (like you could with a custom handler). This means that you either have the writter mucking around in your code - or have "error.404_not_found" shown to users that don't have a locale translation!

此外,我不知道任何使用PHP的gettext的大型项目.我会感谢所有链接到常用的(因此经过测试)的链接,这些链接实际上依赖于本机PHP gettext扩展.

In addition, I am not aware of any large projects which use PHP's gettext. I would appreciate any links to well-used (and therefore tested), systems which actually rely on the native PHP gettext extension.

推荐答案

Gettext使用非常快速的二进制协议.同样,gettext实现通常更简单,因为它只需要echo _('Text to translate');即可.它还具有供翻译人员使用的现有工具,并且事实证明它们运行良好.

Gettext uses a binary protocol that is quite quick. Also the gettext implementation is usually simpler as it only requires echo _('Text to translate');. It also has existing tools for translators to use and they're proven to work well.

您可以将它们存储在数据库中,但是我觉得这样做会比较慢并且有点过大,尤其是因为您必须构建系统来自己编辑翻译.

You can store them in a database but I feel it would be slower and a bit overkill, especially since you'd have to build the system to edit the translations yourself.

如果只有您可以实际将查找缓存在APC的专用内存部分中,那么您将大开眼界.可悲的是,我不知道如何.

If only you could actually cache the lookups in a dedicated memory portion in APC, you'd be golden. Sadly, I don't know how.

这篇关于在PHP应用程序中实现国际化(语言字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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