为多语言 PHP 应用程序存储字符串的推荐方法
通常我将字符串与描述它们的关键字相关联,然后将它们存储在结构为 id int、id varchar(16)、语言 char(2)、string varchar 的数据库表中。然后,我创建一个 PHP 包装函数来根据访问者当前的语言获取每个字符串。
有人告诉我(我想在某处读过)使用 PHP 的内置方法来处理国际化。但我还没有找到任何真正“令人信服”的信息来说明为什么它应该比我的数据库方式更合适。
处理国际化网站字符串的最合适方法是什么?为什么?是否有一种性能更佳是我应该优先选择的?如果有性能更好的,我会改用它。
Normally I associate strings to keywords that describe them, and then store them in a database table with a structure of id int, id varchar(16), language char(2), string varchar
. Then I make a PHP wrapper function to fetch each string depending on the visitor’s current language.
I have been told (and read somewhere I think) to use PHP’s built in methods to handle internationalization. But I have not found any really “convincent” information about why it should be more appropriate than my database way to do it.
What is the most appropriate method to handle internationalized websites strings and why? is there one that I should always prefer for its better performance? If there is one with a better performance then I will switch to use it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这取决于您的目标是什么。如果您需要网站用户在线更新您的翻译,那么您的数据库方法就可以。然而,如果翻译是由专业翻译人员提供,那么最好将翻译与网站解耦。在这种情况下,使用 gettext 之类的东西是一个更好的主意 - 您将能够发送 .po 文件进行翻译,然后将其集成回您的网站。
思考整个流程,管理翻译人员、管理变更等。
It depends on what your objectives are. If you need your translations to be updated on-line by website users, then your database approach is ok. However, if translations are to be provided by professional translators, then it's better to decouple the translation from the web site. Using something like gettext is a better idea in this case - you will be able to send out .po files for translation, then integrate it back into your web site.
Think about the whole process, managing translators, managing changes, etc.
给猫剥皮的方法有很多种。我认为您已经收到此建议,因为 PHP 使用客户端设置来获取默认语言(尽管您可能正在使用类似的方法)。无论哪种方式,都必须存储多个字符串。如果您希望数据库保存这些字符串,那么就可以了。如果您没有看到任何性能问题,那就继续吧。不需要改变你觉得舒服的事情。
There are a number of ways to skin a cat. I think you've been given this advice because PHP uses the client settings to get the default language (although, you're probably using a similar method). Either way, multiple strings have to be stored. If you prefer the db save those strings, then that works. If you're not seeing any performance issues, then go with it. No need to change what you're comfortable with.
Gettext 似乎通常取决于每个进程的区域设置,并且它们也取决于系统安装的区域设置。对于 Web 应用程序,请考虑使用您自己的数据库实现以及一些已经指出的缓存方法。
Gettext seems to normally depend on each proccess’ locale settings, and they depend on system installed locales too. For Web Applications consider using your own Database implementation along with some caching methods already pointed out.