Giter Club home page Giter Club logo

Comments (4)

1dnmr avatar 1dnmr commented on May 18, 2024

yes, missing point

from translation.

1dnmr avatar 1dnmr commented on May 18, 2024

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());
    }
}

}`

from translation.

AnasSafi avatar AnasSafi commented on May 18, 2024

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

from translation.

1dnmr avatar 1dnmr commented on May 18, 2024

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

from translation.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.