atelierspierrot/internationalization

此包已被废弃且不再维护。没有建议的替代包。

一个用于管理 i18n 的 PHP 包:根据本地化管理翻译、复数化和日期格式。

v1.1.0 2015-03-04 22:25 UTC

This package is not auto-updated.

Last update: 2024-03-02 10:17:40 UTC


README

demonstration documentation Code Climate

一个用于管理 i18n 的 PHP 包:根据本地化管理翻译、复数化、日期和数字格式。

用法

对象创建和 PHP 使用

要创建一个新的 I18n 实例,你需要传递一个 I18n\Loader 对象

// Creation of the I18n Loader
$i18n_loader = new \I18n\Loader(array(

    // this is the directory where your language strings are defined
    'language_directory' => __DIR__.'/i18n',

    // this is the list of available languages
    'available_languages' => array(
        'en' => 'en_US_USD',
        'gb' => 'en_GB_UKP',
        'fr' => 'fr_FR_EUR'
    ),
    'default_language' => 'en',

    // this is the tag construction used for replacements in strings
    // by default, "%arg%" will be replacement by the argument "arg" value
    // as this will be passed to a 'sprintf' PHP function, literal percent is written '%%'
    'arg_wrapper_mask' => "%%%s%%",
));

// Creation of the I18n instance (statically) passing it the Loader
$translator = \I18n\I18n::getInstance($i18n_loader);

有关可能的 Loader 选项的完整列表,请参阅源代码。

任何定义目录路径或文件名构造的选项值都可以包含一个 %s 标签,该标签将被当前语言替换。

// for instance:
'language_directory' => __DIR__.'/i18n/%s'

// will render, for the EN language:
'language_directory' => __DIR__.'/i18n/EN'

如你所见,I18n 类被定义为单例对象:任何对 \I18n\I18n::getInstance() 的未来调用都将引用第一个创建的对象实例。

然后,要使用字符串的翻译值,使用 translate 方法

$translator->translate( 'string_index' [, array( arguments )] [, language code] )

你可以使用 pluralize 方法根据项目数量选择翻译字符串

$indexes = array(
    0=>'test_item_zero',
    1=>'test_item_one',
    2=>'test_item_two',
    3=>'test_item_multi'
);
$translator->pluralize( $indexes, $number_of_items [, array( arguments )] [, language code] )

翻译字符串定义

默认情况下(这可以在 Loader 中重写),I18n 对象将加载定义为 PHP 数组的字符串

$i18n_en = array (
  'datetime_mask' => '%a %e %m %Y %H:%M:%S',
  'test' => 'Test in english',
  'test_args' => 'I received arguments : « %arg1% » and « %arg2% »',
  'test_item_zero' => 'No item',
  'test_item_one' => 'Just one item',
  'test_item_two' => 'Two items',
  'test_item_multi' => 'There are %nb% items',
);

这可以定义在一个名为 i18n.CODE.php 的文件中,其中 CODE 是语言的两位字母引用。这些文件将在 language_directory Loader 选项值中搜索和加载。

加载多个语言文件

I18n 对象被设计为可以通过以下方式轻松加载多个语言文件

$i18n->loadFile( my file path )

每个加载的文件都存储在对象的内部缓存系统中(一个简单的 PHP 数组)。

包别名

一组在全局命名空间中定义的别名函数可用,并由 Composer 自动加载

function _T(...) = $i18n->translate(...)
#    or
function translate(...) = $i18n->translate(...)

function _P(...) = $i18n->pluralize(...)
#    or
function pluralize(...) = $i18n->pluralize(...)

function _D(...) = $i18n->getLocalizedDateString(...)
#    or
function datify(...) = $i18n->getLocalizedDateString(...)

扩展

该包包含一个扩展,可用于在 Twig 模板引擎 中使用该类。有关更多信息,请参阅Twig 扩展页面

安装

有关如何安装此包及其命名空间的信息,请参阅我们的 用法 文档

如果您是 Composer 用户,只需将包添加到项目的 composer.json 清单文件的要求中

"atelierspierrot/internationalization": "@stable"

您可以使用特定的版本或主版本的最新版本,使用适当的 版本约束

请注意,此包依赖于外部的 PHP 模式PHP 库

作者 & 许可证

国际化

http://github.com/atelierspierrot/internationalization

版权所有 (c) 2010-2016,Pierre Cassat 和贡献者

Apache 2.0 许可证。

https://apache.ac.cn/licenses/LICENSE-2.0

Les Ateliers Pierrot - 巴黎,法国

http://www.ateliers-pierrot.fr/ - contact@ateliers-pierrot.fr