generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
66affa7
commit 28a2c74
Showing
10 changed files
with
230 additions
and
128 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
<?php | ||
|
||
namespace Nanuc\MissingTranslation\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\File; | ||
use Illuminate\Support\Str; | ||
use Symfony\Component\Finder\Finder; | ||
|
||
class FindMissingTranslationCommand extends Command | ||
{ | ||
public $signature = 'missing-translation:find {language=en : Language to use}'; | ||
|
||
public $description = 'Find (and correct) missing translations'; | ||
|
||
public function handle() | ||
{ | ||
$path = base_path(); | ||
|
||
$stringKeys = []; | ||
|
||
$functions = [ | ||
'trans', | ||
'trans_choice', | ||
'Lang::get', | ||
'Lang::choice', | ||
'Lang::trans', | ||
'Lang::transChoice', | ||
'@lang', | ||
'@choice', | ||
'__', | ||
'$trans.get', | ||
]; | ||
$toBeTranslatedMarker = '!E!'; | ||
|
||
$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 | ||
"\(". // Match opening parenthesis | ||
"[\'\"]". // Match " or ' | ||
'('. // Start a new group to match: | ||
'[a-zA-Z0-9_-]+'. // Must start with group | ||
"([.](?! )[^\1)]+)+". // Be followed by one or more items/keys | ||
')'. // Close group | ||
"[\'\"]". // Closing quote | ||
"[\),]"; // Close parentheses or new parameter | ||
|
||
$stringPattern = | ||
"[^\w]". // Must not have an alphanum before real method | ||
'('.implode('|', $functions).')'. // Must start with one of the functions | ||
"\(". // Match opening parenthesis | ||
"(?P<quote>['\"])". // Match " or ' and store in {quote} | ||
"(?P<string>(?:\\\k{quote}|(?!\k{quote}).)*)". // Match any string that can be {quote} escaped | ||
"\k{quote}". // Match " or ' previously matched | ||
"[\),]"; // Close parentheses or new parameter | ||
|
||
if (! File::exists($this->langFile())) { | ||
File::put($this->langFile(), json_encode([])); | ||
} | ||
|
||
// Find all PHP + Twig files in the app folder, except for storage | ||
$finder = new Finder(); | ||
$finder->in($path)->exclude('storage')->exclude('vendor')->name('*.php')->name('*.twig')->name('*.vue')->files(); | ||
|
||
/** @var \Symfony\Component\Finder\SplFileInfo $file */ | ||
foreach ($finder as $file) { | ||
if (preg_match_all("/$stringPattern/siU", $file->getContents(), $matches)) { | ||
foreach ($matches['string'] as $key) { | ||
/* | ||
if (preg_match("/(^[a-zA-Z0-9_-]+([.][^\1)\ ]+)+$)/siU", $key, $groupMatches)) { | ||
$this->info(str_replace(base_path(), '', $file->getPathname()).': '.$groupMatches[0]); | ||
// group{.group}.key format, already in $groupKeys but also matched here | ||
// do nothing, it has to be treated as a group | ||
continue; | ||
} | ||
*/ | ||
|
||
//TODO: This can probably be done in the regex, but I couldn't do it. | ||
//skip keys which contain namespacing characters, unless they also contain a | ||
//space, which makes it JSON. | ||
if (! (Str::contains($key, '::') && Str::contains($key, '.')) | ||
|| Str::contains($key, ' ')) { | ||
$stringKeys[] = $key; | ||
} | ||
|
||
if (Str::contains($key, $toBeTranslatedMarker)) { | ||
$translation = $this->ask('Please enter German translation for "'.str_replace($toBeTranslatedMarker, '', $key).'"'); | ||
|
||
$content = $file->getContents(); | ||
$content = str_replace($key, $translation, $content); | ||
File::put($file->getRealPath(), $content); | ||
|
||
$translations = $this->getAvailableStrings(); | ||
$translations[$translation] = str_replace($toBeTranslatedMarker, '', $key); | ||
File::put($this->langFile(), json_encode($translations, JSON_UNESCAPED_SLASHES + JSON_UNESCAPED_UNICODE + JSON_PRETTY_PRINT)); | ||
} | ||
} | ||
} | ||
} | ||
$stringKeys = array_unique($stringKeys); | ||
$availableStrings = $this->getAvailableStrings(); | ||
|
||
$missingTranslations = []; | ||
foreach ($stringKeys as $stringKey) { | ||
if (! in_array($stringKey, array_keys($availableStrings))) { | ||
$missingTranslations[] = $stringKey; | ||
} | ||
} | ||
|
||
$this->info('Found ' . count($missingTranslations) . ' missing translations.'); | ||
|
||
if ($this->confirm('Do you want to display the missing phrases?')) { | ||
foreach ($missingTranslations as $missingTranslation) { | ||
$this->info($missingTranslation); | ||
} | ||
} | ||
|
||
if ($this->confirm('Do you want to translate the missing phrases now?')) { | ||
foreach ($missingTranslations as $missingTranslation) { | ||
$translation = $this->ask('Please enter translation (locale: ' . $this->argument('language') . ') for "' . $missingTranslation . '"'); | ||
|
||
if(strlen($translation) > 0) { | ||
$translations = $this->getAvailableStrings(); | ||
$translations[$missingTranslation] = $translation; | ||
File::put($this->langFile(), json_encode($translations, JSON_UNESCAPED_SLASHES + JSON_UNESCAPED_UNICODE + JSON_PRETTY_PRINT)); | ||
|
||
} | ||
} | ||
} | ||
} | ||
|
||
public function getAvailableStrings() | ||
{ | ||
return json_decode(File::get($this->langFile()), true); | ||
} | ||
|
||
public function langFile() | ||
{ | ||
return resource_path('lang/'.$this->argument('language').'.json'); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Nanuc\MissingTranslation; | ||
|
||
use Illuminate\Translation\TranslationServiceProvider as BaseTranslationServiceProvider; | ||
|
||
class TranslationServiceProvider extends BaseTranslationServiceProvider { | ||
public function register() | ||
{ | ||
$this->registerLoader(); | ||
|
||
$this->app->singleton('translator', function($app) | ||
{ | ||
$loader = $app['translation.loader']; | ||
|
||
// When registering the translator component, we'll need to set the default | ||
// locale as well as the fallback locale. So, we'll grab the application | ||
// configuration so we can easily get both of these values from there. | ||
$locale = $app['config']['app.locale']; | ||
|
||
$trans = new Translator($loader, $locale); | ||
|
||
$trans->setFallback($app['config']['app.fallback_locale']); | ||
|
||
if($app->bound('translation-manager')){ | ||
$trans->setTranslationManager($app['translation-manager']); | ||
} | ||
|
||
return $trans; | ||
}); | ||
} | ||
} |
Oops, something went wrong.