mirror of
https://github.com/barryvdh/laravel-ide-helper.git
synced 2026-08-18 01:57:13 +00:00
Add multi-level directory support for translation files (#1718)
This commit is contained in:
@@ -1,53 +1,68 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
function vsCodeGetTranslationsFromFile($file, $path, $namespace)
|
function vsCodeGetTranslationsFromFile(Symfony\Component\Finder\SplFileInfo $file, $path, $namespace)
|
||||||
{
|
{
|
||||||
$key = pathinfo($file, PATHINFO_FILENAME);
|
if ($file->getExtension() !== 'php') {
|
||||||
|
return null;
|
||||||
if ($namespace) {
|
|
||||||
$key = "{$namespace}::{$key}";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$lang = collect(explode(DIRECTORY_SEPARATOR, str_replace($path, '', $file)))
|
$filePath = $file->getRealPath();
|
||||||
->filter()
|
|
||||||
->first();
|
|
||||||
|
|
||||||
$fileLines = Illuminate\Support\Facades\File::lines($file);
|
$relativePath = trim(str_replace($path, '', $file->getPath()), DIRECTORY_SEPARATOR);
|
||||||
|
$lang = explode(DIRECTORY_SEPARATOR, $relativePath)[0] ?? null;
|
||||||
|
|
||||||
|
if (!$lang) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$keyPath = str_replace($path . DIRECTORY_SEPARATOR . $lang . DIRECTORY_SEPARATOR, '', $filePath);
|
||||||
|
$keyWithSlashes = str_replace('.php', '', $keyPath);
|
||||||
|
$baseKey = str_replace(DIRECTORY_SEPARATOR, '.', $keyWithSlashes);
|
||||||
|
|
||||||
|
if ($namespace) {
|
||||||
|
$baseKey = "{$namespace}::{$baseKey}";
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$translations = require $filePath;
|
||||||
|
} catch (Throwable $e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!is_array($translations)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$fileLines = Illuminate\Support\Facades\File::lines($filePath);
|
||||||
$lines = [];
|
$lines = [];
|
||||||
$inComment = false;
|
$inComment = false;
|
||||||
|
|
||||||
foreach ($fileLines as $index => $line) {
|
foreach ($fileLines as $index => $line) {
|
||||||
$trimmed = trim($line);
|
$trimmed = trim($line);
|
||||||
|
if (str_starts_with($trimmed, '/*')) {
|
||||||
if (substr($trimmed, 0, 2) === '/*') {
|
|
||||||
$inComment = true;
|
$inComment = true;
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($inComment) {
|
if ($inComment) {
|
||||||
if (substr($trimmed, -2) !== '*/') {
|
if (str_ends_with($trimmed, '*/')) {
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$inComment = false;
|
$inComment = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (substr($trimmed, 0, 2) === '//') {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if (str_starts_with($trimmed, '//')) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
$lines[] = [$index + 1, $trimmed];
|
$lines[] = [$index + 1, $trimmed];
|
||||||
}
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'k' => $key,
|
'k' => $baseKey,
|
||||||
'la' => $lang,
|
'la' => $lang,
|
||||||
'vs' => collect(Illuminate\Support\Arr::dot((Illuminate\Support\Arr::wrap(__($key, [], $lang)))))
|
'vs' => collect(Illuminate\Support\Arr::dot($translations))
|
||||||
->map(
|
->map(
|
||||||
fn ($value, $key) => vsCodeTranslationValue(
|
fn ($value, $dotKey) => vsCodeTranslationValue(
|
||||||
$key,
|
$dotKey,
|
||||||
$value,
|
$value,
|
||||||
str_replace(base_path(DIRECTORY_SEPARATOR), '', $file),
|
str_replace(base_path(DIRECTORY_SEPARATOR), '', $filePath),
|
||||||
$lines
|
$lines
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@@ -63,13 +78,12 @@ function vsCodeTranslationValue($key, $value, $file, $lines): ?array
|
|||||||
|
|
||||||
$lineNumber = 1;
|
$lineNumber = 1;
|
||||||
$keys = explode('.', $key);
|
$keys = explode('.', $key);
|
||||||
$index = 0;
|
|
||||||
$currentKey = array_shift($keys);
|
$currentKey = array_shift($keys);
|
||||||
|
|
||||||
foreach ($lines as $index => $line) {
|
foreach ($lines as $line) {
|
||||||
if (
|
if (
|
||||||
strpos($line[1], '"' . $currentKey . '"', 0) !== false ||
|
strpos($line[1], '"' . $currentKey . '"') !== false ||
|
||||||
strpos($line[1], "'" . $currentKey . "'", 0) !== false
|
strpos($line[1], "'" . $currentKey . "'") !== false
|
||||||
) {
|
) {
|
||||||
$lineNumber = $line[0];
|
$lineNumber = $line[0];
|
||||||
$currentKey = array_shift($keys);
|
$currentKey = array_shift($keys);
|
||||||
@@ -98,9 +112,9 @@ function vscodeCollectTranslations(string $path, ?string $namespace = null)
|
|||||||
return collect();
|
return collect();
|
||||||
}
|
}
|
||||||
|
|
||||||
return collect(Illuminate\Support\Facades\File::allFiles($realPath))->map(
|
return collect(Illuminate\Support\Facades\File::allFiles($realPath))
|
||||||
fn ($file) => vsCodeGetTranslationsFromFile($file, $path, $namespace)
|
->map(fn ($file) => vsCodeGetTranslationsFromFile($file, $path, $namespace))
|
||||||
);
|
->filter();
|
||||||
}
|
}
|
||||||
|
|
||||||
$loader = app('translator')->getLoader();
|
$loader = app('translator')->getLoader();
|
||||||
@@ -125,6 +139,10 @@ $namespaced = collect($namespaces)->flatMap(
|
|||||||
$final = [];
|
$final = [];
|
||||||
|
|
||||||
foreach ($default->merge($namespaced) as $value) {
|
foreach ($default->merge($namespaced) as $value) {
|
||||||
|
if (!isset($value['vs']) || !is_iterable($value['vs'])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
foreach ($value['vs'] as $key => $v) {
|
foreach ($value['vs'] as $key => $v) {
|
||||||
$dotKey = "{$value['k']}.{$key}";
|
$dotKey = "{$value['k']}.{$key}";
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user