|
| 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); |
0 commit comments