File tree 3 files changed +52
-3
lines changed
3 files changed +52
-3
lines changed Original file line number Diff line number Diff line change @@ -59,7 +59,7 @@ use KgBot\LaravelLocalization\Facades\ExportLocalizations as LaravelLocalization
59
59
View::composer( 'view.file', function ( $view ) {
60
60
61
61
return $view->with( [
62
- 'messages' => ExportLocalization::export(),
62
+ 'messages' => ExportLocalization::export()->toArray() ,
63
63
] );
64
64
} );
65
65
```
Original file line number Diff line number Diff line change 13
13
14
14
class ExportLocalizations
15
15
{
16
+ /**
17
+ * @var $strings array
18
+ */
19
+ protected $ strings = [];
20
+
16
21
public function export ()
17
22
{
18
23
function dirToArray ( $ dir )
@@ -91,6 +96,50 @@ function dirToArray( $dir )
91
96
92
97
event ( new LaravelLocalizationExported ( $ strings ) );
93
98
94
- return $ strings ;
99
+ $ this ->strings = $ strings ;
100
+
101
+ return $ this ;
102
+ }
103
+
104
+ public function toArray ()
105
+ {
106
+ return $ this ->strings ;
107
+ }
108
+
109
+ /**
110
+ * If you need special format of array that's recognised by some npm localization packages as Lang.js
111
+ * https://github.com/rmariuzzo/Lang.js use this method
112
+ *
113
+ * @param array $array
114
+ * @param string $prefix
115
+ *
116
+ * @return array
117
+ */
118
+ public function toFlat ( $ array = [], $ prefix = '' )
119
+ {
120
+ $ array = ( count ( $ array ) ) ? $ array : $ this ->strings ;
121
+ $ result = [];
122
+
123
+ foreach ( $ array as $ key => $ value ) {
124
+ $ new_key = $ prefix . ( empty ( $ prefix ) ? '' : '. ' ) . $ key ;
125
+
126
+ if ( is_array ( $ value ) ) {
127
+ $ result = array_merge ( $ result , $ this ->toFlat ( $ value , $ new_key ) );
128
+ } else {
129
+ $ result [ $ new_key ] = $ value ;
130
+ }
131
+ }
132
+
133
+ return $ result ;
134
+ }
135
+
136
+ public function toJson ()
137
+ {
138
+ return json_encode ( $ this ->strings );
139
+ }
140
+
141
+ public function toCollection ()
142
+ {
143
+ return collect ( $ this ->strings );
95
144
}
96
145
}
Original file line number Diff line number Diff line change 13
13
*/
14
14
Route::get ( config ( 'laravel-localization.routes.prefix ' ), function () {
15
15
16
- $ strings = ExportLocalizations::export ();
16
+ $ strings = ExportLocalizations::export ()-> toArray () ;
17
17
18
18
return response ()->json ( $ strings );
19
19
} )->name ( 'assets.lang ' )->middleware ( config ( 'laravel-localization.routes.middleware ' ) );
You can’t perform that action at this time.
0 commit comments