|
| 1 | +# Twig Scanner |
| 2 | + |
| 3 | +[![Latest Version on Packagist][ico-version]][link-packagist] |
| 4 | +[![Software License][ico-license]](LICENSE) |
| 5 | +[![Build Status][ico-travis]][link-travis] |
| 6 | +[![Quality Score][ico-scrutinizer]][link-scrutinizer] |
| 7 | +[![Total Downloads][ico-downloads]][link-downloads] |
| 8 | + |
| 9 | +Created by Raphaël Droz <raphael.droz@gmail.com> (GPL-3 License) |
| 10 | + |
| 11 | +Twig code scanner to use with [gettext/gettext](https://github.com/php-gettext/Gettext) |
| 12 | + |
| 13 | +## Warning |
| 14 | + |
| 15 | +In order to be usable, upstream ParsedFunction class at php-gettext/Gettext must be slightly modified to ease inheritance. |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +``` |
| 20 | +composer require gettext/twig-scanner |
| 21 | +``` |
| 22 | + |
| 23 | +## About dependencies |
| 24 | + |
| 25 | +We do not require a **specific** version of Twig. |
| 26 | + |
| 27 | +ToDo: Support symfony/twig-bridge `trans` filter. |
| 28 | + |
| 29 | +## Usage example |
| 30 | + |
| 31 | +```php |
| 32 | +use Gettext\Scanner\TwigScanner; |
| 33 | +use Gettext\Generator\PoGenerator; |
| 34 | +use Gettext\Translations; |
| 35 | + |
| 36 | +//Create a new scanner, adding a translation for each domain we want to get: |
| 37 | +$twigScanner = new TwigScanner( |
| 38 | + Translations::create('domain1'), |
| 39 | + Translations::create('domain2'), |
| 40 | + Translations::create('domain3') |
| 41 | +); |
| 42 | + |
| 43 | +//Set a default domain, so any translations with no domain specified, will be added to that domain |
| 44 | +$twigScanner->setDefaultDomain('domain1'); |
| 45 | + |
| 46 | +//Extract all comments starting with 'notes:' |
| 47 | +$twigScanner->extractCommentsStartingWith('notes:'); |
| 48 | + |
| 49 | +//Scan files |
| 50 | +foreach (glob('*.twig') as $file) { |
| 51 | + $twigScanner->scanFile($file); |
| 52 | +} |
| 53 | + |
| 54 | +//Save the translations in .po files |
| 55 | +$generator = new PoGenerator(); |
| 56 | + |
| 57 | +foreach ($twigScanner->getTranslations() as $domain => $translations) { |
| 58 | + $generator->generateFile($translations, "locales/{$domain}.po"); |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +--- |
| 63 | + |
| 64 | +Please see [CHANGELOG](CHANGELOG.md) for more information about recent changes. |
| 65 | + |
| 66 | +The GPL-3 License (GPL-3). Please see [LICENSE](LICENSE) for more information. |
| 67 | + |
| 68 | +[ico-version]: https://img.shields.io/packagist/v/drzraf/twig-scanner.svg?style=flat-square |
| 69 | +[ico-license]: https://img.shields.io/badge/license-GPLv3-brightgreen.svg?style=flat-square |
| 70 | +[ico-travis]: https://img.shields.io/travis/drzraf/Twig-Scanner/master.svg?style=flat-square |
| 71 | +[ico-scrutinizer]: https://img.shields.io/scrutinizer/g/drzraf/Twig-Scanner.svg?style=flat-square |
| 72 | +[ico-downloads]: https://img.shields.io/packagist/dt/drzraf/twig-scanner.svg?style=flat-square |
| 73 | + |
| 74 | +[link-packagist]: https://packagist.org/packages/drzraf/twig-scanner |
| 75 | +[link-travis]: https://travis-ci.org/drzraf/Twig-Scanner |
| 76 | +[link-scrutinizer]: https://scrutinizer-ci.com/g/drzraf/Twig-Scanner |
| 77 | +[link-downloads]: https://packagist.org/packages/drzraf/twig-scanner |
0 commit comments