使用 getttext 本地化 PHP 内容?
我有一个 PHP 应用程序,现在我需要实现多语言支持。这是我第一次必须处理这个问题。 我在互联网上进行了一些搜索,总是找到 PHP 的 gettext 函数,我已经在我的服务器上编译了该函数。 我只是想知道 gettext() 是否是执行此操作的最佳方法?大多数文章甚至可以追溯到 2002 年,PHP 5.2 中是否有一种新方法? 我还读到,当你调整翻译时,你必须重新启动服务器?
I have a PHP application and now I need to implement multi language support. This is the first time I have to deal with this.
I did some searches on the internet and always come to PHP's gettext function, which I have compiled on my server.
I would just like to know if gettext() is the best way doing this? Most articles date back to even 2002, isn't there a new way maybe in PHP 5.2?
Also I read that you have to reboot the server when you make adjustments to the translations??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
intl 扩展使用新的 ICU 库,但仅在 PHP 5.3+ 中可用
The intl extension uses the new ICU library but it's only available in PHP 5.3+
是的,重新启动服务器是一个主要问题,这对我来说是一个障碍。另外 gettext 的主要目的是翻译语言字符串,而不是用文本替换常量,这是否对您有好处由您决定(即文本替换是 'Adog is Brown'=>'Das hund ist braun' (我不'不懂德语;),常量替换为 'catalog_greeting'=>'欢迎来到目录')。
有许多替代的纯 PHP 解决方案可能适合您。我使用常量替换方案,将其保存在数据库中,并在每次保存时为每种语言创建一个单独的序列化数组,因此获取速度非常快,并且数据库格式与性能无关。效果很好,易于设置(甚至从头开始),易于维护和扩展。
Yes, rebooting the server is a major issue which was a dealbraker for me. Also gettext's primary intention is to translate language strings, not substitute constants with text, whether that's good for you is for you to decide (i.e. text replacement is 'A dog is brown'=>'Das hund ist braun' (I don't know German;), constant replacement is 'catalog_greeting'=>'Welcome to the catalog').
There are lots of alternative pure-PHP solutions that may work for you. I use a constant replacement scheme that I save in the database and create a separate serialized array for every language on each save, so fetching is extremely quick and the database format matters does not matter to performance. Works great, easy to setup (even from scratch), easy to maintain and extend.