Skip to content

Commit

Permalink
Additional format when exporting localization with php artisan export…
Browse files Browse the repository at this point in the history
…:messages

It now accepts two formats: javascript (which is default), and json
  • Loading branch information
kg-bot committed Aug 21, 2021
1 parent 9887808 commit 00f6985
Showing 1 changed file with 64 additions and 16 deletions.
80 changes: 64 additions & 16 deletions src/Console/Commands/ExportMessages.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@

class ExportMessages extends Command
{
/** @var \Illuminate\Config\Repository|\Illuminate\Contracts\Foundation\Application|mixed */
protected $filepath;

/** @var array */
protected $messages;

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'export:messages';
protected $signature = 'export:messages {format=javascript}';

/**
* The console command description.
Expand All @@ -31,6 +37,9 @@ class ExportMessages extends Command
public function __construct()
{
parent::__construct();

$this->filepath = config('laravel-localization.js.filepath', resource_path('assets/js'));
$this->messages = ExportLocalizations::export()->toArray();
}

/**
Expand All @@ -40,26 +49,65 @@ public function __construct()
*/
public function handle()
{
$messages = ExportLocalizations::export()->toArray();
$format = $this->argument('format');

if($format === 'javascript') {
return $this->toJavaScript();
}
if($format === 'json') {
return $this->toJson();
}

$this->error( "Format {$format} is not currently supported, you can use callback function if you need additional modification of exported array.");

return 1;
}

protected function toJavaScript() {
$filename = config('laravel-localization.js.filename', 'll_messages.js');

$adapter = new Local($this->filepath);
$filesystem = new Filesystem($adapter);

$contents = 'export default '.json_encode($this->messages);

if ($filesystem->has($filename)) {
$filesystem->delete($filename);
$filesystem->write($filename, $contents);
} else {
$filesystem->write($filename, $contents);
}

$this->info('Messages exported to JavaScript file, you can find them at '.$this->filepath.DIRECTORY_SEPARATOR
.$filename);

return 0;
}

protected function toJson() {

foreach ($this->messages as $language_key => $translations) {
foreach ($translations as $translation_key => $translate) {
$filepath = "$this->filepath/$language_key";
$filename = "$translation_key.json";

$filepath = config('laravel-localization.js.filepath', resource_path('assets/js'));
$filename = config('laravel-localization.js.filename', 'll_messages.js');
$adapter = new Local($filepath);
$filesystem = new Filesystem($adapter);

$adapter = new Local($filepath);
$filesystem = new Filesystem($adapter);
$contents = json_encode($translate, JSON_PRETTY_PRINT);

$contents = 'export default '.json_encode($messages);

if ($filesystem->has($filename)) {
$filesystem->delete($filename);
$filesystem->write($filename, $contents);
} else {
$filesystem->write($filename, $contents);
}
if ($filesystem->has($filename)) {
$filesystem->delete($filename);
$filesystem->write($filename, $contents);
} else {
$filesystem->write($filename, $contents);
}
}
}

$this->info('Messages exported to JavaScript file, you can find them at '.$filepath.DIRECTORY_SEPARATOR
.$filename);
$this->info('Messages exported to JSON files, you can find them at '.$this->filepath);

return 0;
return 0;
}
}

0 comments on commit 00f6985

Please sign in to comment.