1
1
<?php
2
2
3
- function vsCodeGetTranslationsFromFile ($ file , $ path , $ namespace )
3
+ function vsCodeGetTranslationsFromFile (Symfony \ Component \ Finder \ SplFileInfo $ file , $ path , $ namespace )
4
4
{
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 );
6
21
7
22
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 ;
9
30
}
10
31
11
- $ lang = collect ( explode ( DIRECTORY_SEPARATOR , str_replace ( $ path , '' , $ file )))
12
- -> filter ()
13
- -> first ();
32
+ if (! is_array ( $ translations )) {
33
+ return null ;
34
+ }
14
35
15
- $ fileLines = Illuminate \Support \Facades \File::lines ($ file );
36
+ $ fileLines = Illuminate \Support \Facades \File::lines ($ filePath );
16
37
$ lines = [];
17
38
$ inComment = false ;
18
39
19
40
foreach ($ fileLines as $ index => $ line ) {
20
41
$ trimmed = trim ($ line );
21
-
22
- if (substr ($ trimmed , 0 , 2 ) === '/* ' ) {
42
+ if (str_starts_with ($ trimmed , '/* ' )) {
23
43
$ inComment = true ;
24
- continue ;
25
44
}
26
-
27
45
if ($ inComment ) {
28
- if (substr ($ trimmed , - 2 ) !== '*/ ' ) {
29
- continue ;
46
+ if (str_ends_with ($ trimmed , '*/ ' ) ) {
47
+ $ inComment = false ;
30
48
}
31
-
32
- $ inComment = false ;
49
+ continue ;
33
50
}
34
-
35
- if (substr ($ trimmed , 0 , 2 ) === '// ' ) {
51
+ if (str_starts_with ($ trimmed , '// ' )) {
36
52
continue ;
37
53
}
38
-
39
54
$ lines [] = [$ index + 1 , $ trimmed ];
40
55
}
41
56
42
57
return [
43
- 'k ' => $ key ,
58
+ 'k ' => $ baseKey ,
44
59
'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
53
67
)
68
+ )
54
69
->filter (),
55
70
];
56
71
}
@@ -63,13 +78,12 @@ function vsCodeTranslationValue($key, $value, $file, $lines): ?array
63
78
64
79
$ lineNumber = 1 ;
65
80
$ keys = explode ('. ' , $ key );
66
- $ index = 0 ;
67
81
$ currentKey = array_shift ($ keys );
68
82
69
- foreach ($ lines as $ index => $ line ) {
83
+ foreach ($ lines as $ line ) {
70
84
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
73
87
) {
74
88
$ lineNumber = $ line [0 ];
75
89
$ currentKey = array_shift ($ keys );
@@ -98,9 +112,9 @@ function vscodeCollectTranslations(string $path, ?string $namespace = null)
98
112
return collect ();
99
113
}
100
114
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 ( );
104
118
}
105
119
106
120
$ loader = app ('translator ' )->getLoader ();
@@ -125,6 +139,10 @@ function vscodeCollectTranslations(string $path, ?string $namespace = null)
125
139
$ final = [];
126
140
127
141
foreach ($ default ->merge ($ namespaced ) as $ value ) {
142
+ if (!isset ($ value ['vs ' ]) || !is_iterable ($ value ['vs ' ])) {
143
+ continue ;
144
+ }
145
+
128
146
foreach ($ value ['vs ' ] as $ key => $ v ) {
129
147
$ dotKey = "{$ value ['k ' ]}. {$ key }" ;
130
148
0 commit comments