Download the PHP package drabek-digital/gettext-miner without Composer
On this page you can find all versions of the php package drabek-digital/gettext-miner. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download drabek-digital/gettext-miner
More information about drabek-digital/gettext-miner
Files in drabek-digital/gettext-miner
Package gettext-miner
Short Description Gettext Miner is a simple PHP tool for extracting and updating gettext translations from PHP files, Latte templates and other files.
License MIT
Homepage https://drabek.digital/Drabek-digital/gettext-miner
Informations about the package 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
Before usage, you need to configure the tool in project root file named .gettext-miner.neon, see example config below.
Supported extractors:
PHPalias ofDrabekDigital\GettextMiner\Extractors\PHPLegacyLattealias ofDrabekDigital\GettextMiner\Extractors\LegacyLatteNeonalias ofDrabekDigital\GettextMiner\Extractors\NeonSQLalias ofDrabekDigital\GettextMiner\Extractors\SQLNettealias ofDrabekDigital\GettextMiner\Extractors\Nette
If you wish to implement custom extractor you can do it by implementing DrabekDigital\GettextMiner\Extractors\Extractor interface.
Supported output types:
Gettextalias ofDrabekDigital\GettextMiner\OutputFormatters\GettextArrayFilealias ofDrabekDigital\GettextMiner\OutputFormatters\ArrayFile
If you wish to implement custom output format you can do it by implementing DrabekDigital\GettextMiner\OutputFormatters\OutputFormatter interface.
Usage
-
Either install the tool globally or add it to your project dependencies.
-
Configure the tool in the root of your project in
.gettext-miner.neon, see section above -
Run the tool with the following command:
- 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.
Will output this:
ArrayFile
Use this formatter when you want to generate a PHP file with an array of strings.
Will output this:
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:
Supported detectors:
SprintfPluralDetectoralias ofDrabekDigital\GettextMiner\Detectors\SprintfPluralDetector, which will detect plural form if%dis present.SymfonyTranslationPluralDetectoralias ofDrabekDigital\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.
All versions of gettext-miner with dependencies
symfony/console Version ^6|^7
nette/neon Version ^3
nette/utils Version ^4
nette/finder Version ^3
symfony/event-dispatcher Version ^7