From 701cf67ba06fb0aabcba22cbfa1cdc4138f6436c Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Sun, 29 Dec 2024 19:26:20 +0100 Subject: [PATCH] Add translations --- php-templates/translations.php | 143 +++++++++++++++++++++++++++++++++ src/Console/MetaCommand.php | 23 +++++- 2 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 php-templates/translations.php diff --git a/php-templates/translations.php b/php-templates/translations.php new file mode 100644 index 0000000..63a9791 --- /dev/null +++ b/php-templates/translations.php @@ -0,0 +1,143 @@ +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); diff --git a/src/Console/MetaCommand.php b/src/Console/MetaCommand.php index cc878cd..8afbf6f 100644 --- a/src/Console/MetaCommand.php +++ b/src/Console/MetaCommand.php @@ -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', + ], ]; }