cracksalad/i18n
PHP库,以无状态方式正确处理国际化,不使用gettext()!
0.1.2
2024-07-13 10:31 UTC
Requires
- php: >=8.0
- ext-intl: *
- gettext/gettext: ^5.7
This package is auto-updated.
Last update: 2024-09-13 10:52:03 UTC
README
此库依赖于php-gettext/Gettext,并受到delight-im/PHP-I18N以及gettext的启发。
尽管此库主要兼容delight-im/PHP-I18N,但仍有一些差异。主要区别在于,此库不使用gettext()
和setlocale()
,而是采用无状态方式,因此支持独立(并发)请求处理,类似于PSR-15: HTTP Handlers和OpenSwoole的处理方式。另一个区别是,此库不会尝试根据当前URL的域名和路径或某些cookie来检测用户的语言。
安装
注意:此库需要PHP 8.0+
composer require cracksalad/i18n
使用方法
首先,您需要在类似这样的文件夹结构中创建翻译文件(例如,使用bash中的xgettext
)
locale/
├─ en/
│ ├─ LC_MESSAGES/
│ │ ├─ messages.mo
│ │ ├─ messages.po
├─ de/
│ ├─ LC_MESSAGES/
│ │ ├─ messages.mo
│ │ ├─ messages.po
use Cracksalad\I18n\I18n; $i18n = I18n::load('en'); $i18n->_('Hello world!'); // -> Hello world! // format (using sprintf() internally) $i18n->_f('%d items saved', 42); // -> 42 items saved // ICU MessageFormat $i18n->_fe('I first published this library on {0, date, short}', 1692982322); // -> I first published this library on 8/23/23 // singular vs. plural with format $i18n->_pf('%d item saved', '%d items saved', 42); // -> 42 items saved $i18n->_pf('%d item saved', '%d items saved', 1); // -> 1 item saved
许可证
此库采用MIT许可证(MIT)授权。有关更多信息,请参阅LICENSE。