Skip to content

Commit

Permalink
Advanced json support (#1)
Browse files Browse the repository at this point in the history
Extended json support
  • Loading branch information
tanmuhittin authored Mar 29, 2019
1 parent 8cc756e commit d0e9196
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
48 changes: 21 additions & 27 deletions src/commands/TranslateFilesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class TranslateFilesCommand extends Command
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}';
{--targetfiles= : target files}
{--json : explore translations and use json only}';

/**
* The console command description.
Expand All @@ -31,7 +32,8 @@ class TranslateFilesCommand extends Command
{--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}';
{--targetfiles=file1,file2 : Only translate specific files}
{--json : explore translations and use json only}';

/**
* Create a new command instance.
Expand Down Expand Up @@ -64,11 +66,13 @@ public function handle()
if ($locale == $this->base_locale) {
continue;
}
if (is_dir(resource_path('lang/' . $locale)) && $locale !== 'vendor') {
if($this->option('json')){
$this->translate_json_array_file($locale);
}
else if (is_dir(resource_path('lang/' . $locale)) && $locale !== 'vendor') {
$this->line($this->base_locale . " -> " . $locale . " translating...");
$this->translate_php_array_files($locale);
}
$this->translate_json_array_file($locale);
$bar->advance();
}
$bar->finish();
Expand Down Expand Up @@ -162,18 +166,7 @@ private function translate_json_array_file($locale)
{
$groupKeys = [];
$stringKeys = [];
$functions = [
'trans',
'trans_choice',
'Lang::get',
'Lang::choice',
'Lang::trans',
'Lang::transChoice',
'@lang',
'@choice',
'__',
'$trans.get',
];
$functions = config('laravel_google_translate.trans_functions');
$groupPattern = // See https://regex101.com/r/WEJqdL/6
"[^\w|>]" . // Must not have an alphanum or _ or > before real method
'(' . implode( '|', $functions ) . ')' . // Must start with one of the functions
Expand All @@ -196,6 +189,7 @@ private function translate_json_array_file($locale)
$finder = new Finder();
$finder->in( base_path() )->exclude( 'storage' )->exclude( 'vendor' )->name( '*.php' )->name( '*.twig' )->name( '*.vue' )->files();
/** @var \Symfony\Component\Finder\SplFileInfo $file */
$this->line("Exploring strings...");
foreach ( $finder as $file ) {
// Search the current file for the pattern
if ( preg_match_all( "/$groupPattern/siU", $file->getContents(), $matches ) ) {
Expand All @@ -217,23 +211,23 @@ 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')){
$this->line('Found : '.$key);
}
}
}
}
}
// Remove duplicates
$groupKeys = array_unique( $groupKeys );
$groupKeys = array_unique( $groupKeys ); // todo: not supporting group keys for now add this feature!
$stringKeys = array_unique( $stringKeys );
// Add the translations to the database, if not existing.
/*foreach ( $groupKeys as $key ) {
// Split the group and item
list( $group, $item ) = explode( '.', $key, 2 );
$this->missingKey( '', $group, $item );
}
*/
$this->line('Exploration completed. Let\'s get started');
$new_lang = [];
$json_translations_string = file_get_contents(resource_path('lang/' . $locale . '.json'));
$json_existing_translations = json_decode($json_translations_string, true);
$json_existing_translations = [];
if(file_exists(resource_path('lang/' . $locale . '.json'))){
$json_translations_string = file_get_contents(resource_path('lang/' . $locale . '.json'));
$json_existing_translations = json_decode($json_translations_string, true);
}
foreach ($stringKeys as $to_be_translated){
//check existing translations
if(isset($json_existing_translations[$to_be_translated]) &&
Expand All @@ -246,7 +240,7 @@ private function translate_json_array_file($locale)
}
$new_lang[$to_be_translated] = addslashes(self::translate($to_be_translated, $locale));
if ($this->option('verbose')) {
$this->line($to_be_translated . ' : ' . $new_lang[$key]);
$this->line($to_be_translated . ' : ' . $new_lang[$to_be_translated]);
}
}
$file = fopen(resource_path('lang/' . $locale . '.json'), "w+");
Expand Down
13 changes: 13 additions & 0 deletions src/laravel_google_translate.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
<?php
return [
'google_translate_api_key'=>null,
'trans_functions' => [
'trans',
'trans_choice',
'Lang::get',
'Lang::choice',
'Lang::trans',
'Lang::transChoice',
'@lang',
'@choice',
'__',
'$trans.get',
'$t'
],
];

0 comments on commit d0e9196

Please sign in to comment.