drabek-digital/gettext-miner 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

drabek-digital/gettext-miner

Composer 安装命令:

composer require drabek-digital/gettext-miner

包简介

Gettext Miner is a simple PHP tool for extracting and updating gettext translations from PHP files, Latte templates and other files.

README 文档

README

Gettext Miner is a tool for extracting translatable strings from PHP, Latte, Neon, and other files.

The tool comes bundled with extractors for these formats:

  • PHP using calls like $t->translate('full string') or $t->_('full string') or with pluralization $t->translate('Your basket has %d items!', $count).
  • Latte templates in multiple variants: {_'full string'} and $t->translate('full string').
  • Neon config files with configurable paths to extract.

More over it has comes with extractor tuned for Nette applications and Nette forms which can extract strings from functions internally using translator, such as $form->addText('fullname', 'Your full name').

The extracted strings can be persisted into a gettext template file or a PHP file with an PHP array.

Also, the support for multiple build targets (aka modules) within one project is present.

Configuration

Before usage, you need to configure the tool in project root file named .gettext-miner.neon, see example config below.

# Define particular target
Module1:
    # All directories that should be scanned recursively
    sources:
        - app/Modules/Module1/
        - app/Config/
    # Optional explicit list of files to parse
    files:                     
        - extras/foo.php
    # Extractors configuration (keys are just random identifiers)
    extractors:
        php:
            extractor: PHP
            # Append additional functions and argument position to be extracted
            extraFunctions:
                myTranslate: 1
            # Optional: Replace default configuration with completely custom set of functions
            # functions:
            #    myTranslate: 1
            # Optional: overwrite default extensions
            # extensions:
            # - php
        latte:
            extractor: Latte
            # Optional: Additional modifiers (Latte filters) names or regexes
            extraModifiers:
                - myFilter #ensure that the provided content is properly quoted for usage in preg functions
                - myTruncate[^|]+               # allow filter with parameters up to next filter or to end of Latte tag
                - myTruncate[^|]+\|noescape     # multiple composed filters has to be explicitly allowed
            # Optional: ovewrite default extensions
            # extensions:
            # - latte
        sql:
            extractor: SQL      # wrap string like this for extraction /*_*/'String'/*_*/ 
            # Optional: ovewrite default extensions
            # extensions:
            # - sql
        neon:
            extractor: Neon
            paths:
                app/config/config.neon: # we need to specify neon file path
                    - parameters|myArray # and path to extract strings
            # Optional: ovewrite default extensions
            # extensions:
            # - neon
    # Configure output
    output:
        # Path where to store the gettext template
        destination: app/Modules/Module1/Locales/template.pot
        formatter: Gettext

Supported extractors:

  • PHP alias of DrabekDigital\GettextMiner\Extractors\PHP
  • LegacyLatte alias of DrabekDigital\GettextMiner\Extractors\LegacyLatte
  • Neon alias of DrabekDigital\GettextMiner\Extractors\Neon
  • SQL alias of DrabekDigital\GettextMiner\Extractors\SQL
  • Nette alias of DrabekDigital\GettextMiner\Extractors\Nette

If you wish to implement custom extractor you can do it by implementing DrabekDigital\GettextMiner\Extractors\Extractor interface.

Supported output types:

  • Gettext alias of DrabekDigital\GettextMiner\OutputFormatters\Gettext
  • ArrayFile alias of DrabekDigital\GettextMiner\OutputFormatters\ArrayFile

If you wish to implement custom output format you can do it by implementing DrabekDigital\GettextMiner\OutputFormatters\OutputFormatter interface.

Usage

  1. Either install the tool globally or add it to your project dependencies.
# Global installation
$ composer global require drabek-digital/gettext-miner

# Project installation
$ composer require --dev drabek-digital/gettext-miner
  1. Configure the tool in the root of your project in .gettext-miner.neon, see section above

  2. Run the tool with the following command:

$ gettext-miner extract project-dir/
  1. Profit!

Output formatters

Gettext

Use this formatter when you want to generate a gettext template file to be translated in stardard gettext tools, such as Poedit.

    # ...
    output:
        destination: template.pot
        formatter: Gettext
        extraMeta:
            key: value
        detector: SymfonyTranslationPluralDetector
        lineNumbers: true
    # ...

Will output this:

# Created: 2025-01-03T11:19:36+02:00

msgid ""
msgstr ""
"Content-Type: text/plain; charset=UTF-8\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"key: value\n"

ArrayFile

Use this formatter when you want to generate a PHP file with an array of strings.

    # ...
    output:
        destination: strings.php
        formatter: ArrayFile
        indent: tabs
        outputVariable: translations
    # ...

Will output this:

<?php declare(strict_types = 1);
$translations = [
	'Translated string',
];

Filters usage guidance

The implementation of each filter is up to the extractor. Some filters like PHP are using PHP tokenization, while others like Latte are using regular expressions, which can cause issues with complex and non-standard/simple syntax.

When you find yourself in a situation when in PHP code you need to extract some random string outside a translation function call prepend /*gettext-miner*/ comment just before the string, like const FOO = /*gettext-miner*/ 'enum value label';

Also, it is worth mentioning that in all code ensure that functions are provided with simple non-concatenated strings, like $t->translate('Hello world') and not $t->translate('Hello' . ' world').

Beware that Latte filter only use a reasonable subset of syntax, consult the code to stay on the safe side.

Pluralization detection

Gettext format supports pluralization, however the template needs to be already written with plural forms defined. The tool can detect plural forms in PHP code using the plural detector.

It has to be specified in the configuration:

    # ...
    output:
        destination: template.pot
        formatter: Gettext
        detector: SymfonyTranslationPluralDetector
    # ...

Supported detectors:

  • SprintfPluralDetector alias of DrabekDigital\GettextMiner\Detectors\SprintfPluralDetector, which will detect plural form if %d is present.
  • SymfonyTranslationPluralDetector alias of DrabekDigital\GettextMiner\Detectors\SymfonyTranslationPluralDetector, which will detect plural form if %count% is present.

Other notes

All paths are relative to the project root.

Be careful with paths within the project as for example on Linux it is possible to create directory or file named a\b which however converts into Gettext path reference as a/b which has different meaning and won't work for reference click through. Sadly this cannot be easily detected and fixed.

drabek-digital/gettext-miner 适用场景与选型建议

drabek-digital/gettext-miner 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 03 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 drabek-digital/gettext-miner 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 32
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-05