yiisoft / i18n-translator
Requires
- php: ^8.0
- psr/event-dispatcher: 1.0.0
- yiisoft/files: ^1.0|^2.0
- yiisoft/i18n: ^1.0
Requires (Dev)
- maglnet/composer-require-checker: ^4.2
- phpunit/phpunit: ^9.5
- rector/rector: ^0.15.2
- roave/infection-static-analysis-plugin: ^1.25
- spatie/phpunit-watcher: ^1.23
- vimeo/psalm: ^4.30|^5.4
- yiisoft/di: ^1.2
Suggests
- ext-intl: Allows using intl message formatter
- ext-tokenizer: Allows using message extraction
- yiisoft/event-dispatcher: To listen for events about missing categories and messages
This package is auto-updated.
Last update: 2024-05-22 14:30:02 UTC
README
Yii消息翻译器
此包允许将消息翻译成多种语言。它可以与基于Yii的应用程序和独立的PHP应用程序一起工作。
要求
- PHP 8.0或更高版本。
安装
可以使用Composer安装此包。
composer require yiisoft/translator
附加包
有两种类型的附加包。消息源提供对各种消息存储格式的支持,如PHP数组或GNU gettext。消息格式化程序提供在翻译消息中识别的额外语法。
消息源
- translator-message-php - PHP文件消息存储。
- translator-message-db - 数据库消息存储。
- translator-message-gettext - gettext消息存储。
内置消息格式化程序
提取消息
通过控制台提取器进行消息提取,该提取器搜索翻译器消息调用并构建翻译文件。
在某些情况下,您可能需要在没有使用控制台的情况下这样做。如果这是您的情况,请参阅提取器指南。
配置
快速入门
首先,获取配置的事件分配器实例。在框架中使用时,通常这样做
public function actionProcess(\Psr\EventDispatcher\EventDispatcherInterface $eventDispatcher) { // ... }
配置取决于使用的容器,因此以下我们将手动创建一个实例。
/** @var \Psr\EventDispatcher\EventDispatcherInterface $eventDispatcher */ $locale = 'ru'; $fallbackLocale = 'en'; $translator = new Yiisoft\Translator\Translator( $locale, $fallbackLocale, $eventDispatcher );
$fallbackLocale
和$eventDispatcher
是可选的。回退区域用于在主区域中找不到翻译时。事件分配器用于分配丢失的翻译事件。
现在我们有了实例,但它不知道从哪里获取翻译。让我们告诉它
// Default category is used when no category is specified explicitly. $defaultCategoryName = 'app'; $pathToTranslations = './messages/'; // We use MessageSource that is based on PHP files. $messageSource = new \Yiisoft\Translator\Message\Php\MessageSource($pathToTranslations); // We use Intl message formatter. $formatter = new \Yiisoft\Translator\IntlMessageFormatter(); // Now get an instance of CategorySource. $category = new Yiisoft\Translator\CategorySource( $defaultCategoryName, $messageSource, $formatter ); // And add it. $translator->addCategorySources($category);
就是这样。翻译器已准备好使用。
为Yii3应用程序的高级配置
安装包后,您将在应用程序的配置中找到以下配置文件
config/packages/yiisoft/translator/common.php
config/packages/yiisoft/translator/params.php
您需要获取MessageReader
和MessageSource
的实现以完成配置。请参阅“附加包”,“消息源”以上。
以下配置是在所有必需的包安装后为Yii3应用程序设置的
您需要在common.php
和params.php
文件中取消注释ApplicationCategorySource
周围的字符串
<?php declare(strict_types=1); use Psr\EventDispatcher\EventDispatcherInterface; use Yiisoft\Definitions\Reference; use Yiisoft\Translator\TranslatorInterface; use Yiisoft\Translator\Translator; use Yiisoft\Translator\CategorySource; /** @var array $params */ return [ // Configure application CategorySource ApplicationCategorySource::class => [ // <- Uncommented 'class' => CategorySource::class, '__construct()' => [ 'name' => $params['yiisoft/translator']['defaultCategory'], ], ], TranslatorInterface::class => [ 'class' => Translator::class, '__construct()' => [ $params['yiisoft/translator']['locale'], $params['yiisoft/translator']['fallbackLocale'], Reference::to(EventDispatcherInterface::class), ], 'addCategorySources()' => [ $params['yiisoft/translator']['categorySources'] ], ], ];
和params.php
<?php declare(strict_types=1); use Yiisoft\Definitions\Reference; return [ 'yiisoft/translator' => [ 'locale' => 'en-US', 'fallbackLocale' => null, 'defaultCategory' => 'app', 'categorySources' => [ // You can add categories to your application and your modules using `Reference::to` below Reference::to(ApplicationCategorySource::class), // <- Uncommented // Reference::to(MyModuleCategorySource::class), ], ], ];
多个翻译源
/** @var \Yiisoft\Translator\TranslatorInterface $translator */ $categoryName = 'module'; $pathToModuleTranslations = './module/messages/'; $moduleMessageSource = new \Yiisoft\Translator\Message\Php\MessageSource($pathToModuleTranslations); // Simple message formatter. $formatter = new \Yiisoft\Translator\Formatter\Simple\SimpleMessageFormatter(); $additionalCategory = new Yiisoft\Translator\CategorySource( $categoryName, $moduleMessageSource, $formatter ); $translator->addCategorySources($additionalCategory);
一次添加多个分类源
/** * @var \Yiisoft\Translator\TranslatorInterface $translator * @var \Yiisoft\Translator\CategorySource $additionalCategory1 * @var \Yiisoft\Translator\CategorySource $additionalCategory2 */ $translator->addCategorySources($additionalCategory1, $additionalCategory2);
覆盖翻译消息
如果你使用了一个具有消息翻译的模块并且想要重定义默认的翻译消息,你可以添加你的类别源,使用与模块中相同的 categoryName
。
在翻译过程中,从后往前使用 CategorySource
,允许覆盖相同类别和ID的消息。
/** @var \Yiisoft\Translator\TranslatorInterface $translator */ /** @var \Yiisoft\Translator\Message\Php\MessageSource $yourCustomMessageSource */ /** @var \Yiisoft\Translator\Formatter\Simple\SimpleMessageFormatter $formatter */ // CategorySource for module with "validator" category name. $categoryNameAsModule = 'validator'; // $moduleCategorySource = new Yiisoft\Translator\CategorySource( $categoryNameAsModule, $yourCustomMessageSource, $formatter ); // Needs be added after module category source is added. $translator->addCategorySources($moduleCategorySource);
通用用法
使用默认语言和默认类别
// single translation $messageIdentificator = 'submit'; echo $translator->translate($messageIdentificator); // output: `Submit message` // translation with plural $messageIdentificator = 'multiHumans'; echo $translator->translate($messageIdentificator, ['n' => 3]); // output: `3 humans`
指定类别和语言
$messageIdentificator = 'submit'; echo $translator->translate($messageIdentificator, [], 'moduleId', 'ru'); // output: `Отправить сообщение`
更改默认区域设置
$newDefaultLocale = 'de-DE'; $translator->setLocale($newDefaultLocale);
获取当前区域设置,如果不知道设置区域设置
echo $translator->getLocale();
获取一个新的默认使用该区域设置的Translator实例,如果未明确指定区域设置
$newDefaultLocale = 'de-DE'; echo $translator->withLocale($newDefaultLocale);
获取一个新的默认使用该类别设置的Translator实例,如果未明确指定类别
$newDefaultCategoryId = 'module2'; echo $translator->withDefaultCategory($newDefaultCategoryId);
更多信息
该软件包包含用于开发自定义格式化程序、读取器和编写器的接口。
文档
如果你需要帮助或有问题,Yii 论坛 是一个好地方。你也可以查看其他 Yii 社区资源。
许可证
Yii 消息翻译是自由软件。它根据 BSD 许可证发布。请参阅 LICENSE
了解更多信息。
由 Yii 软件 维护。