yiisoft/i18n-formatter-intl

此包已被废弃且不再维护。作者建议使用yiisoft/translator-formatter-intl包。

Yii Translator intl 消息格式化器

1.0.0 2021-05-13 10:44 UTC

This package is auto-updated.

Last update: 2022-11-08 06:56:52 UTC


README

此包已弃用,现在它是yiisoft/translator的一部分。

68747470733a2f2f796969736f66742e6769746875622e696f2f646f63732f696d616765732f7969695f6c6f676f2e737667

Yii Translator intl 消息格式化器


Latest Stable Version Total Downloads Build status Scrutinizer Code Quality Code Coverage Mutation testing badge static analysis type-coverage

此包提供了利用 PHP intl 扩展消息格式化功能的消息格式化器。

要求

  • PHP 7.4 或更高版本。
  • intl PHP 扩展 1.0.2 或更高版本。
  • ICU 库 49.0 或更高版本。

安装

可以使用 composer 安装此包

composer require yiisoft/translator-formatter-intl --prefer-dist

配置

如果您使用 yiisoft/config,则会自动获取配置。如果没有,则需要以下 DI 容器配置

<?php

declare(strict_types=1);

use Yiisoft\Translator\MessageFormatterInterface;
use Yiisoft\Translator\Formatter\Intl\IntlMessageFormatter;

return [
    MessageFormatterInterface::class => IntlMessageFormatter::class,
];

通用用法

yiisoft/translator 一起使用的示例

/** @var \Yiisoft\Translator\Translator $translator **/

$categoryName = 'moduleId';
$pathToModuleTranslations = './module/messages/';
$additionalCategorySource = new Yiisoft\Translator\CategorySource(
    $categoryName, 
    new \Yiisoft\Translator\Message\Php\MessageSource($pathToModuleTranslations),
    new \Yiisoft\Translator\Formatter\Intl\IntlMessageFormatter()
);
$translator->addCategorySource($additionalCategorySource);

$translator->translate('Test string: {str}', ['str' => 'string data'], 'moduleId', 'en');
// output: Test string: string data

不使用 yiisoft/translator 包的示例

/** @var \Yiisoft\Translator\Formatter\Intl\IntlMessageFormatter $formatter */
$pattern = 'Total {count, number} {count, plural, one{item} other{items}}.';
$params = ['count' => 1];
$locale = 'en';
echo $formatter->format($pattern, $params, $locale);
// output: Total 1 item. 

$pattern = '{gender, select, female{Уважаемая} other{Уважаемый}} {firstname}';
$params = ['gender' => null, 'firstname' => 'Vadim'];
echo $formatter->format($pattern, $params, 'ru');
// output: Уважаемый Vadim 

$pattern = '{name} is {gender} and {gender, select, female{she} male{he} other{it}} loves Yii!';
$params = ['name' => 'Alexander', 'gender' => 'male'];
echo $formatter->format($pattern, $params, $locale);
// output: Alexander is male and he loves Yii! 

要获取您正在使用的区域设置可用的选项列表 - 请参阅 https://intl.rmcreative.ru/

测试

单元测试

此包使用 PHPUnit 进行测试。要运行测试

./vendor/bin/phpunit

突变测试

此包测试使用 Infection 突变框架和 Infection 静态分析插件 进行检查。要运行它

./vendor/bin/roave-infection-static-analysis-plugin

静态分析

代码使用 Psalm 进行静态分析。要运行静态分析

./vendor/bin/psalm

许可

Yii Translator intl 消息格式化器是自由软件。它根据 BSD 许可证的条款发布。有关更多信息,请参阅 LICENSE

Yii 软件 维护。

支持项目

Open Collective

关注更新

Official website Twitter Telegram Facebook Slack