11<?php
22
3- function vsCodeGetTranslationsFromFile ($ file , $ path , $ namespace )
3+ function vsCodeGetTranslationsFromFile (Symfony \ Component \ Finder \ SplFileInfo $ file , $ path , $ namespace )
44{
5- $ key = pathinfo ($ file , PATHINFO_FILENAME );
5+ if ($ file ->getExtension () !== 'php ' ) {
6+ return null ;
7+ }
8+
9+ $ filePath = $ file ->getRealPath ();
10+
11+ $ relativePath = trim (str_replace ($ path , '' , $ file ->getPath ()), DIRECTORY_SEPARATOR );
12+ $ lang = explode (DIRECTORY_SEPARATOR , $ relativePath )[0 ] ?? null ;
13+
14+ if (!$ lang ) {
15+ return null ;
16+ }
17+
18+ $ keyPath = str_replace ($ path . DIRECTORY_SEPARATOR . $ lang . DIRECTORY_SEPARATOR , '' , $ filePath );
19+ $ keyWithSlashes = str_replace ('.php ' , '' , $ keyPath );
20+ $ baseKey = str_replace (DIRECTORY_SEPARATOR , '. ' , $ keyWithSlashes );
621
722 if ($ namespace ) {
8- $ key = "{$ namespace }:: {$ key }" ;
23+ $ baseKey = "{$ namespace }:: {$ baseKey }" ;
24+ }
25+
26+ try {
27+ $ translations = require $ filePath ;
28+ } catch (Throwable $ e ) {
29+ return null ;
930 }
1031
11- $ lang = collect ( explode ( DIRECTORY_SEPARATOR , str_replace ( $ path , '' , $ file )))
12- -> filter ()
13- -> first ();
32+ if (! is_array ( $ translations )) {
33+ return null ;
34+ }
1435
15- $ fileLines = Illuminate \Support \Facades \File::lines ($ file );
36+ $ fileLines = Illuminate \Support \Facades \File::lines ($ filePath );
1637 $ lines = [];
1738 $ inComment = false ;
1839
1940 foreach ($ fileLines as $ index => $ line ) {
2041 $ trimmed = trim ($ line );
21-
22- if (substr ($ trimmed , 0 , 2 ) === '/* ' ) {
42+ if (str_starts_with ($ trimmed , '/* ' )) {
2343 $ inComment = true ;
24- continue ;
2544 }
26-
2745 if ($ inComment ) {
28- if (substr ($ trimmed , - 2 ) !== '*/ ' ) {
29- continue ;
46+ if (str_ends_with ($ trimmed , '*/ ' ) ) {
47+ $ inComment = false ;
3048 }
31-
32- $ inComment = false ;
49+ continue ;
3350 }
34-
35- if (substr ($ trimmed , 0 , 2 ) === '// ' ) {
51+ if (str_starts_with ($ trimmed , '// ' )) {
3652 continue ;
3753 }
38-
3954 $ lines [] = [$ index + 1 , $ trimmed ];
4055 }
4156
4257 return [
43- 'k ' => $ key ,
58+ 'k ' => $ baseKey ,
4459 '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- )
60+ 'vs ' => collect (Illuminate \Support \Arr::dot ($ translations ))
61+ ->map (
62+ fn ($ value , $ dotKey ) => vsCodeTranslationValue (
63+ $ dotKey ,
64+ $ value ,
65+ str_replace (base_path (DIRECTORY_SEPARATOR ), '' , $ filePath ),
66+ $ lines
5367 )
68+ )
5469 ->filter (),
5570 ];
5671}
@@ -63,13 +78,12 @@ function vsCodeTranslationValue($key, $value, $file, $lines): ?array
6378
6479 $ lineNumber = 1 ;
6580 $ keys = explode ('. ' , $ key );
66- $ index = 0 ;
6781 $ currentKey = array_shift ($ keys );
6882
69- foreach ($ lines as $ index => $ line ) {
83+ foreach ($ lines as $ line ) {
7084 if (
71- strpos ($ line [1 ], '" ' . $ currentKey . '" ' , 0 ) !== false ||
72- strpos ($ line [1 ], "' " . $ currentKey . "' " , 0 ) !== false
85+ strpos ($ line [1 ], '" ' . $ currentKey . '" ' ) !== false ||
86+ strpos ($ line [1 ], "' " . $ currentKey . "' " ) !== false
7387 ) {
7488 $ lineNumber = $ line [0 ];
7589 $ currentKey = array_shift ($ keys );
@@ -98,9 +112,9 @@ function vscodeCollectTranslations(string $path, ?string $namespace = null)
98112 return collect ();
99113 }
100114
101- return collect (Illuminate \Support \Facades \File::allFiles ($ realPath ))-> map (
102- fn ($ file ) => vsCodeGetTranslationsFromFile ($ file , $ path , $ namespace )
103- );
115+ return collect (Illuminate \Support \Facades \File::allFiles ($ realPath ))
116+ -> map ( fn ($ file ) => vsCodeGetTranslationsFromFile ($ file , $ path , $ namespace) )
117+ -> filter ( );
104118}
105119
106120$ loader = app ('translator ' )->getLoader ();
@@ -125,6 +139,10 @@ function vscodeCollectTranslations(string $path, ?string $namespace = null)
125139$ final = [];
126140
127141foreach ($ default ->merge ($ namespaced ) as $ value ) {
142+ if (!isset ($ value ['vs ' ]) || !is_iterable ($ value ['vs ' ])) {
143+ continue ;
144+ }
145+
128146 foreach ($ value ['vs ' ] as $ key => $ v ) {
129147 $ dotKey = "{$ value ['k ' ]}. {$ key }" ;
130148
0 commit comments