erebot / intl
Erebot 的 I18N API
0.2.3
2019-01-02 20:26 UTC
Requires
- php: >=7.0
- ext-iconv: *
- ext-intl: *
- ext-reflection: *
- ext-spl: *
- fpoirotte/php_parsergenerator_installer: ^0.1.0
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-15 22:47:23 UTC
README
这个库最初是为了为 Erebot IRC 机器人提供国际化(I18N)功能而开发的。
它主要用于与使用 gettext 系列程序生成的文件协同工作。它可以加载 GNU Gettext PO 和 MO 文件,以及 Solaris Gettext MO 文件。
如果你曾经使用过 Gettext,那么这个库的编程接口相当简单。它松散地基于 Python gettext
模块的接口,支持 PHP 7.0+,且不需要 Gettext
PHP 扩展。
安装
要在项目中使用此库,您必须已经使用 Composer 依赖管理器。
只需将此库添加到项目依赖项中
$ php /path/to/composer.phar require erebot/intl
用法
开始使用此库的最简单方法是使用 GettextFactory
抽象类,如下所示
<?php // This is the name of the domain the translations will be taken from. $domain = 'messages'; // This is the path to a directory containing a Gettext-compatible file hierarchy. $localedir = __DIR__ . DIRECTORY_SEPARATOR . 'i18n'; // This is a list of languages to try and use, in order of descending preference. $languages = array('fr_FR'); // Retrieve an instance of a translator. // This will first try to load a GNU Gettext PO file for the given domain/language, // then fall back to a GNU Gettext MO file, and then to a Solaris MO file. $translator = \Erebot\Intl\GettextFactory::translation($domain, $localedir, $languages); // Translate a single word, using the default context. // In French, this could be translated into "lunettes" (eyeglasses) echo $translator->gettext('glasses'); // Translate the same word, using a specific context. // "_" is an alias/shorthand for the "gettext" method. // In French, this would be translated into "verres" (drinking glasses) echo $translator->gettext('glasses', 'drinks'); // Translate a sentence using either the appropriate singular or plural form. // Please note that "ngettext" is called first to retrieve the proper translation // based on the number of lines in the file, then "printf" is used to replace // the "%d" format placeholder with that same value. echo printf($translator->ngettext('There is %d line in this file', 'There are %d lines in this file', __LINE__), __LINE__) . PHP_EOL;
许可协议
此库是免费软件:您可以在自由软件基金会发布的 GNU 通用公共许可证(GPL)的条款下重新分发和/或修改它,无论是许可证的第 3 版,还是(根据您的选择)任何后续版本。
分发此库的目的是希望它是有用的,但没有任何保证;甚至没有关于其商业性或针对特定目的的适用性的暗示性保证。有关详细信息,请参阅 GNU 通用公共许可证。
您应已收到一份 GNU 通用公共许可证副本。如果没有,请参阅 <https://gnu.ac.cn/licenses/>。