Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load command for DB -> files #52

Open
gboor opened this issue Jun 21, 2016 · 4 comments
Open

Load command for DB -> files #52

gboor opened this issue Jun 21, 2016 · 4 comments

Comments

@gboor
Copy link

gboor commented Jun 21, 2016

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.

@1dnmr
Copy link

1dnmr commented May 29, 2018

yes, missing point

@1dnmr
Copy link

1dnmr commented May 30, 2018

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
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'trans:export {--group=} {--lang=} {--list} {--test}';

/**
 * The console command description.
 *
 * @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));
        });
    }
}

/**
 * Execute the console command.
 *
 * @return mixed
 */
public function handle()
{
    try {
        $list = $this->option('list');
        if (!empty($list)) {
            $this->listFiles();
            return;
        }
        $lang = $this->option('lang');
        $group = $this->option('group');
        if (!empty($group)) {
            $do_test = $this->option('test');
            if (!empty($do_test)) {
                $this->testResourseFile($group, $lang);
            } else {
                $this->makeResourseFile($group, $lang);
            }
        } else {
            $this->info('You should specify group name first. Use trans:export --list to see available groups');
        }
    } catch (\Exception $e) {
        \Log::error($e->getMessage());
        $this->error($e->getMessage());
    }
}

}`

@AnasSafi
Copy link

AnasSafi commented Jul 3, 2018

@1dnmr Can you write full code and document how to use it please?

@1dnmr
Copy link

1dnmr commented Jul 3, 2018

@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}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants