mirror of
https://github.com/barryvdh/laravel-ide-helper.git
synced 2026-08-18 01:57:13 +00:00
Add translations
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
<?php
|
||||
|
||||
function vsCodeGetTranslationsFromFile($file, $path, $namespace)
|
||||
{
|
||||
$key = pathinfo($file, PATHINFO_FILENAME);
|
||||
|
||||
if ($namespace) {
|
||||
$key = "{$namespace}::{$key}";
|
||||
}
|
||||
|
||||
$lang = collect(explode(DIRECTORY_SEPARATOR, str_replace($path, "", $file)))
|
||||
->filter()
|
||||
->first();
|
||||
|
||||
$fileLines = \Illuminate\Support\Facades\File::lines($file);
|
||||
$lines = [];
|
||||
$inComment = false;
|
||||
|
||||
foreach ($fileLines as $index => $line) {
|
||||
$trimmed = trim($line);
|
||||
|
||||
if (substr($trimmed, 0, 2) === "/*") {
|
||||
$inComment = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($inComment) {
|
||||
if (substr($trimmed, -2) !== "*/") {
|
||||
continue;
|
||||
}
|
||||
|
||||
$inComment = false;
|
||||
}
|
||||
|
||||
if (substr($trimmed, 0, 2) === "//") {
|
||||
continue;
|
||||
}
|
||||
|
||||
$lines[] = [$index + 1, $trimmed];
|
||||
}
|
||||
|
||||
return [
|
||||
"k" => $key,
|
||||
"la" => $lang,
|
||||
"vs" => collect(\Illuminate\Support\Arr::dot((\Illuminate\Support\Arr::wrap(__($key, [], $lang)))))
|
||||
->map(
|
||||
fn($value, $key) => vsCodeTranslationValue(
|
||||
$key,
|
||||
$value,
|
||||
str_replace(base_path(DIRECTORY_SEPARATOR), "", $file),
|
||||
$lines
|
||||
)
|
||||
)
|
||||
->filter()
|
||||
];
|
||||
}
|
||||
|
||||
function vsCodeTranslationValue($key, $value, $file, $lines): ?array
|
||||
{
|
||||
if (is_array($value)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$lineNumber = 1;
|
||||
$keys = explode(".", $key);
|
||||
$index = 0;
|
||||
$currentKey = array_shift($keys);
|
||||
|
||||
foreach ($lines as $index => $line) {
|
||||
if (
|
||||
strpos($line[1], '"' . $currentKey . '"', 0) !== false ||
|
||||
strpos($line[1], "'" . $currentKey . "'", 0) !== false
|
||||
) {
|
||||
$lineNumber = $line[0];
|
||||
$currentKey = array_shift($keys);
|
||||
}
|
||||
|
||||
if ($currentKey === null) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
"v" => $value,
|
||||
"p" => $file,
|
||||
"li" => $lineNumber,
|
||||
"pa" => preg_match_all("/\:([A-Za-z0-9_]+)/", $value, $matches)
|
||||
? $matches[1]
|
||||
: []
|
||||
];
|
||||
}
|
||||
|
||||
function vscodeCollectTranslations(string $path, ?string $namespace = null)
|
||||
{
|
||||
$realPath = realpath($path);
|
||||
|
||||
if (!is_dir($realPath)) {
|
||||
return collect();
|
||||
}
|
||||
|
||||
return collect(\Illuminate\Support\Facades\File::allFiles($realPath))->map(
|
||||
fn($file) => vsCodeGetTranslationsFromFile($file, $path, $namespace)
|
||||
);
|
||||
}
|
||||
|
||||
$loader = app("translator")->getLoader();
|
||||
$namespaces = $loader->namespaces();
|
||||
|
||||
$reflection = new ReflectionClass($loader);
|
||||
$property = $reflection->hasProperty("paths")
|
||||
? $reflection->getProperty("paths")
|
||||
: $reflection->getProperty("path");
|
||||
$property->setAccessible(true);
|
||||
|
||||
$paths = \Illuminate\Support\Arr::wrap($property->getValue($loader));
|
||||
|
||||
$default = collect($paths)->flatMap(
|
||||
fn($path) => vscodeCollectTranslations($path)
|
||||
);
|
||||
|
||||
$namespaced = collect($namespaces)->flatMap(
|
||||
fn($path, $namespace) => vscodeCollectTranslations($path, $namespace)
|
||||
);
|
||||
|
||||
$final = [];
|
||||
|
||||
foreach ($default->merge($namespaced) as $value) {
|
||||
foreach ($value["vs"] as $key => $v) {
|
||||
$dotKey = "{$value["k"]}.{$key}";
|
||||
|
||||
if (!isset($final[$dotKey])) {
|
||||
$final[$dotKey] = [];
|
||||
}
|
||||
|
||||
$final[$dotKey][$value["la"]] = $v;
|
||||
|
||||
if ($value["la"] === \Illuminate\Support\Facades\App::currentLocale()) {
|
||||
$final[$dotKey]["default"] = $v;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return collect($final);
|
||||
@@ -192,7 +192,7 @@ class MetaCommand extends Command
|
||||
'configs' => $this->loadTemplate('configs')->pluck('name')->filter(),
|
||||
'routes' => $this->loadTemplate('routes')->pluck('name')->filter(),
|
||||
'views' => $this->loadTemplate('views')->pluck('key')->filter(),
|
||||
|
||||
'translations' => $this->loadTemplate('translations')->filter()->keys(),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -217,9 +217,30 @@ class MetaCommand extends Command
|
||||
'\route()' => [
|
||||
0 => 'routes',
|
||||
],
|
||||
'\Illuminate\Support\Facades\Route::get()' => [
|
||||
0 => 'routes',
|
||||
],
|
||||
'\Illuminate\Routing\Router::get()' => [
|
||||
0 => 'routes',
|
||||
],
|
||||
'\view()' => [
|
||||
0 => 'views',
|
||||
],
|
||||
'\Illuminate\Support\Facades\View::make()' => [
|
||||
0 => 'views',
|
||||
],
|
||||
'\Illuminate\View\Factory::make()' => [
|
||||
0 => 'views',
|
||||
],
|
||||
'\__()' => [
|
||||
0 => 'translations',
|
||||
],
|
||||
'\trans()' => [
|
||||
0 => 'translations',
|
||||
],
|
||||
'\Illuminate\Contracts\Translation\Translator::get()' => [
|
||||
0 => 'translations',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user