Skip to content

Commit 1ac8f82

Browse files
author
Raphaël Droz
committed
initial commit
0 parents  commit 1ac8f82

18 files changed

+1721
-0
lines changed

.gitattributes

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/tests/ export-ignore
2+
/phpunit.xml export-ignore
3+
.* export-ignore
4+
phpcs.xml export-ignore
5+
6+
# Set the line ending configuration
7+
* text=lf

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
vendor
2+
composer.lock
3+
*.cache
4+
5+
# Eclipse-specific files
6+
/.settings/
7+
/.buildpath
8+
/.project
9+
10+
# PhpStorm-specific files
11+
/.idea/

.travis.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
language: php
2+
sudo: false
3+
4+
php:
5+
- 7.2
6+
- 7.3
7+
8+
before_install:
9+
- composer install
10+
11+
script:
12+
- composer test

CHANGELOG.md

Whitespace-only changes.

LICENSE

Lines changed: 674 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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

composer.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "drzraf/twig-scanner",
3+
"type": "library",
4+
"description": "Twig scanner for gettext",
5+
"keywords": ["twig", "gettext", "i18n", "translation", "scanner"],
6+
"homepage": "https://github.com/drzraf/twig-scanner",
7+
"license": "GPL-3.0-or-later",
8+
"authors": [
9+
{
10+
"name": "Raphaël Droz",
11+
"email": "raphael.droz@gmail.com",
12+
"role": "Developer"
13+
}
14+
],
15+
"require": {
16+
"php": "^7.2",
17+
"gettext/gettext": "^5.2.0"
18+
},
19+
"require-dev": {
20+
"phpunit/phpunit": "^8.0",
21+
"squizlabs/php_codesniffer": "^3.0",
22+
"oscarotero/php-cs-fixer-config": "^1.0",
23+
"friendsofphp/php-cs-fixer": "^2.15",
24+
"timber/timber": "^1.8"
25+
},
26+
"autoload": {
27+
"psr-4": {
28+
"Gettext\\Scanner\\": "src"
29+
}
30+
},
31+
"autoload-dev": {
32+
"psr-4": {
33+
"Gettext\\Tests\\": "tests"
34+
}
35+
},
36+
"scripts": {
37+
"test": [
38+
"phpunit",
39+
"phpcs"
40+
],
41+
"cs-fix": "php-cs-fixer fix"
42+
}
43+
}

phpcs.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="Gettext coding standard">
3+
<description>Gettext coding standard</description>
4+
5+
<!-- display progress -->
6+
<arg value="p"/>
7+
<arg name="colors"/>
8+
9+
<!-- coding standard -->
10+
<rule ref="PSR2"/>
11+
12+
<!-- Paths to check -->
13+
<file>src</file>
14+
</ruleset>

phpunit.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<phpunit bootstrap="./vendor/autoload.php">
2+
<testsuites>
3+
<testsuite name="All tests">
4+
<directory>./tests/</directory>
5+
</testsuite>
6+
</testsuites>
7+
<filter>
8+
<whitelist processUncoveredFilesFromWhitelist="true">
9+
<directory suffix=".php">src</directory>
10+
</whitelist>
11+
</filter>
12+
</phpunit>

src/ParsedFunction2.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
/**
4+
* Copyright (C) 2018-2020 raphael.droz@gmail.com
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*/
10+
11+
declare(strict_types = 1);
12+
13+
namespace Gettext\Scanner;
14+
15+
/**
16+
* ToDo: Don't make Gettext\Scanner\ParsedFunction final
17+
*/
18+
class ParsedFunction2 extends ParsedFunction
19+
{
20+
const ALLOWED_PROTOTYPES = [
21+
'gettext',
22+
'dgettext',
23+
'pgettext',
24+
'dpgettext',
25+
'ngettext',
26+
'dngettext',
27+
'npgettext',
28+
'dnpgettext',
29+
'ngettext',
30+
'dngettext',
31+
'npgettext',
32+
'dnpgettext',
33+
];
34+
35+
private $prototype = null;
36+
37+
/**
38+
* In order for TwigScanner to inherit CodeScanner.php, the mappings must be self-contained within
39+
* the CodeScanner::$functions property.
40+
*
41+
* Storing the official gettext function allows TwigFunctionScanner...
42+
* 1. To keep track of the original function name (not hijacking of the $name property)
43+
* 2. To avoid hardcoding wp-specific function.
44+
* 3. To store the prototype in order to obtain their handlers.
45+
*/
46+
public function setPrototype(?string $prototype)
47+
{
48+
if ($prototype && !in_array($prototype, self::ALLOWED_PROTOTYPES, true)) {
49+
throw new Exception('Not a valid gettext function');
50+
}
51+
$this->prototype = $prototype;
52+
return $this;
53+
}
54+
55+
public function getPrototype() : ?string
56+
{
57+
return $this->prototype;
58+
}
59+
60+
public function hasDomain() : bool
61+
{
62+
return $this->prototype && substr($this->prototype, 0, 1) === 'd';
63+
}
64+
65+
/**
66+
* Transform prototype (eg: 'gettext') to its domain-counterpart (eg 'dgettext').
67+
*/
68+
public function setDomain()
69+
{
70+
if ($this->prototype) {
71+
$this->prototype = 'd' . ltrim($this->prototype, 'd');
72+
}
73+
return $this;
74+
}
75+
76+
/**
77+
* The opposite of setDomain()
78+
*/
79+
public function unsetDomain()
80+
{
81+
$this->prototype = ltrim($this->prototype, 'd');
82+
return $this;
83+
}
84+
}

0 commit comments

Comments
 (0)