Skip to content

Commit 68278bd

Browse files
committed
Allow constants in gettext functions #116
1 parent e5d8f1e commit 68278bd

File tree

15 files changed

+44
-20
lines changed

15 files changed

+44
-20
lines changed

src/Extractors/JsCode.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
class JsCode extends Extractor implements ExtractorInterface
1212
{
1313
public static $options = [
14+
'constants' => [],
15+
1416
'functions' => [
1517
'gettext' => 'gettext',
1618
'__' => 'gettext',

src/Extractors/PhpCode.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class PhpCode extends Extractor implements ExtractorInterface
1616
// - non-empty string: to extract comments that start with that string
1717
'extractComments' => false,
1818

19+
'constants' => [],
20+
1921
'functions' => [
2022
'gettext' => 'gettext',
2123
'__' => 'gettext',

src/Utils/FunctionsScanner.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ abstract class FunctionsScanner
1010
/**
1111
* Scan and returns the functions and the arguments.
1212
*
13+
* @param array $constants Constants used in the code to replace
14+
*
1315
* @return array
1416
*/
15-
abstract public function getFunctions();
17+
abstract public function getFunctions(array $constants = []);
1618

1719
/**
1820
* Search for specific functions and create translations.
@@ -25,7 +27,7 @@ public function saveGettextFunctions(Translations $translations, array $options)
2527
$functions = $options['functions'];
2628
$file = $options['file'];
2729

28-
foreach ($this->getFunctions() as $function) {
30+
foreach ($this->getFunctions($options['constants']) as $function) {
2931
list($name, $line, $args) = $function;
3032

3133
if (!isset($functions[$name])) {

src/Utils/JsFunctionsScanner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function __construct($code)
2020
/**
2121
* {@inheritdoc}
2222
*/
23-
public function getFunctions()
23+
public function getFunctions(array $constants = [])
2424
{
2525
$length = strlen($this->code);
2626
$line = 1;

src/Utils/PhpFunctionsScanner.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function ($token) {
5858
/**
5959
* {@inheritdoc}
6060
*/
61-
public function getFunctions()
61+
public function getFunctions(array $constants = [])
6262
{
6363
$count = count($this->tokens);
6464
$bufferFunctions = [];
@@ -95,8 +95,13 @@ public function getFunctions()
9595
$bufferFunctions[0]->addArgumentChunk(PhpCode::convertString($value[1]));
9696
}
9797
break;
98+
9899
case T_STRING:
99100
if (isset($bufferFunctions[0])) {
101+
if (isset($constants[$value[1]])) {
102+
$bufferFunctions[0]->addArgumentChunk($constants[$value[1]]);
103+
break;
104+
}
100105
$bufferFunctions[0]->stopArgument();
101106
}
102107
//new function found
@@ -119,6 +124,7 @@ public function getFunctions()
119124
break;
120125
}
121126
break;
127+
122128
case T_COMMENT:
123129
if (isset($bufferFunctions[0])) {
124130
$comment = $this->parsePhpComment($value[1]);
@@ -127,6 +133,7 @@ public function getFunctions()
127133
}
128134
}
129135
break;
136+
130137
default:
131138
if (isset($bufferFunctions[0])) {
132139
$bufferFunctions[0]->stopArgument();

tests/AbstractTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected static function asset($file)
3030
return './tests/assets/'.$file;
3131
}
3232

33-
protected static function get($file, $format = null)
33+
protected static function get($file, $format = null, array $options = [])
3434
{
3535
if ($format === null) {
3636
$format = basename($file);
@@ -39,7 +39,7 @@ protected static function get($file, $format = null)
3939
$method = "from{$format}File";
4040
$file = static::asset($file.'.'.static::$ext[$format]);
4141

42-
return Translations::$method($file);
42+
return Translations::$method($file, $options);
4343
}
4444

4545
protected function assertContent(Translations $translations, $file, $format = null)
@@ -51,7 +51,7 @@ protected function assertContent(Translations $translations, $file, $format = nu
5151
$method = "to{$format}String";
5252
$content = file_get_contents(static::asset($file.'.'.static::$ext[$format]));
5353

54-
$this->assertSame($content, $translations->$method());
54+
$this->assertSame($content, $translations->$method(), $file);
5555
}
5656

5757
protected static function saveContent(Translations $translations, $file, $format = null)

tests/AssetsTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,11 @@ public function testPhpCode()
278278

279279
public function testPhpCode2()
280280
{
281-
$translations = static::get('phpcode2/input', 'PhpCode');
281+
$translations = static::get('phpcode2/input', 'PhpCode', [
282+
'constants' => [
283+
'CONTEXT' => 'my-context'
284+
]
285+
]);
282286
$countTranslations = 9;
283287
$countHeaders = 8;
284288

tests/assets/phpcode2/Csv.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ Report-Msgid-Bugs-To:
1313
,"text concatenated with 'comments'",
1414
,"Stop at the variable",
1515
,"No comments",
16-
,"All comments",
16+
my-context,"All comments",
1717
,"i18n Tagged comment",
1818
,"i18n Tagged comment inside",

tests/assets/phpcode2/Jed.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"No comments": [
2424
""
2525
],
26-
"All comments": [
26+
"my-context\\u0004All comments": [
2727
""
2828
],
2929
"i18n Tagged comment": [

tests/assets/phpcode2/Json.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,17 @@
2424
"No comments": [
2525
""
2626
],
27-
"All comments": [
28-
""
29-
],
3027
"i18n Tagged comment": [
3128
""
3229
],
3330
"i18n Tagged comment inside": [
3431
""
3532
]
33+
},
34+
"my-context": {
35+
"All comments": [
36+
""
37+
]
3638
}
3739
}
3840
}

0 commit comments

Comments
 (0)