ExportLanguages.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. class ExportLanguages extends Command
  5. {
  6. /**
  7. * The name and signature of the console command.
  8. *
  9. * @var string
  10. */
  11. protected $signature = 'i18n:export';
  12. /**
  13. * The console command description.
  14. *
  15. * @var string
  16. */
  17. protected $description = 'Build and export js localization files.';
  18. /**
  19. * Create a new command instance.
  20. *
  21. * @return void
  22. */
  23. public function __construct()
  24. {
  25. parent::__construct();
  26. }
  27. /**
  28. * Execute the console command.
  29. *
  30. * @return int
  31. */
  32. public function handle()
  33. {
  34. if(config('app.env') !== 'local') {
  35. $this->error('This command is meant for development purposes and should only be run in a local environment');
  36. return Command::FAILURE;
  37. }
  38. $path = base_path('resources/lang');
  39. $langs = [];
  40. foreach (new \DirectoryIterator($path) as $io) {
  41. $name = $io->getFilename();
  42. $skip = ['vendor'];
  43. if($io->isDot() || in_array($name, $skip)) {
  44. continue;
  45. }
  46. if($io->isDir()) {
  47. array_push($langs, $name);
  48. }
  49. }
  50. $exportDir = resource_path('assets/js/i18n/');
  51. $exportDirAlt = public_path('_lang/');
  52. foreach($langs as $lang) {
  53. $strings = \Lang::get('web', [], $lang);
  54. $json = json_encode($strings, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  55. $path = "{$exportDir}{$lang}.json";
  56. file_put_contents($path, $json);
  57. $pathAlt = "{$exportDirAlt}{$lang}.json";
  58. file_put_contents($pathAlt, $json);
  59. }
  60. return Command::SUCCESS;
  61. }
  62. }