PHPackages                             yiisoft/i18n-translator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. yiisoft/i18n-translator

Abandoned → [yiisoft/translator](/?search=yiisoft%2Ftranslator)Library[Framework](/categories/framework)

yiisoft/i18n-translator
=======================

Yii Message Translator

3.2.1(5mo ago)261.6k9[10 issues](https://github.com/yiisoft/translator/issues)[1 PRs](https://github.com/yiisoft/translator/pulls)BSD-3-ClausePHPPHP 8.0 - 8.5CI passing

Since Mar 22Pushed today14 watchersCompare

[ Source](https://github.com/yiisoft/translator)[ Packagist](https://packagist.org/packages/yiisoft/i18n-translator)[ Docs](https://www.yiiframework.com/)[ GitHub Sponsors](https://github.com/sponsors/yiisoft)[ OpenCollective](https://opencollective.com/yiisoft)[ RSS](/packages/yiisoft-i18n-translator/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (10)Dependencies (9)Versions (21)Used By (0)

 [ ![Yii](https://camo.githubusercontent.com/8317c17418b39410a660f5149071d26c5023c0d5fb2b7ebb771324812f666d73/68747470733a2f2f796969736f66742e6769746875622e696f2f646f63732f696d616765732f7969695f6c6f676f2e737667) ](https://github.com/yiisoft)

Yii Message Translator
======================

[](#yii-message-translator)

[![Latest Stable Version](https://camo.githubusercontent.com/87f188e6ca83b8df21153094265d27bb6f90598479c22701799967cff9e85221/68747470733a2f2f706f7365722e707567782e6f72672f796969736f66742f7472616e736c61746f722f76)](https://packagist.org/packages/yiisoft/translator)[![Total Downloads](https://camo.githubusercontent.com/8f51e981eb5dc99e7efa5bd51216e63c440503865d0d9eb954ed93d2911a42cf/68747470733a2f2f706f7365722e707567782e6f72672f796969736f66742f7472616e736c61746f722f646f776e6c6f616473)](https://packagist.org/packages/yiisoft/translator)[![Build status](https://github.com/yiisoft/translator/actions/workflows/build.yml/badge.svg)](https://github.com/yiisoft/translator/actions/workflows/build.yml)[![Code Coverage](https://camo.githubusercontent.com/0662eaff434bc9114c2ead85fb1e44cfe1ff712d32886fc52a316520a8faafba/68747470733a2f2f636f6465636f762e696f2f67682f796969736f66742f7472616e736c61746f722f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/yiisoft/translator)[![Mutation testing badge](https://camo.githubusercontent.com/5bd6304dd3f28e8a7c5a4f47fdb42912f2db5e900536cb57fa40d5986a475e5e/68747470733a2f2f696d672e736869656c64732e696f2f656e64706f696e743f7374796c653d666c61742675726c3d687474707325334125324625324662616467652d6170692e737472796b65722d6d757461746f722e696f2532466769746875622e636f6d253246796969736f66742532467472616e736c61746f722532466d6173746572)](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/translator/master)[![static analysis](https://github.com/yiisoft/translator/workflows/static%20analysis/badge.svg)](https://github.com/yiisoft/translator/actions?query=workflow%3A%22static+analysis%22)[![type-coverage](https://camo.githubusercontent.com/0ee6344614984973a75cbdfd8e4db4fd5c04d7055e85f16ddaf3e6fdd83dc277/68747470733a2f2f73686570686572642e6465762f6769746875622f796969736f66742f7472616e736c61746f722f636f7665726167652e737667)](https://shepherd.dev/github/yiisoft/translator)

This package allows translating messages into several languages. It can work with both Yii-based applications and standalone PHP applications.

Requirements
------------

[](#requirements)

- PHP 8.0 - 8.5.

Installation
------------

[](#installation)

The package could be installed with [Composer](https://getcomposer.org):

```
composer require yiisoft/translator
```

### Additional packages

[](#additional-packages)

There are two types of additional packages. Message source provide support of various message storage formats such as PHP arrays or GNU gettext. Message formatters provide extra syntax that is recognized in translated messages.

#### Message sources

[](#message-sources)

- [translator-message-php](https://github.com/yiisoft/translator-message-php) - PHP file message storage.
- [translator-message-db](https://github.com/yiisoft/translator-message-db) - Database message storage.
- [translator-message-gettext](https://github.com/yiisoft/translator-message-gettext) - gettext message storage.
- `InMemoryMessageSource` - simple in-memory message source that can be used for testing purposes.

Built-in message formatters
---------------------------

[](#built-in-message-formatters)

- [Simple formatter](docs/guide/en/simple-formatter.md) just replaces parameters in messages. Does not take into account the locale.
- [`intl` formatter](docs/guide/en/intl-formatter.md) utilizes PHP intl extension message formatting capabilities.

Extracting messages
-------------------

[](#extracting-messages)

The message extraction is done via [console extractor](https://github.com/yiisoft/translator-extractor) that searches for translator message calls and builds translation files.

In some cases you need to do so without using console. If that is your case, check [extractor guide](docs/guide/en/extractor.md).

Configuration
-------------

[](#configuration)

### Quick start

[](#quick-start)

First, get a configured instance of event dispatcher. When using a framework it is usually done as:

```
public function actionProcess(\Psr\EventDispatcher\EventDispatcherInterface $eventDispatcher)
{
    // ...
}
```

Configuration depends on the container used so below we'll create an instance manually.

```
/** @var \Psr\EventDispatcher\EventDispatcherInterface $eventDispatcher */
$locale = 'ru';
$fallbackLocale = 'en';

$translator = new Yiisoft\Translator\Translator(
    $locale,
    $fallbackLocale,
    $eventDispatcher
);
```

`$fallbackLocale` and `$eventDispatcher` are optional. Fallback locale is used when no translation was found in the main locale. Event dispatcher is used to dispatch missing translation events.

Now we've got an instance, but it has no idea where to get translations from. Let's tell it:

```
// 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);
```

That's it. Translator is ready to be used.

### Advanced configuration for Yii3 application

[](#advanced-configuration-for-yii3-application)

After installing the package, you will get the following configuration files in your application config:

- `config/packages/yiisoft/translator/common.php`
- `config/packages/yiisoft/translator/params.php`

You need get implementation of `MessageReader` and `MessageSource` to complete configuration. See "Additional packages", "Message sources" above.

The following configuration is for Yii3 application after all needed packages installed:

You need uncomment strings around `ApplicationCategorySource` in `common.php` and `params.php` files:

```
