Skip to content

Commit

Permalink
Interactive cli (#3)
Browse files Browse the repository at this point in the history
too much options, add interactive cli
  • Loading branch information
tanmuhittin authored Mar 29, 2019
1 parent d7cc8c4 commit a966502
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 34 deletions.
9 changes: 1 addition & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,7 @@ Then you can run
```console
php artisan translate:files
```
or specify base locale, excluded files, target locales. If you like to see the translated texts use --verbose option. --force option enables overwrites to existing target files.
```console
php artisan translate:files --baselocale=tr --exclude=auth,passwords --targetlocales=en,de --verbose --force --targetfiles=test.php,test2.php
```
Explore texts and use json file for translations
```console
php artisan translate:files --baselocale=tr --exclude=auth,passwords --targetlocales=en,de --verbose --force --targetfiles=test.php,test2.php
```

## potential issues

### SSL certificate problem: unable to get local issuer certificate
Expand Down
79 changes: 53 additions & 26 deletions src/commands/TranslateFilesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,22 @@ class TranslateFilesCommand extends Command
public $base_locale;
public $excluded_files;
public $target_files;
public $json;
public $force;
public $verbose;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'translate:files {--baselocale=en : Set the base locale. default is en}
{--exclude=auth,pagination,validation,passwords : comma separated list of excluded files. default is auth,pagination,passwords,validation}
{--targetlocales= : comma separated list of target locales} {--force : Force to overwrite target locale files}
{--targetfiles= : target files}
{--json : explore translations and use json only}';
protected $signature = 'translate:files';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Translate Translation files. translate:files {--baselocale=en : Set the base locale. default is en}
{--exclude=auth,pagination,validation,passwords : comma separated list of excluded files. default is auth,pagination,passwords,validation}
{--targetlocales=tr,de : comma separated list of target locales}
{--verbose : Verbose each translation}
{--force : Force to overwrite target locale files}
{--targetfiles=file1,file2 : Only translate specific files}
{--json : explore translations and use json only}';
protected $description = 'Translate Translation files. translate:files';

/**
* Create a new command instance.
Expand All @@ -52,23 +45,50 @@ public function __construct()
*/
public function handle()
{
$this->base_locale = $this->option('baselocale');
$this->base_locale = $this->ask('What is base locale?','en');
$target_locales = array_filter(explode(",", $this->ask('What are the target locales? Comma seperate each lang key','tr,it')));
$should_force = $this->choice('Force overwrite existing translations?',['No','Yes'],'No');
$this->force = false;
if($should_force === 'Yes'){
$this->force = true;
}
$mode = $this->choice('Use text exploration and json translation or php files?',['json','php'],'php');
$this->json = false;
if($mode === 'json'){
$this->json = true;
}
if(!$this->json){
$this->target_files = array_filter(explode(",", $this->ask('Are there specific target files to translate only? ex: file1,file2','')));
foreach ($this->target_files as $key=>$target_file){
$this->target_files[$key] = $target_file.'.php';
}
$this->excluded_files = array_filter(explode(",", $this->ask('Are there specific files to exclude?','auth,pagination,validation,passwords')));
}
$should_verbose = $this->choice('Verbose each translation?',['No','Yes'],'No');
$this->verbose = false;
if($should_verbose === 'Yes'){
$this->verbose = true;
}
/*$this->base_locale = $this->option('baselocale');
$this->target_files = array_filter(explode(",", $this->option('targetfiles')));
$this->excluded_files = explode(",", $this->option('exclude'));
$target_locales = array_filter(explode(",", $this->option('targetlocales')));
$target_locales = array_filter(explode(",", $this->option('targetlocales')));*/
if (count($target_locales) > 0) {
$this->locales = $target_locales;
}
$bar = $this->output->createProgressBar((count($this->locales) - 1));
$bar->start();
// loop target locales
$this->line("");
if($this->json){
$stringKeys = $this->explore_strings();
}
foreach ($this->locales as $locale) {
if ($locale == $this->base_locale) {
continue;
}
if($this->option('json')){
$this->translate_json_array_file($locale);
if($this->json){
$this->translate_json_array_file($locale,$stringKeys);
}
else if (is_dir(resource_path('lang/' . $locale)) && $locale !== 'vendor') {
$this->line($this->base_locale . " -> " . $locale . " translating...");
Expand Down Expand Up @@ -165,15 +185,15 @@ private function translate_php_array_files($locale)
$to_be_translateds = trans($file, [], $this->base_locale);
$new_lang = [];
foreach ($to_be_translateds as $key => $to_be_translated) {
if (isset($already_translateds[$key]) && $already_translateds[$key] != '' && !$this->option('force')) {
if (isset($already_translateds[$key]) && $already_translateds[$key] != '' && !$this->force) {
$new_lang[$key] = $already_translateds[$key];
if ($this->option('verbose')) {
if ($this->verbose) {
$this->line('Exists Skipping -> ' . $to_be_translated . ' : ' . $new_lang[$key]);
}
continue;
}
$new_lang[$key] = addslashes(self::translate($to_be_translated, $locale));
if ($this->option('verbose')) {
if ($this->verbose) {
$this->line($to_be_translated . ' : ' . $new_lang[$key]);
}
}
Expand All @@ -187,11 +207,9 @@ private function translate_php_array_files($locale)
}

/**
* @param $locale
* @throws \Exception
* @return array
*/
private function translate_json_array_file($locale)
{
private function explore_strings(){
$groupKeys = [];
$stringKeys = [];
$functions = config('laravel_google_translate.trans_functions');
Expand Down Expand Up @@ -239,7 +257,7 @@ private function translate_json_array_file($locale)
if ( !( mb_strpos( $key, '::' ) !== FALSE && mb_strpos( $key, '.' ) !== FALSE )
|| mb_strpos( $key, ' ' ) !== FALSE ) {
$stringKeys[] = $key;
if($this->option('verbose')){
if($this->verbose){
$this->line('Found : '.$key);
}
}
Expand All @@ -249,6 +267,15 @@ private function translate_json_array_file($locale)
// Remove duplicates
$groupKeys = array_unique( $groupKeys ); // todo: not supporting group keys for now add this feature!
$stringKeys = array_unique( $stringKeys );
return $stringKeys;
}

/**
* @param $locale
* @throws \Exception
*/
private function translate_json_array_file($locale,$stringKeys)
{
$this->line('Exploration completed. Let\'s get started');
$new_lang = [];
$json_existing_translations = [];
Expand All @@ -260,14 +287,14 @@ private function translate_json_array_file($locale)
//check existing translations
if(isset($json_existing_translations[$to_be_translated]) &&
$json_existing_translations[$to_be_translated]!='' &&
!$this->option('force'))
!$this->force)
{
$new_lang[$to_be_translated] = $json_existing_translations[$to_be_translated];
$this->line('Exists Skipping -> ' . $to_be_translated . ' : ' . $new_lang[$to_be_translated]);
continue;
}
$new_lang[$to_be_translated] = addslashes(self::translate($to_be_translated, $locale));
if ($this->option('verbose')) {
if ($this->verbose) {
$this->line($to_be_translated . ' : ' . $new_lang[$to_be_translated]);
}
}
Expand Down

0 comments on commit a966502

Please sign in to comment.