PHPackages                             drabek-digital/gettext-miner - 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. [Localization &amp; i18n](/categories/localization)
4. /
5. drabek-digital/gettext-miner

ActiveLibrary[Localization &amp; i18n](/categories/localization)

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.

v1.0(1y ago)11MITPHPPHP &gt;=8.3CI failing

Since Mar 31Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/DrabekDigital/gettext-miner)[ Packagist](https://packagist.org/packages/drabek-digital/gettext-miner)[ Docs](https://drabek.digital/Drabek-digital/gettext-miner)[ RSS](/packages/drabek-digital-gettext-miner/feed)WikiDiscussions main Synced 3w ago

READMEChangelog (1)Dependencies (11)Versions (2)Used By (0)

Gettext Miner
=============

[](#gettext-miner)

**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
-------------

[](#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
-----

[](#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
```

2. Configure the tool in the root of your project in `.gettext-miner.neon`, see section above
3. Run the tool with the following command:

```
$ gettext-miner extract project-dir/
```

4. Profit!

Output formatters
-----------------

[](#output-formatters)

### Gettext

[](#gettext)

Use this formatter when you want to generate a gettext template file to be translated in stardard gettext tools, such as [Poedit](https://poedit.net/).

```
    # ...
    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

[](#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:

```
