Skip to content

Commit d9c4d6c

Browse files
Oscar OteroRaphaël Droz
andauthored
Refactored Twig scanner (#1)
Refactored Twig scanner Co-authored-by: Raphaël Droz <raphael.droz+floss@gmail.com>
1 parent 1ac8f82 commit d9c4d6c

File tree

10 files changed

+301
-646
lines changed

10 files changed

+301
-646
lines changed

.php_cs.dist

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
return My\PhpCsFixerConfig::create()
4+
->setFinder(
5+
PhpCsFixer\Finder::create()
6+
->files()
7+
->name('*.php')
8+
->in(__DIR__.'/src')
9+
->in(__DIR__.'/tests')
10+
->exclude('assets')
11+
);

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
],
1515
"require": {
1616
"php": "^7.2",
17-
"gettext/gettext": "^5.2.0"
17+
"gettext/gettext": "dev-feature/functions-handlers-trait"
1818
},
1919
"require-dev": {
2020
"phpunit/phpunit": "^8.0",
2121
"squizlabs/php_codesniffer": "^3.0",
2222
"oscarotero/php-cs-fixer-config": "^1.0",
2323
"friendsofphp/php-cs-fixer": "^2.15",
24-
"timber/timber": "^1.8"
24+
"timber/timber": "dev-master"
2525
},
2626
"autoload": {
2727
"psr-4": {

src/ParsedFunction2.php

Lines changed: 0 additions & 84 deletions
This file was deleted.

src/TimberScanner.php

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
use Gettext\Translation;
16+
use Timber\Twig;
17+
use Twig\Environment;
18+
19+
/**
20+
* Class to scan Twig files and get gettext translations
21+
*/
22+
class TimberScanner extends TwigScanner
23+
{
24+
protected $functions = [
25+
'__' => 'text',
26+
'_e' => 'text',
27+
'_x' => 'text_context',
28+
'_ex' => 'text_context',
29+
'_n' => 'single_plural_number',
30+
'_nx' => 'single_plural_number_context',
31+
'_n_noop' => 'single_plural',
32+
'_nx_noop' => 'single_plural_context',
33+
];
34+
35+
/**
36+
* Returns a new Twig environment i18n-enabled with Timber.
37+
*/
38+
public static function createTwig(): Environment
39+
{
40+
$twig = parent::createTwig();
41+
42+
$timber = new Twig();
43+
$timber->add_timber_functions($twig);
44+
$timber->add_timber_filters($twig);
45+
46+
return $twig;
47+
}
48+
49+
protected function text(ParsedFunction $function): ?Translation
50+
{
51+
list($original, $domain) = array_pad($function->getArguments(), 2, null);
52+
53+
return $this->addComments(
54+
$function,
55+
$this->saveTranslation($domain, null, $original)
56+
);
57+
}
58+
59+
protected function text_context(ParsedFunction $function): ?Translation
60+
{
61+
list($original, $context, $domain) = array_pad($function->getArguments(), 3, null);
62+
63+
return $this->addComments(
64+
$function,
65+
$this->saveTranslation($domain, $context, $original)
66+
);
67+
}
68+
69+
protected function single_plural_number(ParsedFunction $function): ?Translation
70+
{
71+
list($original, $plural, $number, $domain) = array_pad($function->getArguments(), 4, null);
72+
73+
return $this->addComments(
74+
$function,
75+
$this->saveTranslation($domain, null, $original, $plural)
76+
);
77+
}
78+
79+
protected function single_plural_number_context(ParsedFunction $function): ?Translation
80+
{
81+
list($original, $plural, $number, $context, $domain) = array_pad($function->getArguments(), 5, null);
82+
83+
return $this->addComments(
84+
$function,
85+
$this->saveTranslation($domain, $context, $original, $plural)
86+
);
87+
}
88+
89+
protected function single_plural(ParsedFunction $function): ?Translation
90+
{
91+
list($original, $plural, $domain) = array_pad($function->getArguments(), 3, null);
92+
93+
return $this->addComments(
94+
$function,
95+
$this->saveTranslation($domain, null, $original, $plural)
96+
);
97+
}
98+
99+
protected function single_plural_context(ParsedFunction $function): ?Translation
100+
{
101+
list($original, $plural, $context, $domain) = array_pad($function->getArguments(), 4, null);
102+
103+
return $this->addComments(
104+
$function,
105+
$this->saveTranslation($domain, $context, $original, $plural)
106+
);
107+
}
108+
}

0 commit comments

Comments
 (0)