Skip to content

Commit

Permalink
New methods, toArray(), toJson(), toCollection(), toFlat()
Browse files Browse the repository at this point in the history
  • Loading branch information
kg-bot committed Jun 9, 2018
1 parent 1e5d960 commit b3f2c7a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ use KgBot\LaravelLocalization\Facades\ExportLocalizations as LaravelLocalization
View::composer( 'view.file', function ( $view ) {
return $view->with( [
'messages' => ExportLocalization::export(),
'messages' => ExportLocalization::export()->toArray(),
] );
} );
```
Expand Down
51 changes: 50 additions & 1 deletion src/Classes/ExportLocalizations.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@

class ExportLocalizations
{
/**
* @var $strings array
*/
protected $strings = [];

public function export()
{
function dirToArray( $dir )
Expand Down Expand Up @@ -91,6 +96,50 @@ function dirToArray( $dir )

event( new LaravelLocalizationExported( $strings ) );

return $strings;
$this->strings = $strings;

return $this;
}

public function toArray()
{
return $this->strings;
}

/**
* If you need special format of array that's recognised by some npm localization packages as Lang.js
* https://github.com/rmariuzzo/Lang.js use this method
*
* @param array $array
* @param string $prefix
*
* @return array
*/
public function toFlat( $array = [], $prefix = '' )
{
$array = ( count( $array ) ) ? $array : $this->strings;
$result = [];

foreach ( $array as $key => $value ) {
$new_key = $prefix . ( empty( $prefix ) ? '' : '.' ) . $key;

if ( is_array( $value ) ) {
$result = array_merge( $result, $this->toFlat( $value, $new_key ) );
} else {
$result[ $new_key ] = $value;
}
}

return $result;
}

public function toJson()
{
return json_encode( $this->strings );
}

public function toCollection()
{
return collect( $this->strings );
}
}
2 changes: 1 addition & 1 deletion src/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
Route::get( config( 'laravel-localization.routes.prefix' ), function () {

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

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

0 comments on commit b3f2c7a

Please sign in to comment.