Skip to content

Commit

Permalink
Export callback, moved route logic outside of closure function so rou…
Browse files Browse the repository at this point in the history
…tes can be cached now
  • Loading branch information
kg-bot committed Feb 28, 2020
1 parent ef6e416 commit 194e093
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 34 deletions.
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,50 @@ It can be just one path or multiple paths, for example
paths => [resource_path('lang'), app_path('lang'), app_path('Modules/Blog/lang')]
```

You can run your own callback function after export, to do that you must register globally accessible function, for example
register `function.php` inside composer files autoload, and add your function inside `config/laravel-localization.php`,
key `export_callback`. Example:
```php
// helpers/functions.php

if (! function_exists('testExport')) {
/**
* Change all instances of :argument to {argument}
*
* @param $string
* @return void
*
*/
function testExport($string) {
array_walk_recursive($string, function (&$v, $k) { $v = preg_replace('/:(\w+)/', '{$1}', $v); });

return $string;
}
}


// composer.json
....
"autoload": {
"files": [
"helpers/functions.php"
],
"psr-4": {
"App\\": "app/"
},
"classmap": [
"database/seeds",
"database/factories"
]
},
....

// config/laravel-localization.php
...
'export_callback' => 'testExport',
...
```

# Usage

This package can be used in multiple ways, I'll give examples for some of them, but there's really no limitation.
Expand Down
21 changes: 17 additions & 4 deletions src/Classes/ExportLocalizations.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ protected function findLanguageFiles($path)
*/
public function jsonSerialize()
{
return $this->strings;
return self::executeCallback($this->strings);
}

/**
Expand All @@ -174,7 +174,7 @@ public function jsonSerialize()
*/
public function toArray()
{
return $this->strings;
return self::executeCallback($this->strings);
}

/**
Expand Down Expand Up @@ -225,7 +225,7 @@ public function toFlat($prefix = '.')
$results[$default_key] = array_combine($buffer, $buffer);
}

return $results;
return self::executeCallback($results);
}

/**
Expand All @@ -235,7 +235,7 @@ public function toFlat($prefix = '.')
*/
public function toCollection()
{
return collect($this->strings);
return collect(self::executeCallback($this->strings));
}

/**
Expand Down Expand Up @@ -335,4 +335,17 @@ protected function parseJsonFiles($file, $key, $dir)
];
}
}

public static function exportToArray(){
return (new ExportLocalizations)->export()->toArray();
}

protected static function executeCallback($strings)
{
if($callback = config('laravel-localization.export_callback')) {
$strings = $callback($strings);
}

return $strings;
}
}
55 changes: 30 additions & 25 deletions src/config/laravel-localization.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@

'routes' => [

/*
/**
* Route prefix, example of route http://localhost/js/localizations.js
*
*/
'prefix' => env('LARAVEL_LOCALIZATION_PREFIX', '/js/localization.js'),
'prefix' => env('LARAVEL_LOCALIZATION_PREFIX', '/js/localization.js'),

/*
/**
* Route name, defaults to assets.lang
*/
'name' => env('LARAVEL_LOCALIZATION_ROUTE_NAME', 'assets.lang'),
'name' => env('LARAVEL_LOCALIZATION_ROUTE_NAME', 'assets.lang'),

/*
/**
* Middleware used on localization routes.
*
* You can add more middleware with .env directive, example LARAVEL_LOCALIZATION_MIDDLEWARE=web,auth:api, etc.
Expand All @@ -32,14 +32,14 @@
explode(',', env('LARAVEL_LOCALIZATION_MIDDLEWARE'))
: [],

/*
/**
* Should we enable public URL from which we can access translations
*/
'enable' => env('LARAVEL_LOCALIZATION_ROUTE_ENABLE', false),
'enable' => env('LARAVEL_LOCALIZATION_ROUTE_ENABLE', false),
],
'events' => [

/*
/**
* This package emits some events after it getters all translation messages
*
* Here you can change channel on which events will broadcast
Expand All @@ -48,53 +48,58 @@
],
'caches' => [

/*
/**
* What cache driver do you want to use - more information: https://laravel.com/docs/5.6/cache#driver-prerequisites
*/
'driver' => 'file',
'driver' => 'file',

/*
/**
* Key name of the cache entry for the localization array
*/
'key' => 'localization.array',
'key' => 'localization.array',

/*
/**
* Timeout of the cached data in minutes - set to 0 to disable
*/
'timeout' => 60,
],
'js' => [
/*
'js' => [
/**
* Default locale for export
*/
'default_locale' => 'en',

/*
/**
* root location to where JavaScript file will be exported
*/
'filepath' => resource_path('assets/js'),
'filepath' => resource_path('assets/js'),

/*
/**
* File name for JavaScript file with exported messages
*/
'filename' => 'll_messages.js',
'filename' => 'll_messages.js',
],
'paths' => [
'paths' => [

/*
/**
* You can export more lang files then just files in resources/lang, for example
*
* In you .env file just add:
* LARAVEL_LOCALIZATION_LANG_DIRS=resources/lang,Modules/Blog/Resources/lang
*/
'lang_dirs' => [resource_path('lang')],
],
/*
* You can customize the regexp for lang files to be able to exclude certain files.
*/
'file_regexp' => [
/**
* You can customize the regexp for lang files to be able to exclude certain files.
*/
'file_regexp' => [
'php' => '/^.+\.php$/i',
'json' => '/^.+\.json$/i',
],
/**
* This function will be called every time after export, it should be globally accessible function (eg. Laravel helper function)
* and it should accept (string) argument
*/
'export_callback' => null,

];
6 changes: 1 addition & 5 deletions src/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
/*
* Localization
*/
Route::get(config('laravel-localization.routes.prefix'), function () {
$strings = ExportLocalizations::export()->toArray();

return response()->json($strings);
})->name(config('laravel-localization.routes.name'))
Route::get(config('laravel-localization.routes.prefix'), 'KgBot\LaravelLocalization\Classes\ExportLocalizations@exportToArray')->name(config('laravel-localization.routes.name'))
->middleware(config('laravel-localization.routes.middleware'));
}

0 comments on commit 194e093

Please sign in to comment.