Open gboor opened 8 years ago
yes, missing point
Implementation, in case somebody need it as me.
`<?php
namespace App\Console\Commands;
use Illuminate\Console\Command; use Illuminate\Support\Facades\DB;
class ExportResourceCommand extends Command { /**
@var string */ protected $signature = 'trans:export {--group=} {--lang=} {--list} {--test}';
/**
@var string */ protected $description = 'Export database translations back to file system';
public function construct() { parent::construct(); }
private function listFiles() { foreach(RESOURCE_FILES as $key => $value) { $this->info($key . ' - '. $value); } }
private function makeResourseFile($group, $lang = null) { $supported_locales = !empty($lang)?[$lang]:config('app.support_local'); foreach($supported_locales as $locale) { $dir = $locale == 'cn' ? 'zh-CN' : $locale; $file_name = base_path() . '/resources/lang/'.$dir.'/'.$group.'.php'; $f = fopen($file_name, "w+") or die("Unable to open file!"); fwrite($f, "<?php\n"); fwrite($f, "\n"); fwrite($f, "return [\n"); DB::table('translator_translations')->where(['locale' => $locale, 'group' => $group])->orderBy('item', 'asc')->each(function($row) use (&$f) { fwrite($f, sprintf("'%s' => '%s',\n", $row->item, $row->text)); }); fwrite($f, "];\n"); fclose($f); } }
private function testResourseFile($group, $lang = null) { $supported_locales = !empty($lang)?[$lang]:config('app.support_local'); foreach($supported_locales as $locale) { DB::table('translator_translations')->where(['locale' => $lang, 'group' => $group])->orderBy('item', 'desc')->each(function($row) use (&$f) { $this->info(sprintf("'%s' => '%s',\n", $row->item, $row->text)); }); } }
/**
@1dnmr Can you write full code and document how to use it please?
@AnasSafi all code is above. You need create new command: php artisan make:command and paste this code. Then use it like described above: trans:export {--group=} {--lang=} {--list} {--test}
I know there is a command to load the translation files in to the database, but is it possible to facilitate a way to do it the other way around? I.e.; generate language files from a database?
The scenario for this is that I currently have a web app with 2 environments; 1 for translators to test their stuff and 1 production. Ideally, I want to generate the files for production and not install the Waavi/translation package there at all, saving some overhead.