Skip to content

Commit 90c39c6

Browse files
committed
Export messages to flat structure inside ll_messages.js file
1 parent 40e1519 commit 90c39c6

File tree

2 files changed

+80
-10
lines changed

2 files changed

+80
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace KgBot\LaravelLocalization\Console\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use KgBot\LaravelLocalization\Facades\ExportLocalizations;
7+
use League\Flysystem\Adapter\Local;
8+
use League\Flysystem\Filesystem;
9+
10+
class ExportMessagesToFlat extends Command
11+
{
12+
/**
13+
* The name and signature of the console command.
14+
*
15+
* @var string
16+
*/
17+
protected $signature = 'export:messages-flat';
18+
19+
/**
20+
* The console command description.
21+
*
22+
* @var string
23+
*/
24+
protected $description = 'Export all localization messages to JavaScript file flat format, suitable for Lang.js';
25+
26+
/**
27+
* Create a new command instance.
28+
*
29+
* @return void
30+
*/
31+
public function __construct()
32+
{
33+
parent::__construct();
34+
}
35+
36+
/**
37+
* Execute the console command.
38+
*
39+
* @return mixed
40+
*/
41+
public function handle()
42+
{
43+
$messages = ExportLocalizations::export()->toFlat();
44+
45+
$filepath = config( 'laravel-localization.js.filepath', resource_path( 'assets/js' ) );
46+
$filename = config( 'laravel-localization.js.filename', 'll_messages.js' );
47+
48+
$adapter = new Local( $filepath );
49+
$filesystem = new Filesystem( $adapter );
50+
51+
$contents = 'export default ' . json_encode( $messages );
52+
53+
if ( $filesystem->has( $filename ) ) {
54+
55+
$filesystem->delete( $filename );
56+
$filesystem->write( $filename, $contents );
57+
58+
} else {
59+
60+
$filesystem->write( $filename, $contents );
61+
}
62+
63+
$this->info( 'Messages exported to JavaScript file, you can find them at ' . $filepath . DIRECTORY_SEPARATOR
64+
. $filename );
65+
66+
return true;
67+
}
68+
}

src/LaravelLocalizationServiceProvider.php

+12-10
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,37 @@
1111
use Illuminate\Support\ServiceProvider;
1212
use KgBot\LaravelLocalization\Classes\ExportLocalizations;
1313
use KgBot\LaravelLocalization\Console\Commands\ExportMessages;
14+
use KgBot\LaravelLocalization\Console\Commands\ExportMessagesToFlat;
1415

1516
class LaravelLocalizationServiceProvider extends ServiceProvider
1617
{
1718
public function boot()
1819
{
19-
$this->app->bind('export-localization', function () {
20+
$this->app->bind( 'export-localization', function () {
2021
return new ExportLocalizations();
21-
});
22+
} );
2223

2324
/*
2425
* Config
2526
*/
2627
$this->mergeConfigFrom(
27-
__DIR__.'/config/laravel-localization.php', 'laravel-localization'
28+
__DIR__ . '/config/laravel-localization.php', 'laravel-localization'
2829
);
2930

30-
$this->publishes([
31-
__DIR__.'/config/laravel-localization.php' => config_path('laravel-localization.php'),
32-
], 'config');
31+
$this->publishes( [
32+
__DIR__ . '/config/laravel-localization.php' => config_path( 'laravel-localization.php' ),
33+
], 'config' );
3334

3435
/*
3536
* Routes
3637
*/
37-
$this->loadRoutesFrom(__DIR__.'/routes.php');
38+
$this->loadRoutesFrom( __DIR__ . '/routes.php' );
3839

39-
if ($this->app->runningInConsole()) {
40-
$this->commands([
40+
if ( $this->app->runningInConsole() ) {
41+
$this->commands( [
4142
ExportMessages::class,
42-
]);
43+
ExportMessagesToFlat::class,
44+
] );
4345
}
4446
}
4547
}

0 commit comments

Comments
 (0)