Skip to content

Commit 194e093

Browse files
committed
Export callback, moved route logic outside of closure function so routes can be cached now
1 parent ef6e416 commit 194e093

File tree

4 files changed

+92
-34
lines changed

4 files changed

+92
-34
lines changed

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,50 @@ It can be just one path or multiple paths, for example
4848
paths => [resource_path('lang'), app_path('lang'), app_path('Modules/Blog/lang')]
4949
```
5050

51+
You can run your own callback function after export, to do that you must register globally accessible function, for example
52+
register `function.php` inside composer files autoload, and add your function inside `config/laravel-localization.php`,
53+
key `export_callback`. Example:
54+
```php
55+
// helpers/functions.php
56+
57+
if (! function_exists('testExport')) {
58+
/**
59+
* Change all instances of :argument to {argument}
60+
*
61+
* @param $string
62+
* @return void
63+
*
64+
*/
65+
function testExport($string) {
66+
array_walk_recursive($string, function (&$v, $k) { $v = preg_replace('/:(\w+)/', '{$1}', $v); });
67+
68+
return $string;
69+
}
70+
}
71+
72+
73+
// composer.json
74+
....
75+
"autoload": {
76+
"files": [
77+
"helpers/functions.php"
78+
],
79+
"psr-4": {
80+
"App\\": "app/"
81+
},
82+
"classmap": [
83+
"database/seeds",
84+
"database/factories"
85+
]
86+
},
87+
....
88+
89+
// config/laravel-localization.php
90+
...
91+
'export_callback' => 'testExport',
92+
...
93+
```
94+
5195
# Usage
5296

5397
This package can be used in multiple ways, I'll give examples for some of them, but there's really no limitation.

src/Classes/ExportLocalizations.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ protected function findLanguageFiles($path)
164164
*/
165165
public function jsonSerialize()
166166
{
167-
return $this->strings;
167+
return self::executeCallback($this->strings);
168168
}
169169

170170
/**
@@ -174,7 +174,7 @@ public function jsonSerialize()
174174
*/
175175
public function toArray()
176176
{
177-
return $this->strings;
177+
return self::executeCallback($this->strings);
178178
}
179179

180180
/**
@@ -225,7 +225,7 @@ public function toFlat($prefix = '.')
225225
$results[$default_key] = array_combine($buffer, $buffer);
226226
}
227227

228-
return $results;
228+
return self::executeCallback($results);
229229
}
230230

231231
/**
@@ -235,7 +235,7 @@ public function toFlat($prefix = '.')
235235
*/
236236
public function toCollection()
237237
{
238-
return collect($this->strings);
238+
return collect(self::executeCallback($this->strings));
239239
}
240240

241241
/**
@@ -335,4 +335,17 @@ protected function parseJsonFiles($file, $key, $dir)
335335
];
336336
}
337337
}
338+
339+
public static function exportToArray(){
340+
return (new ExportLocalizations)->export()->toArray();
341+
}
342+
343+
protected static function executeCallback($strings)
344+
{
345+
if($callback = config('laravel-localization.export_callback')) {
346+
$strings = $callback($strings);
347+
}
348+
349+
return $strings;
350+
}
338351
}

src/config/laravel-localization.php

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@
1010

1111
'routes' => [
1212

13-
/*
13+
/**
1414
* Route prefix, example of route http://localhost/js/localizations.js
1515
*
1616
*/
17-
'prefix' => env('LARAVEL_LOCALIZATION_PREFIX', '/js/localization.js'),
17+
'prefix' => env('LARAVEL_LOCALIZATION_PREFIX', '/js/localization.js'),
1818

19-
/*
19+
/**
2020
* Route name, defaults to assets.lang
2121
*/
22-
'name' => env('LARAVEL_LOCALIZATION_ROUTE_NAME', 'assets.lang'),
22+
'name' => env('LARAVEL_LOCALIZATION_ROUTE_NAME', 'assets.lang'),
2323

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

35-
/*
35+
/**
3636
* Should we enable public URL from which we can access translations
3737
*/
38-
'enable' => env('LARAVEL_LOCALIZATION_ROUTE_ENABLE', false),
38+
'enable' => env('LARAVEL_LOCALIZATION_ROUTE_ENABLE', false),
3939
],
4040
'events' => [
4141

42-
/*
42+
/**
4343
* This package emits some events after it getters all translation messages
4444
*
4545
* Here you can change channel on which events will broadcast
@@ -48,53 +48,58 @@
4848
],
4949
'caches' => [
5050

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

56-
/*
56+
/**
5757
* Key name of the cache entry for the localization array
5858
*/
59-
'key' => 'localization.array',
59+
'key' => 'localization.array',
6060

61-
/*
61+
/**
6262
* Timeout of the cached data in minutes - set to 0 to disable
6363
*/
6464
'timeout' => 60,
6565
],
66-
'js' => [
67-
/*
66+
'js' => [
67+
/**
6868
* Default locale for export
6969
*/
7070
'default_locale' => 'en',
7171

72-
/*
72+
/**
7373
* root location to where JavaScript file will be exported
7474
*/
75-
'filepath' => resource_path('assets/js'),
75+
'filepath' => resource_path('assets/js'),
7676

77-
/*
77+
/**
7878
* File name for JavaScript file with exported messages
7979
*/
80-
'filename' => 'll_messages.js',
80+
'filename' => 'll_messages.js',
8181
],
82-
'paths' => [
82+
'paths' => [
8383

84-
/*
84+
/**
8585
* You can export more lang files then just files in resources/lang, for example
8686
*
8787
* In you .env file just add:
8888
* LARAVEL_LOCALIZATION_LANG_DIRS=resources/lang,Modules/Blog/Resources/lang
8989
*/
9090
'lang_dirs' => [resource_path('lang')],
9191
],
92-
/*
93-
* You can customize the regexp for lang files to be able to exclude certain files.
94-
*/
95-
'file_regexp' => [
92+
/**
93+
* You can customize the regexp for lang files to be able to exclude certain files.
94+
*/
95+
'file_regexp' => [
9696
'php' => '/^.+\.php$/i',
9797
'json' => '/^.+\.json$/i',
9898
],
99+
/**
100+
* This function will be called every time after export, it should be globally accessible function (eg. Laravel helper function)
101+
* and it should accept (string) argument
102+
*/
103+
'export_callback' => null,
99104

100105
];

src/routes.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111
/*
1212
* Localization
1313
*/
14-
Route::get(config('laravel-localization.routes.prefix'), function () {
15-
$strings = ExportLocalizations::export()->toArray();
16-
17-
return response()->json($strings);
18-
})->name(config('laravel-localization.routes.name'))
14+
Route::get(config('laravel-localization.routes.prefix'), 'KgBot\LaravelLocalization\Classes\ExportLocalizations@exportToArray')->name(config('laravel-localization.routes.name'))
1915
->middleware(config('laravel-localization.routes.middleware'));
2016
}

0 commit comments

Comments
 (0)