Skip to content

Commit 0a57a0a

Browse files
committed
getopts
1 parent 5e96d7d commit 0a57a0a

2 files changed

Lines changed: 52 additions & 7 deletions

File tree

i18n_add_t.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,18 @@
2828
if (str_ends_with($file, ".phtml")) {
2929
$content = preg_replace_callback_array(
3030
[
31-
'/(^|[^\?])>(\s*)(([^<]|(?=<\?php\s+e\(.*\);\s*\?>)<)+)(\s*)</m' => function ($matches) {
32-
if (!trim($matches[3]) || str_starts_with($matches[3], '<?php e(')) {
31+
'/(^|[^\?])>(([^<]|(?=<\?php\s+e\(.*\);\s*\?>)<)+)</s' => function ($matches) {
32+
if (!trim($matches[2]) || str_starts_with(trim($matches[2]), '<?php e(')) {
3333
return $matches[0];
3434
}
35-
$string = $matches[3];
35+
$string = $matches[2];
36+
$leadingWhite = '';
37+
$trailingWhite = '';
38+
if (preg_match('/^(\s*)(.*?)(\s*)$/s', $string, $whiteMatches)) {
39+
$leadingWhite = $whiteMatches[1];
40+
$string = $whiteMatches[2];
41+
$trailingWhite = $whiteMatches[3];
42+
}
3643
$args = [];
3744
$string = preg_replace_callback_array(
3845
[
@@ -44,11 +51,11 @@
4451
$string
4552
);
4653
if ($args) {
47-
return $matches[1] . '>' . $matches[2] . '<?php e(t("' . str_replace('\"', "'", addslashes($string)) . '", ' . implode(',', $args) . ')); ?>' . $matches[5] . '<';
54+
return $matches[1] . '>' . $leadingWhite . '<?php e(t("' . str_replace('\"', "'", addslashes(preg_replace('/\s+/', ' ', $string))) . '", ' . implode(',', $args) . ')); ?>' . $trailingWhite . '<';
4855
}
49-
return $matches[1] . '>' . $matches[2] . '<?php e(t("' . str_replace('\"', "'", addslashes($string)) . '")); ?>' . $matches[5] . '<';
56+
return $matches[1] . '>' . $leadingWhite . '<?php e(t("' . str_replace('\"', "'", addslashes(preg_replace('/\s+/', ' ', $string))) . '")); ?>' . $trailingWhite . '<';
5057
},
51-
'/((alt|title|placeholder)="(.*?)")/m' => function ($matches) {
58+
'/((alt|title|placeholder)="(.*?)")/s' => function ($matches) {
5259
if (!trim($matches[3]) || str_starts_with($matches[3], '<?php e(')) {
5360
return $matches[0];
5461
}
@@ -61,7 +68,7 @@
6168
if (str_ends_with($file, ".php")) {
6269
$content = preg_replace_callback_array(
6370
[
64-
'/error[^=]+=[^"](".*?")/m' => function ($matches) {
71+
'/error[^=]+=[^"](".*?")/s' => function ($matches) {
6572
return str_replace($matches[1], 't(' . $matches[1] . ')', $matches[0]);
6673
},
6774
],

i18n_extract.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<?php
22

3+
use PhpParser\Lexer;
34
use PhpParser\Node;
45
use PhpParser\NodeTraverser;
56
use PhpParser\NodeVisitorAbstract;
67
use PhpParser\ParserFactory;
8+
use PhpParser\PrettyPrinter;
79

810
if (!file_exists('vendor/autoload.php') || !file_exists('pages')) {
911
echo "Please run this script from the project root.\n";
@@ -52,6 +54,7 @@ public function leaveNode(Node $node)
5254
if ($node->args[0] instanceof PhpParser\Node\Arg) {
5355
if ($node->args[0]->value instanceof PhpParser\Node\Scalar\String_) {
5456
$string = $node->args[0]->value->value;
57+
$string = preg_replace("/\s+/", " ", $string);
5558
if (!isset($this->strings[$string])) {
5659
$this->strings[$string] = [];
5760
}
@@ -68,6 +71,41 @@ public function getStrings(): array
6871
}
6972
}
7073

74+
function str_split_word_aware(string $string, int $maxLengthOfLine): array
75+
{
76+
if (strlen($string) < $maxLengthOfLine - 6) {
77+
return [$string];
78+
}
79+
80+
$lines = [''];
81+
$words = explode(' ', $string);
82+
83+
$currentLine = '';
84+
$lineAccumulator = '';
85+
foreach ($words as $currentWord) {
86+
87+
$currentWordWithSpace = sprintf('%s ', $currentWord);
88+
$lineAccumulator .= $currentWordWithSpace;
89+
if (strlen($lineAccumulator) < $maxLengthOfLine) {
90+
$currentLine = $lineAccumulator;
91+
continue;
92+
}
93+
94+
$lines[] = $currentLine;
95+
96+
// Overwrite the current line and accumulator with the current word
97+
$currentLine = $currentWordWithSpace;
98+
$lineAccumulator = $currentWordWithSpace;
99+
}
100+
101+
if ($currentLine !== '') {
102+
$lines[] = $currentLine;
103+
}
104+
105+
$lines[count($lines) - 1] = rtrim($lines[count($lines) - 1], ' ');
106+
107+
return $lines;
108+
}
71109

72110
$parserFactory = new ParserFactory();
73111
$parser = $parserFactory->createForNewestSupportedVersion();

0 commit comments

Comments
 (0)