@@ -20,7 +20,7 @@ public function __construct($basePath)
20
20
21
21
$ this ->translator = app ('translator ' );
22
22
}
23
-
23
+
24
24
/**
25
25
* Translate the given message
26
26
*
@@ -31,75 +31,78 @@ public function __construct($basePath)
31
31
*/
32
32
public function trans ($ key , $ default = null , $ placeholders = [])
33
33
{
34
- if (gettype (__ ($ key )) === 'array ' ) {
34
+ $ translation = __ ($ key , $ placeholders );
35
+ if (is_array ($ translation )) {
35
36
return $ key ;
36
37
}
38
+ if (config ('app.debug ' )) {
39
+ if (!is_array ($ default )) {
40
+ $ default = [$ this ->translator ->getFallback () => $ default ];
41
+ }
42
+ foreach ($ default as $ locale => $ item ) {
43
+ $ this ->updateTranslation ($ key , $ item , $ locale );
44
+ }
45
+ $ translation = __ ($ key , $ placeholders );
46
+ }
37
47
38
- $ fallbackLocale = $ this ->translator ->getFallback ();
39
-
40
- if ($ this ->canBeUpdated ($ default , $ key , $ fallbackLocale )) {
41
-
42
- $ path = explode ('. ' , $ key );
43
-
44
- $ filename = $ path [0 ];
45
-
46
- if (! isset ($ path [1 ])) return __ ($ key , $ placeholders );
47
-
48
- $ this ->translator ->addLines ([$ key => $ default ], $ fallbackLocale );
48
+ return $ translation ;
49
+ }
49
50
51
+ private function updateTranslation ($ key , $ default , $ locale )
52
+ {
53
+ $ path = explode ('. ' , $ key );
54
+ if ($ this ->canBeUpdated ($ default , $ key , $ locale ) && !empty ($ path [1 ])) {
55
+ $ this ->translator ->addLines ([$ key => $ default ], $ locale );
50
56
$ this ->writeToLangFile (
51
- $ fallbackLocale ,
52
- $ this ->translator ->get ($ filename ),
53
- $ filename
57
+ $ locale ,
58
+ $ this ->translator ->get ($ path [ 0 ] ),
59
+ $ path [ 0 ]
54
60
);
55
61
56
62
}
57
- return __ ($ key , $ placeholders );
58
63
}
59
64
60
- private function canBeUpdated ($ default , $ key , $ fallbackLocale )
65
+ private function canBeUpdated ($ default , $ key , $ locale )
61
66
{
67
+ if (!$ default || $ this ->translator ->hasForLocale ($ key , $ locale )) {
68
+ return false ;
69
+ }
62
70
$ parsedKey = $ this ->translator ->parseKey ($ key );
63
-
64
71
[$ namespace , $ path , $ item ] = $ parsedKey ;
65
-
66
72
$ items = array_filter (explode ('. ' , $ item ));
67
-
68
73
foreach ($ items as $ item ) {
69
74
$ path .= '. ' . $ item ;
70
- if ($ this ->translator ->has ($ path ) && gettype ($ this ->translator ->get ($ path )) === 'string ' ) {
75
+ if ($ this ->translator ->hasForLocale ($ path , $ locale )
76
+ && is_string ($ this ->translator ->get ($ path , [], $ locale ))) {
71
77
return false ;
72
78
}
73
79
}
74
80
75
- return $ default && config ('app.debug ' )
76
- && !$ this ->translator ->hasForLocale ($ key , $ fallbackLocale );
81
+ return true ;
77
82
}
78
83
79
84
/**
80
85
* Write to language file
81
- *
86
+ *
82
87
* @param $locale
83
88
* @param $translations
84
89
* @return bool
85
90
*/
86
91
private function writeToLangFile ($ locale , $ translations , $ filename )
87
92
{
88
- $ header = "<?php \n\nreturn " ;
89
-
90
- $ language_file = $ this ->basePath . "/ {$ locale }/ {$ filename }.php " ;
91
-
93
+ $ file = $ this ->basePath . "/ {$ locale }/ {$ filename }.php " ;
92
94
try {
93
- if (($ fp = fopen ($ language_file , 'w ' )) !== FALSE ) {
94
-
95
+ if (($ fp = fopen ($ file , 'w ' )) !== FALSE ) {
96
+ $ header = " <?php \n\n return " ;
95
97
fputs ($ fp , $ header . $ this ->var_export54 ($ translations ) . "; \n" );
96
-
97
98
fclose ($ fp );
98
-
99
+
99
100
return true ;
100
101
}
101
102
} catch (\Exception $ e ) {
102
- return false ;}}
103
+ return false ;
104
+ }
105
+ }
103
106
104
107
/**
105
108
* var_export to php5.4 array syntax
@@ -112,26 +115,19 @@ private function writeToLangFile($locale, $translations, $filename)
112
115
private function var_export54 ($ var , $ indent = "" )
113
116
{
114
117
switch (gettype ($ var )) {
115
-
116
118
case "string " :
117
119
return '" ' . addcslashes ($ var , "\\\$\"\r\n\t\v\f" ) . '" ' ;
118
-
119
120
case "array " :
120
121
$ indexed = array_keys ($ var ) === range (0 , count ($ var ) - 1 );
121
-
122
122
$ r = [];
123
-
124
123
foreach ($ var as $ key => $ value ) {
125
124
$ r [] = "$ indent "
126
125
. ($ indexed ? "" : $ this ->var_export54 ($ key ) . " => " )
127
126
. $ this ->var_export54 ($ value , "$ indent " );
128
127
}
129
-
130
128
return "[ \n" . implode (", \n" , $ r ) . "\n" . $ indent . "] " ;
131
-
132
129
case "boolean " :
133
130
return $ var ? "TRUE " : "FALSE " ;
134
-
135
131
default :
136
132
return var_export ($ var , TRUE );
137
133
}
0 commit comments