From 194e093786fcef39136ea441692e6e707749a028 Mon Sep 17 00:00:00 2001 From: Stefan Ninic Date: Fri, 28 Feb 2020 13:41:21 +0100 Subject: [PATCH] Export callback, moved route logic outside of closure function so routes can be cached now --- README.md | 44 +++++++++++++++++++++++ src/Classes/ExportLocalizations.php | 21 ++++++++--- src/config/laravel-localization.php | 55 ++++++++++++++++------------- src/routes.php | 6 +--- 4 files changed, 92 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 6de157c..a6910e3 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/Classes/ExportLocalizations.php b/src/Classes/ExportLocalizations.php index b488ac9..3a45d20 100644 --- a/src/Classes/ExportLocalizations.php +++ b/src/Classes/ExportLocalizations.php @@ -164,7 +164,7 @@ protected function findLanguageFiles($path) */ public function jsonSerialize() { - return $this->strings; + return self::executeCallback($this->strings); } /** @@ -174,7 +174,7 @@ public function jsonSerialize() */ public function toArray() { - return $this->strings; + return self::executeCallback($this->strings); } /** @@ -225,7 +225,7 @@ public function toFlat($prefix = '.') $results[$default_key] = array_combine($buffer, $buffer); } - return $results; + return self::executeCallback($results); } /** @@ -235,7 +235,7 @@ public function toFlat($prefix = '.') */ public function toCollection() { - return collect($this->strings); + return collect(self::executeCallback($this->strings)); } /** @@ -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; + } } diff --git a/src/config/laravel-localization.php b/src/config/laravel-localization.php index 3621b5a..c1dbad5 100644 --- a/src/config/laravel-localization.php +++ b/src/config/laravel-localization.php @@ -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. @@ -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 @@ -48,40 +48,40 @@ ], '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: @@ -89,12 +89,17 @@ */ '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, ]; diff --git a/src/routes.php b/src/routes.php index 236d1b3..c0ac914 100644 --- a/src/routes.php +++ b/src/routes.php @@ -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')); }