Skip to content

Commit 3610fd9

Browse files
committed
Parse multiple lang directories
1 parent ec6494a commit 3610fd9

File tree

3 files changed

+86
-26
lines changed

3 files changed

+86
-26
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ You can export config by running
4040
php artisan vendor:publish --provider=KgBot\LaravelLocalization\LaravelLocalizationServiceProvider --tag=config
4141
```
4242

43+
if you want to parse multiple language directories or some other directory except `resources/lang` you can add special
44+
`.env` variable `LARAVEL_LOCALIZATION_LANG_DIRS`.
45+
46+
It can be just one path or multiple paths, for example
47+
```
48+
LARAVEL_LOCALIZATION_LANG_DIRS=resources/lang,app/Modules/Blog/resources/lang,app/Modules/Products/resources/lang
49+
```
50+
4351
# Usage
4452

4553
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

+57-17
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,24 @@ public function export()
5252
return $this;
5353
}
5454

55-
// Collect language files and build array with translations
56-
$files = $this->findLanguageFiles( resource_path( 'lang' ) );
55+
foreach ( config( 'laravel-localization.paths.lang_dirs' ) as $dir ) {
5756

58-
// Parse translations and create final array
59-
array_walk( $files[ 'lang' ], [ $this, 'parseLangFiles' ] );
60-
array_walk( $files[ 'vendor' ], [ $this, 'parseVendorFiles' ] );
61-
array_walk( $files[ 'json' ], [ $this, 'parseJsonFiles' ] );
57+
try {
58+
59+
// Collect language files and build array with translations
60+
$files = $this->findLanguageFiles( $dir );
61+
62+
// Parse translations and create final array
63+
array_walk( $files[ 'lang' ], [ $this, 'parseLangFiles' ], $dir );
64+
array_walk( $files[ 'vendor' ], [ $this, 'parseVendorFiles' ], $dir );
65+
array_walk( $files[ 'json' ], [ $this, 'parseJsonFiles' ], $dir );
66+
67+
} catch ( \Exception $exception ) {
68+
69+
\Log::critical( 'Can\'t read lang directory ' . $dir . ', error: ' . $exception->getMessage() );
70+
}
71+
72+
}
6273

6374
// Trigger event for final translated array
6475
event( new LaravelLocalizationExported( $this->strings ) );
@@ -112,8 +123,8 @@ protected function findLanguageFiles( $path )
112123
sort( $files );
113124

114125
// Remove full path from items
115-
array_walk( $files, function ( &$item ) {
116-
$item = str_replace( resource_path( 'lang' ), '', $item );
126+
array_walk( $files, function ( &$item ) use ( $path ) {
127+
$item = str_replace( $path, '', $item );
117128
} );
118129

119130
// Fetch non-vendor files from filtered php files
@@ -224,19 +235,29 @@ public function toCollection()
224235
*
225236
* @param string $file
226237
*/
227-
protected function parseLangFiles( $file )
238+
protected function parseLangFiles( $file, $key, $dir )
228239
{
229240
// Base package name without file ending
230241
$packageName = basename( $file, '.php' );
231242

232243
// Get package, language and file contents from language file
233244
// /<language_code>/(<package/)<filename>.php
234245
$language = explode( DIRECTORY_SEPARATOR, $file )[ 1 ];
235-
$fileContents = require resource_path( 'lang' ) . DIRECTORY_SEPARATOR . $file;
246+
$fileContents = require $dir . DIRECTORY_SEPARATOR . $file;
236247

237248
// Check if language already exists in array
238249
if ( array_key_exists( $language, $this->strings ) ) {
239-
$this->strings[ $language ][ $packageName ] = $fileContents;
250+
251+
if ( array_key_exists( $packageName, $this->strings[ $language ] ) ) {
252+
253+
$this->strings[ $language ][ $packageName ] =
254+
array_replace_recursive( (array) $this->strings[ $language ][ $packageName ], (array)
255+
$fileContents );
256+
} else {
257+
258+
$this->strings[ $language ][ $packageName ] = $fileContents;
259+
}
260+
240261
} else {
241262
$this->strings[ $language ] = [
242263
$packageName => $fileContents,
@@ -249,7 +270,7 @@ protected function parseLangFiles( $file )
249270
*
250271
* @param string $file
251272
*/
252-
protected function parseVendorFiles( $file )
273+
protected function parseVendorFiles( $file, $key, $dir )
253274
{
254275
// Base package name without file ending
255276
$packageName = basename( $file, '.php' );
@@ -258,14 +279,26 @@ protected function parseVendorFiles( $file )
258279
// /vendor/<package>/<language_code>/<filename>.php
259280
$package = explode( DIRECTORY_SEPARATOR, $file )[ 2 ];
260281
$language = explode( DIRECTORY_SEPARATOR, $file )[ 3 ];
261-
$fileContents = require resource_path( 'lang' ) . DIRECTORY_SEPARATOR . $file;
282+
$fileContents = require $dir . DIRECTORY_SEPARATOR . $file;
262283

263284
// Check if language already exists in array
264285
if ( array_key_exists( $language, $this->strings ) ) {
265286
// Check if package already exists in language
266287
if ( array_key_exists( $package, $this->strings[ $language ] ) ) {
267-
$this->strings[ $language ][ $package ][ $packageName ] = $fileContents;
288+
289+
if ( array_key_exists( $packageName, $this->strings[ $language ][ $package ] ) ) {
290+
291+
$this->strings[ $language ][ $package ][ $packageName ] =
292+
array_replace_recursive( (array) $this->strings[ $language ][ $package ][ $packageName ],
293+
(array)
294+
$fileContents );
295+
296+
} else {
297+
298+
$this->strings[ $language ][ $package ][ $packageName ] = $fileContents;
299+
}
268300
} else {
301+
269302
$this->strings[ $language ][ $package ] = [ $packageName => $fileContents ];
270303
}
271304
} else {
@@ -279,19 +312,26 @@ protected function parseVendorFiles( $file )
279312
}
280313
}
281314

282-
protected function parseJsonFiles( $file )
315+
protected function parseJsonFiles( $file, $key, $dir )
283316
{
284317
// Base package name without file ending
285318
$language = basename( $file, '.json' );
286319

287320
// Get package, language and file contents from language file
288321
// /<language_code>/(<package/)<filename>.php
289-
$fileContents = json_decode( file_get_contents( resource_path( 'lang' ) . $file ) );
322+
$fileContents = json_decode( file_get_contents( $dir . $file ) );
290323

291324
// Check if language already exists in array
292325
if ( array_key_exists( 'json', $this->strings ) ) {
293326

294-
$this->strings[ 'json' ][ $language ] = $fileContents;
327+
if ( array_key_exists( $language, $this->strings[ 'json' ] ) ) {
328+
329+
$this->strings[ 'json' ][ $language ] =
330+
array_replace_recursive( (array) $this->strings[ 'json' ][ $language ], (array) $fileContents );
331+
332+
} else {
333+
$this->strings[ 'json' ][ $language ] = $fileContents;
334+
}
295335
} else {
296336
$this->strings[ 'json' ] = [
297337

src/config/laravel-localization.php

+21-9
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
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

1919
/*
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

2424
/*
2525
* Middleware used on localization routes.
@@ -28,14 +28,14 @@
2828
*
2929
* Don't use space in .env directive after ,
3030
*/
31-
'middleware' => (env('LARAVEL_LOCALIZATION_MIDDLEWARE')) ?
32-
explode(',', env('LARAVEL_LOCALIZATION_MIDDLEWARE'))
31+
'middleware' => ( env( 'LARAVEL_LOCALIZATION_MIDDLEWARE' ) ) ?
32+
explode( ',', env( 'LARAVEL_LOCALIZATION_MIDDLEWARE' ) )
3333
: [],
3434

3535
/*
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

@@ -44,7 +44,7 @@
4444
*
4545
* Here you can change channel on which events will broadcast
4646
*/
47-
'channel' => env('LARAVEL_LOCALIZATION_EVENTS_CHANNEL', ''),
47+
'channel' => env( 'LARAVEL_LOCALIZATION_EVENTS_CHANNEL', '' ),
4848
],
4949
'caches' => [
5050

@@ -63,7 +63,7 @@
6363
*/
6464
'timeout' => 60,
6565
],
66-
'js' => [
66+
'js' => [
6767
/*
6868
* Default locale for export
6969
*/
@@ -72,12 +72,24 @@
7272
/*
7373
* root location to where JavaScript file will be exported
7474
*/
75-
'filepath' => resource_path('assets/js'),
75+
'filepath' => resource_path( 'assets/js' ),
7676

7777
/*
7878
* File name for JavaScript file with exported messages
7979
*/
80-
'filename' => 'll_messages.js',
80+
'filename' => 'll_messages.js',
81+
],
82+
'paths' => [
83+
84+
/**
85+
* You can export more lang files then just files in resources/lang, for example
86+
*
87+
* In you .env file just add:
88+
* LARAVEL_LOCALIZATION_LANG_DIRS=resources/lang,Modules/Blog/Resources/lang
89+
*/
90+
'lang_dirs' => ( env( 'LARAVEL_LOCALIZATION_LANG_DIRS' ) ) ?
91+
explode( ',', env( 'LARAVEL_LOCALIZATION_LANG_DIRS' ) )
92+
: [ 'resources/lang' ],
8193
],
8294

8395
];

0 commit comments

Comments
 (0)