Skip to content

Commit b3f2c7a

Browse files
committed
New methods, toArray(), toJson(), toCollection(), toFlat()
1 parent 1e5d960 commit b3f2c7a

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ use KgBot\LaravelLocalization\Facades\ExportLocalizations as LaravelLocalization
5959
View::composer( 'view.file', function ( $view ) {
6060
6161
return $view->with( [
62-
'messages' => ExportLocalization::export(),
62+
'messages' => ExportLocalization::export()->toArray(),
6363
] );
6464
} );
6565
```

src/Classes/ExportLocalizations.php

+50-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313

1414
class ExportLocalizations
1515
{
16+
/**
17+
* @var $strings array
18+
*/
19+
protected $strings = [];
20+
1621
public function export()
1722
{
1823
function dirToArray( $dir )
@@ -91,6 +96,50 @@ function dirToArray( $dir )
9196

9297
event( new LaravelLocalizationExported( $strings ) );
9398

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 );
95144
}
96145
}

src/routes.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
Route::get( config( 'laravel-localization.routes.prefix' ), function () {
1515

16-
$strings = ExportLocalizations::export();
16+
$strings = ExportLocalizations::export()->toArray();
1717

1818
return response()->json( $strings );
1919
} )->name( 'assets.lang' )->middleware( config( 'laravel-localization.routes.middleware' ) );

0 commit comments

Comments
 (0)