Skip to content

Commit b5f881a

Browse files
committed
Merge remote-tracking branch 'origin/AC-9843-collect-phrases-issue' into spartans_pr_12062024
2 parents eca1758 + 5d2cf66 commit b5f881a

File tree

5 files changed

+76
-27
lines changed

5 files changed

+76
-27
lines changed

dev/tests/integration/testsuite/Magento/Setup/Module/I18n/Parser/Adapter/JsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ public function testParse()
3838
[
3939
'phrase' => 'text single quote',
4040
'file' => $file,
41-
'line' => 2,
41+
'line' => 1,
4242
'quote' => '\''
4343
],
4444
[
4545
'phrase' => 'text "some',
4646
'file' => $file,
47-
'line' => 3,
47+
'line' => 1,
4848
'quote' => '\''
4949
]
5050
];

dev/tests/static/testsuite/Magento/Test/Integrity/App/Language/TranslationFilesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
class TranslationFilesTest extends TranslationFiles
1818
{
1919
/**
20-
* Context
20+
* I18n\Context
2121
*
2222
* @var \Magento\Setup\Module\I18n\Context
2323
*/

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

Lines changed: 66 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,29 @@
55
*/
66
namespace Magento\Setup\Module\I18n\Parser\Adapter;
77

8+
use Magento\Framework\Exception\FileSystemException;
9+
use Magento\Framework\Filesystem\Driver\File;
10+
811
/**
912
* Js parser adapter
1013
*/
14+
1115
class Js extends AbstractAdapter
1216
{
17+
/**
18+
* @var \Magento\Framework\Filesystem\Driver\File
19+
*/
20+
protected $_filesystem;
21+
22+
/**
23+
* Adapter construct
24+
*
25+
* @param \Magento\Framework\Filesystem\Driver\File $fileSystem
26+
*/
27+
public function __construct()
28+
{
29+
$this->_filesystem = new \Magento\Framework\Filesystem\Driver\File();
30+
}
1331
/**
1432
* Covers
1533
* $.mage.__('Example text')
@@ -32,27 +50,58 @@ class Js extends AbstractAdapter
3250
*/
3351
protected function _parse()
3452
{
35-
$fileHandle = @fopen($this->_file, 'r');
53+
$fileHandle = $this->_filesystem->fileOpen($this->_file, 'r');
3654
$lineNumber = 0;
37-
while (!feof($fileHandle)) {
38-
$lineNumber++;
39-
$fileRow = fgets($fileHandle, 4096);
40-
$results = [];
41-
$regexes = [
42-
static::REGEX_MAGE_TRANSLATE,
43-
static::REGEX_TRANSLATE_FUNCTION
44-
];
45-
46-
foreach ($regexes as $regex) {
47-
preg_match_all($regex, $fileRow, $results, PREG_SET_ORDER);
48-
for ($i = 0, $count = count($results); $i < $count; $i++) {
49-
if (isset($results[$i][2])) {
50-
$quote = $results[$i][1];
51-
$this->_addPhrase($quote . $results[$i][2] . $quote, $lineNumber);
55+
try {
56+
while (($line = $this->fileReadLine($fileHandle, 0)) !== false) {
57+
$lineNumber++;
58+
$fileRow = preg_replace('/"\s+\+"|"\s+\+\s+\"|"\+\s+\"|"\+"/', '', $line);
59+
$fileRow = preg_replace("/'\s+\+'|'\s+\+\s+\'|'\+\s+\'|'\+'/", "", $fileRow);
60+
$results = [];
61+
$regexes = [
62+
static::REGEX_MAGE_TRANSLATE,
63+
static::REGEX_TRANSLATE_FUNCTION
64+
];
65+
66+
foreach ($regexes as $regex) {
67+
preg_match_all($regex, $fileRow, $results, PREG_SET_ORDER);
68+
for ($i = 0, $count = count($results); $i < $count; $i++) {
69+
if (isset($results[$i][2])) {
70+
$quote = $results[$i][1];
71+
$this->_addPhrase($quote . $results[$i][2] . $quote, $lineNumber);
72+
}
5273
}
5374
}
5475
}
76+
} catch (\Exception $e) {
77+
$this->_filesystem->fileClose($fileHandle);
78+
throw new FileSystemException(
79+
new \Magento\Framework\Phrase('Stream get line failed %1', [$e->getMessage()])
80+
);
81+
}
82+
$this->_filesystem->fileClose($fileHandle);
83+
}
84+
85+
/**
86+
* Reads the line content from file pointer (with specified number of bytes from the current position).
87+
*
88+
* @param resource $resource
89+
* @param int $length
90+
* @param string $ending [optional]
91+
* @return string
92+
* @throws FileSystemException
93+
*/
94+
public function fileReadLine($resource, $length, $ending = null)
95+
{
96+
try {
97+
// phpcs:disable
98+
$result = @stream_get_line($resource, $length, $ending);
99+
// phpcs:enable
100+
} catch (\Exception $e) {
101+
throw new FileSystemException(
102+
new \Magento\Framework\Phrase('Stream get line failed %1', [$e->getMessage()])
103+
);
55104
}
56-
fclose($fileHandle);
105+
return $result;
57106
}
58107
}

setup/src/Magento/Setup/Module/I18n/ServiceLocator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ class ServiceLocator
2828
private static $_context;
2929

3030
/**
31-
* Dictionary generator
31+
* I18n Dictionary generator
3232
*
3333
* @var \Magento\Setup\Module\I18n\Dictionary\Generator
3434
*/
3535
private static $_dictionaryGenerator;
3636

3737
/**
38-
* Pack generator
38+
* I18n Pack generator
3939
*
4040
* @var \Magento\Setup\Module\I18n\Pack\Generator
4141
*/

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,31 +43,31 @@ public function testParse()
4343
[
4444
'phrase' => 'Phrase 1',
4545
'file' => $this->_testFile,
46-
'line' => $this->_stringsCount - 4,
46+
'line' => 1,
4747
'quote' => Phrase::QUOTE_SINGLE,
4848
],
4949
[
5050
'phrase' => 'Phrase 2 %1',
5151
'file' => $this->_testFile,
52-
'line' => $this->_stringsCount - 3,
52+
'line' => 1,
5353
'quote' => Phrase::QUOTE_DOUBLE
5454
],
5555
[
5656
'phrase' => 'Field ',
5757
'file' => $this->_testFile,
58-
'line' => $this->_stringsCount - 2,
58+
'line' => 1,
5959
'quote' => Phrase::QUOTE_SINGLE
6060
],
6161
[
6262
'phrase' => ' is required.',
6363
'file' => $this->_testFile,
64-
'line' => $this->_stringsCount - 2,
64+
'line' => 1,
6565
'quote' => Phrase::QUOTE_SINGLE
6666
],
6767
[
6868
'phrase' => 'Welcome, %1!',
6969
'file' => $this->_testFile,
70-
'line' => $this->_stringsCount - 1,
70+
'line' => 1,
7171
'quote' => Phrase::QUOTE_SINGLE
7272
]
7373
];

0 commit comments

Comments
 (0)