Skip to content

Commit f3cb12e

Browse files
ENGCOM-8304: Fix #11175 - i18n:collect-phrases -m can't find many important magento phrases #30320
2 parents bc61377 + 05f7c87 commit f3cb12e

File tree

3 files changed

+57
-31
lines changed

3 files changed

+57
-31
lines changed

setup/src/Magento/Setup/Module/I18n/Parser/Adapter/Html.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Setup\Module\I18n\Parser\Adapter;
79

10+
use Exception;
811
use Magento\Email\Model\Template\Filter;
912

1013
/**
@@ -16,17 +19,30 @@ class Html extends AbstractAdapter
1619
* Covers
1720
* <span><!-- ko i18n: 'Next'--><!-- /ko --></span>
1821
* <th class="col col-method" data-bind="i18n: 'Select Method'"></th>
22+
* @deprecated Not used anymore because of newly introduced constant
23+
* @see self::HTML_REGEX_LIST
1924
*/
2025
const HTML_FILTER = "/i18n:\s?'(?<value>[^'\\\\]*(?:\\\\.[^'\\\\]*)*)'/i";
2126

27+
private const HTML_REGEX_LIST = [
28+
// <span><!-- ko i18n: 'Next'--><!-- /ko --></span>
29+
// <th class="col col-method" data-bind="i18n: 'Select Method'"></th>
30+
"/i18n:\s?'(?<value>[^'\\\\]*(?:\\\\.[^'\\\\]*)*)'/i",
31+
// <translate args="'System Messages'"/>
32+
// <span translate="'Examples'"></span>
33+
"/translate( args|)=\"'(?<value>[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*)'\"/i"
34+
];
35+
2236
/**
2337
* @inheritdoc
2438
*/
2539
protected function _parse()
2640
{
41+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
2742
$data = file_get_contents($this->_file);
2843
if ($data === false) {
29-
throw new \Exception('Failed to load file from disk.');
44+
// phpcs:ignore Magento2.Exceptions.DirectThrow
45+
throw new Exception('Failed to load file from disk.');
3046
}
3147

3248
$results = [];
@@ -37,15 +53,19 @@ protected function _parse()
3753
if (preg_match(Filter::TRANS_DIRECTIVE_REGEX, $results[$i][2], $directive) !== 1) {
3854
continue;
3955
}
56+
4057
$quote = $directive[1];
4158
$this->_addPhrase($quote . $directive[2] . $quote);
4259
}
4360
}
4461

45-
preg_match_all(self::HTML_FILTER, $data, $results, PREG_SET_ORDER);
46-
for ($i = 0, $count = count($results); $i < $count; $i++) {
47-
if (!empty($results[$i]['value'])) {
48-
$this->_addPhrase($results[$i]['value']);
62+
foreach (self::HTML_REGEX_LIST as $regex) {
63+
preg_match_all($regex, $data, $results, PREG_SET_ORDER);
64+
65+
for ($i = 0, $count = count($results); $i < $count; $i++) {
66+
if (!empty($results[$i]['value'])) {
67+
$this->_addPhrase($results[$i]['value']);
68+
}
4969
}
5070
}
5171
}

setup/src/Magento/Setup/Test/Unit/Module/I18n/Parser/Adapter/HtmlTest.php

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,102 +7,106 @@
77

88
namespace Magento\Setup\Test\Unit\Module\I18n\Parser\Adapter;
99

10-
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1110
use Magento\Setup\Module\I18n\Parser\Adapter\Html;
1211
use PHPUnit\Framework\TestCase;
1312

1413
class HtmlTest extends TestCase
1514
{
1615
/**
17-
* @var string
18-
*/
19-
protected $_testFile;
20-
21-
/**
22-
* @var int
16+
* @var Html
2317
*/
24-
protected $_stringsCount;
18+
private $model;
2519

2620
/**
27-
* @var Html
21+
* @var string
2822
*/
29-
protected $_adapter;
23+
private $testFile;
3024

3125
protected function setUp(): void
3226
{
33-
$this->_testFile = str_replace('\\', '/', realpath(dirname(__FILE__))) . '/_files/email.html';
34-
$this->_stringsCount = count(file($this->_testFile));
35-
36-
$this->_adapter = (new ObjectManager($this))->getObject(Html::class);
27+
$this->testFile = str_replace('\\', '/', realpath(__DIR__)) . '/_files/email.html';
28+
$this->model = new Html();
3729
}
3830

3931
public function testParse()
4032
{
4133
$expectedResult = [
4234
[
4335
'phrase' => 'Phrase 1',
44-
'file' => $this->_testFile,
36+
'file' => $this->testFile,
4537
'line' => '',
4638
'quote' => '\'',
4739
],
4840
[
4941
'phrase' => 'Phrase 2 with %a_lot of extra info for the brilliant %customer_name.',
50-
'file' => $this->_testFile,
42+
'file' => $this->testFile,
5143
'line' => '',
5244
'quote' => '"',
5345
],
5446
[
5547
'phrase' => 'This is test data',
56-
'file' => $this->_testFile,
48+
'file' => $this->testFile,
5749
'line' => '',
5850
'quote' => '',
5951
],
6052
[
6153
'phrase' => 'This is test data at right side of attr',
62-
'file' => $this->_testFile,
54+
'file' => $this->testFile,
6355
'line' => '',
6456
'quote' => '',
6557
],
6658
[
6759
'phrase' => 'This is \\\' test \\\' data',
68-
'file' => $this->_testFile,
60+
'file' => $this->testFile,
6961
'line' => '',
7062
'quote' => '',
7163
],
7264
[
7365
'phrase' => 'This is \\" test \\" data',
74-
'file' => $this->_testFile,
66+
'file' => $this->testFile,
7567
'line' => '',
7668
'quote' => '',
7769
],
7870
[
7971
'phrase' => 'This is test data with a quote after',
80-
'file' => $this->_testFile,
72+
'file' => $this->testFile,
8173
'line' => '',
8274
'quote' => '',
8375
],
8476
[
8577
'phrase' => 'This is test data with space after ',
86-
'file' => $this->_testFile,
78+
'file' => $this->testFile,
8779
'line' => '',
8880
'quote' => '',
8981
],
9082
[
9183
'phrase' => '\\\'',
92-
'file' => $this->_testFile,
84+
'file' => $this->testFile,
9385
'line' => '',
9486
'quote' => '',
9587
],
9688
[
9789
'phrase' => '\\\\\\\\ ',
98-
'file' => $this->_testFile,
90+
'file' => $this->testFile,
91+
'line' => '',
92+
'quote' => '',
93+
],
94+
[
95+
'phrase' => 'This is test content in translate tag',
96+
'file' => $this->testFile,
97+
'line' => '',
98+
'quote' => '',
99+
],
100+
[
101+
'phrase' => 'This is test content in translate attribute',
102+
'file' => $this->testFile,
99103
'line' => '',
100104
'quote' => '',
101105
],
102106
];
103107

104-
$this->_adapter->parse($this->_testFile);
108+
$this->model->parse($this->testFile);
105109

106-
$this->assertEquals($expectedResult, $this->_adapter->getPhrases());
110+
$this->assertEquals($expectedResult, $this->model->getPhrases());
107111
}
108112
}

setup/src/Magento/Setup/Test/Unit/Module/I18n/Parser/Adapter/_files/email.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,7 @@
2929
<label data-bind="i18n: ''"></label>
3030
<label data-bind="i18n: '\''"></label>
3131
<label data-bind="i18n: '\\\\ '"></label>
32+
<span><translate args="'This is test content in translate tag'" /></span>
33+
<span translate="'This is test content in translate attribute'"></span>
3234
</body>
3335
</html>

0 commit comments

Comments
 (0)