简体   繁体   English

用gettext但没有语言环境麻烦的i18n?

[英]i18n with gettext but without the locale hassle?

I am looking for a standards-compliant way to store multi-language content for a Web Application. 我正在寻找一种符合标准的方法来存储Web应用程序的多语言内容。 Until now, I have employed my own "translate()" functions that read data from a file or a dictionary table in a database. 到目前为止,我已经使用了自己的“translate()”函数,这些函数从数据库中的文件或字典表中读取数据。 However, keeping the dictionaries up to date in a database table is very cumbersome if you work with different copies of the web app. 但是,如果您使用Web应用程序的不同副本,则在数据库表中保持字典最新是非常麻烦的。

I like gettext because there is a multitude of tools available for it. 我喜欢gettext,因为有很多可用的工具。 However, I develop applications for different platforms. 但是,我为不同的平台开发应用程序。 I am absolutely unwilling to deal with the crappy ways of setlocale() and consorts - namely the dozens of different locale string variations differing from system to system that you need to provide for to get it working. 我绝对不愿意处理setlocale()和consorts的糟糕方式 - 即需要提供的几十种不同的区域设置字符串变体,以使其工作。 Never ever. 永远不能。 I have a set of languages - say de, en, and es - and I want to load the appropriate dictionary and work with _() without touching setlocale() or bindtexdomain() once. 我有一组语言 - 比如de,en和es - 我想加载适当的字典并使用_()而不需要触及setlocale()或bindtexdomain()一次。

Is this somehow possible using gettext? 这是否可以使用gettext? Or does somebody know another simple, small, fast (!) i18n solution for PHP that can work with .po/.mo files, preferably without requiring a PHP extension? 或者有人知道另一个简单,小巧,快速(!)i18n的PHP解决方案,可以使用.po / .mo文件,最好不需要PHP扩展吗?

This method should work even with non-standard locales: 即使使用非标准语言环境,此方法也应该有效:

$locale = 'someWeirdLocale';

putenv('LC_MESSAGES=' . $locale);
bindtextdomain('domain', './locale');
textdomain('domain');

Enjoy! 请享用!

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

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