定制 yiisoft/translator-extractor 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

yiisoft/translator-extractor

Composer 安装命令:

composer require --dev yiisoft/translator-extractor

包简介

Yii message extractor

README 文档

README

Yii

Yii Translator 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 8.0 or higher.

Installation

The package could be installed with Composer:

composer require yiisoft/translator-extractor

Configuration

You need configure MessageReader and MessageWriter in config file of the package, config/console/translator-extractor.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()' => [
            [
                DynamicReference::to([
                    'class' => ExtractorCategorySource::class,
                    '__construct()' => [
                        'app',
                        'messageReader' => DynamicReference::to(static fn () => new MessageSource($params['yiisoft/translator-extractor']['messagePath'])),
                        'messageWriter' => DynamicReference::to(static fn () => new MessageSource($params['yiisoft/translator-extractor']['messagePath'])),
                    ],
                ]),
            ],
        ],
        '->translate' // optional, default value for Translation call to look for.
    ],
];

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()' => [
            [
                DynamicReference::to([
                    'class' => ExtractorCategorySource::class,
                    '__construct()' => [
                        'app',
                        '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

./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:

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

Notice: By default extractor has vendor directory in the application directory excluded. To include it you can specify empty value for except:

./yii translator/extract /path/to/your/project --except=''

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:

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

Or in short format:

./yii translator/extract -Lru

Specify default category

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

./yii translator/extract --category=your_category_name

Using except option

To exclude all directories named dir1 use --except:

./yii translator/extract --except=**/dir1/**

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

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

Using only option

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

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

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

./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

Documentation

If you need help or have a question, the Yii Forum is a good place for that. You may also check out other Yii Community Resources.

License

The Yii Translator Message 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

yiisoft/translator-extractor 适用场景与选型建议

yiisoft/translator-extractor 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 68.46k 次下载、GitHub Stars 达 14, 最近一次更新时间为 2021 年 04 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「i18n」 「internationalization」 「dev」 「extractor」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 yiisoft/translator-extractor 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 yiisoft/translator-extractor 我们能提供哪些服务?
定制开发 / 二次开发

基于 yiisoft/translator-extractor 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 68.46k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 14
  • 点击次数: 28
  • 依赖项目数: 7
  • 推荐数: 0

GitHub 信息

  • Stars: 14
  • Watchers: 12
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2021-04-14