yiisoft/translator-extractor

Yii message extractor

dev-master / 3.0.x-dev 2021-11-26 19:52 UTC

This package is auto-updated.

Last update: 2021-11-26 19:52:43 UTC


README

68747470733a2f2f796969736f66742e6769746875622e696f2f646f63732f696d616765732f7969695f6c6f676f2e737667

Yii Message Extractor

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

The package allows automatically extracting translation IDs from PHP source files and writing them to one of the translator message sources.

Requirements

  • PHP 7.4 or higher.

Installation

The package could be installed with composer:

composer require yiisoft/translator-extractor --prefer-dist

Configuration

You need configure MessageReader and MessageWriter in config file of the package:

config/packages/yiisoft/translator-extractor/console.php

For example, when using PHP MessageSource the config will be the following using relative path:

use \Yiisoft\Translator\Message\Php\MessageSource;

return [
    Extractor::class => [
        '__construct()' => [
            'messageReader' => DynamicReference::to(static fn () => new MessageSource($params['yiisoft/translator-extractor']['messagePath'])),
            'messageWriter' => DynamicReference::to(static fn () => new MessageSource($params['yiisoft/translator-extractor']['messagePath'])),
        ],
    ],
];

And in params.php file you can configure parameters of a message source:

return [
    'yiisoft/yii-console' => [
        'commands' => [
            'translator/extract' => ExtractCommand::class,
        ],
    ],
    'yiisoft/translator-extractor' => [
        // Using relative path:
        'messagePath' => dirname(__DIR__, 5) . '/messages',
    ],
];

Or if with using PHP MessageSource the config will be the following using Aliases:

use \Yiisoft\Translator\Message\Php\MessageSource;

return [
    Extractor::class => [
        '__construct()' => [
            'messageReader' => DynamicReference::to(static fn (Aliases $aliases) => new MessageSource($aliases->get('@message'))),
            'messageWriter' => DynamicReference::to(static fn (Aliases $aliases) => new MessageSource($aliases->get('@message'))),
        ],
    ],
];

Attention: Both MessageReader and MessageWriter should be configured for using the same MessageSource. The extractor needs it to work with existing messages.

General usage

php yii translator/extract

This command will recursively find all messages in the code starting with the current directory and will save it into a message source for default language en. You can specify the path explicitly:

php yii translator/extract /path/to/your/project

Full list of options:

Usage:
  translator/extract [options] [--] [<path>]

Arguments:
  path                       Path for extracting message IDs.

Options:
  -L, --languages=LANGUAGES  Comma separated list of languages to write message sources for. By default it is `en`. [default: "en"]
  -C, --category=CATEGORY    Default message category to use when category is not set. [default: "app"]
  -E, --except[=EXCEPT]      Exclude path from extracting. (multiple values allowed)
  -O, --only[=ONLY]          Use the only specified path for extracting. (multiple values allowed)

Specify languages

You can specify multiple languages to write IDs into:

php yii translator/extract --languages=en,ru

Or in short format:

php yii translator/extract -Lru

Specify default category

Also, you can specify default message category to use when category is not set.

php yii translator/extract --category=your_category_name

Using except option

To exclude vendor directory use --except:

php yii translator/extract --except=**/vendor/**

To exclude both vendor and tests directories the following options could be used:

php yii translator/extract --except=**/vendor/** --except=**/tests/**

Using only option

To parse only test.php files in any directory use --only option:

php yii translator/extract --only=**/test.php

To parse only /var/www/html/test.php file use:

php yii translator/extract --only=/var/www/html/test.php

For more info about except and only parameters check documentation of yiisoft/files package.

Working with gettext

The package currently does not support extracting messages into gettext format. To extract messages for gettext, you may use the following shell script (in Linux-based OS):

find src/ -name *.php | xargs xgettext --from-code=utf-8 --language=PHP --no-location --omit-header --sort-output --keyword=translate --output="locales/category.pot"

for d in locales/*/ ; do
    for i in locales/*.pot; do
        if [ ! -f "$d$(basename "$i" .pot).po" ]; then
            touch "$d$(basename "$i" .pot).po"
        fi

        msgmerge --update --silent --backup=off "$d$(basename "$i" .pot).po" $i
    done
done

Testing

Unit testing

The package is tested with PHPUnit. To run tests:

./vendor/bin/phpunit

Mutation testing

The package tests are checked with Infection mutation framework with Infection Static Analysis Plugin. To run it:

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

Static analysis

The code is statically analyzed with Psalm. To run static analysis:

./vendor/bin/psalm

License

The Yii translator extractor is free software. It is released under the terms of the BSD License. Please see LICENSE for more information.

Maintained by Yii Software.

Support the project

Open Collective

Follow updates

Official website Twitter Telegram Facebook Slack