Skip to content

Commit 701cf67

Browse files
committed
Add translations
1 parent 1353c67 commit 701cf67

File tree

2 files changed

+165
-1
lines changed

2 files changed

+165
-1
lines changed

php-templates/translations.php

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<?php
2+
3+
function vsCodeGetTranslationsFromFile($file, $path, $namespace)
4+
{
5+
$key = pathinfo($file, PATHINFO_FILENAME);
6+
7+
if ($namespace) {
8+
$key = "{$namespace}::{$key}";
9+
}
10+
11+
$lang = collect(explode(DIRECTORY_SEPARATOR, str_replace($path, "", $file)))
12+
->filter()
13+
->first();
14+
15+
$fileLines = \Illuminate\Support\Facades\File::lines($file);
16+
$lines = [];
17+
$inComment = false;
18+
19+
foreach ($fileLines as $index => $line) {
20+
$trimmed = trim($line);
21+
22+
if (substr($trimmed, 0, 2) === "/*") {
23+
$inComment = true;
24+
continue;
25+
}
26+
27+
if ($inComment) {
28+
if (substr($trimmed, -2) !== "*/") {
29+
continue;
30+
}
31+
32+
$inComment = false;
33+
}
34+
35+
if (substr($trimmed, 0, 2) === "//") {
36+
continue;
37+
}
38+
39+
$lines[] = [$index + 1, $trimmed];
40+
}
41+
42+
return [
43+
"k" => $key,
44+
"la" => $lang,
45+
"vs" => collect(\Illuminate\Support\Arr::dot((\Illuminate\Support\Arr::wrap(__($key, [], $lang)))))
46+
->map(
47+
fn($value, $key) => vsCodeTranslationValue(
48+
$key,
49+
$value,
50+
str_replace(base_path(DIRECTORY_SEPARATOR), "", $file),
51+
$lines
52+
)
53+
)
54+
->filter()
55+
];
56+
}
57+
58+
function vsCodeTranslationValue($key, $value, $file, $lines): ?array
59+
{
60+
if (is_array($value)) {
61+
return null;
62+
}
63+
64+
$lineNumber = 1;
65+
$keys = explode(".", $key);
66+
$index = 0;
67+
$currentKey = array_shift($keys);
68+
69+
foreach ($lines as $index => $line) {
70+
if (
71+
strpos($line[1], '"' . $currentKey . '"', 0) !== false ||
72+
strpos($line[1], "'" . $currentKey . "'", 0) !== false
73+
) {
74+
$lineNumber = $line[0];
75+
$currentKey = array_shift($keys);
76+
}
77+
78+
if ($currentKey === null) {
79+
break;
80+
}
81+
}
82+
83+
return [
84+
"v" => $value,
85+
"p" => $file,
86+
"li" => $lineNumber,
87+
"pa" => preg_match_all("/\:([A-Za-z0-9_]+)/", $value, $matches)
88+
? $matches[1]
89+
: []
90+
];
91+
}
92+
93+
function vscodeCollectTranslations(string $path, ?string $namespace = null)
94+
{
95+
$realPath = realpath($path);
96+
97+
if (!is_dir($realPath)) {
98+
return collect();
99+
}
100+
101+
return collect(\Illuminate\Support\Facades\File::allFiles($realPath))->map(
102+
fn($file) => vsCodeGetTranslationsFromFile($file, $path, $namespace)
103+
);
104+
}
105+
106+
$loader = app("translator")->getLoader();
107+
$namespaces = $loader->namespaces();
108+
109+
$reflection = new ReflectionClass($loader);
110+
$property = $reflection->hasProperty("paths")
111+
? $reflection->getProperty("paths")
112+
: $reflection->getProperty("path");
113+
$property->setAccessible(true);
114+
115+
$paths = \Illuminate\Support\Arr::wrap($property->getValue($loader));
116+
117+
$default = collect($paths)->flatMap(
118+
fn($path) => vscodeCollectTranslations($path)
119+
);
120+
121+
$namespaced = collect($namespaces)->flatMap(
122+
fn($path, $namespace) => vscodeCollectTranslations($path, $namespace)
123+
);
124+
125+
$final = [];
126+
127+
foreach ($default->merge($namespaced) as $value) {
128+
foreach ($value["vs"] as $key => $v) {
129+
$dotKey = "{$value["k"]}.{$key}";
130+
131+
if (!isset($final[$dotKey])) {
132+
$final[$dotKey] = [];
133+
}
134+
135+
$final[$dotKey][$value["la"]] = $v;
136+
137+
if ($value["la"] === \Illuminate\Support\Facades\App::currentLocale()) {
138+
$final[$dotKey]["default"] = $v;
139+
}
140+
}
141+
}
142+
143+
return collect($final);

src/Console/MetaCommand.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ protected function getExpectedArgumentSets()
192192
'configs' => $this->loadTemplate('configs')->pluck('name')->filter(),
193193
'routes' => $this->loadTemplate('routes')->pluck('name')->filter(),
194194
'views' => $this->loadTemplate('views')->pluck('key')->filter(),
195-
195+
'translations' => $this->loadTemplate('translations')->filter()->keys(),
196196
];
197197
}
198198

@@ -217,9 +217,30 @@ protected function getExpectedArguments()
217217
'\route()' => [
218218
0 => 'routes',
219219
],
220+
'\Illuminate\Support\Facades\Route::get()' => [
221+
0 => 'routes',
222+
],
223+
'\Illuminate\Routing\Router::get()' => [
224+
0 => 'routes',
225+
],
220226
'\view()' => [
221227
0 => 'views',
222228
],
229+
'\Illuminate\Support\Facades\View::make()' => [
230+
0 => 'views',
231+
],
232+
'\Illuminate\View\Factory::make()' => [
233+
0 => 'views',
234+
],
235+
'\__()' => [
236+
0 => 'translations',
237+
],
238+
'\trans()' => [
239+
0 => 'translations',
240+
],
241+
'\Illuminate\Contracts\Translation\Translator::get()' => [
242+
0 => 'translations',
243+
],
223244
];
224245
}
225246

0 commit comments

Comments
 (0)