Created
January 11, 2019 00:58
-
-
Save m5lil/2aa213e833473d92eca2013f860f2a3f to your computer and use it in GitHub Desktop.
scans your project resources/view/ and app/ folder to find lang(...) and __(...) functions, then it create keys based on first parameter value and insert into json translation files
This file contains hidden or 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
<?php | |
namespace Translator\Command; | |
use Illuminate\Console\Command; | |
class Translator extends Command | |
{ | |
/** | |
* @var string | |
*/ | |
protected $signature = 'translator:update'; | |
/** | |
* @var string | |
*/ | |
protected $description = 'Search new keys and update translation file'; | |
/** | |
* Execute the console command. | |
* | |
* @return void | |
*/ | |
public function handle() | |
{ | |
$translationKeys = $this->findProjectTranslationsKeys(); | |
$translationFiles = $this->getProjectTranslationFiles(); | |
foreach ($translationFiles as $file) { | |
$translationData = $this->getAlreadyTranslatedKeys($file); | |
$this->line("lang " . str_replace('.json', '', basename($file))); | |
$added = []; | |
foreach ($translationKeys as $key) { | |
if (!isset($translationData[$key])) { | |
$this->warn(" - Added {$key}"); | |
$translationData[$key] = ''; | |
$added[] = $key; | |
} | |
} | |
if ($added) { | |
$this->line("updating file..."); | |
$this->writeNewTranslationFile($file, $translationData); | |
$this->info("done!"); | |
} else { | |
$this->warn("new keys not found for this language"); | |
} | |
$this->line(""); | |
} | |
} | |
/** | |
* @return array | |
*/ | |
private function findProjectTranslationsKeys() | |
{ | |
$allKeys = []; | |
$viewsDirectories = [ app_path(), resource_path('views'), ]; | |
foreach($viewsDirectories as $directory) { | |
$this->getTranslationKeysFromDir($allKeys, $directory); | |
} | |
ksort($allKeys); | |
return $allKeys; | |
} | |
/** | |
* @param array $keys | |
* @param string $dirPath | |
* @param string $fileExt | |
*/ | |
private function getTranslationKeysFromDir(&$keys, $dirPath, $fileExt = 'php') | |
{ | |
$files = glob_recursive("{$dirPath}/*.{$fileExt}", GLOB_BRACE); | |
// function glob_recursive($pattern, $flags = 0) | |
// { | |
// $files = glob($pattern, $flags); | |
// foreach (glob(dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) { | |
// $files = array_merge($files, glob_recursive($dir . '/' . basename($pattern), $flags)); | |
// } | |
// return $files; | |
// } | |
foreach ($files as $file) { | |
$content = $this->getSanitizedContent($file); | |
$this->getTranslationKeysFromFunction($keys, 'lang', $content); | |
$this->getTranslationKeysFromFunction($keys, '__', $content); | |
} | |
} | |
/** | |
* @param array $keys | |
* @param string $functionName | |
* @param string $content | |
*/ | |
private function getTranslationKeysFromFunction(&$keys, $functionName, $content) | |
{ | |
$matches = []; | |
preg_match_all("#{$functionName}\((.*?)\)#", $content, $matches); | |
if (!empty($matches)) { | |
foreach ($matches[1] as $match) { | |
$strings = []; | |
preg_match('#\'(.*?)\'#', str_replace('"', "'", $match), $strings); | |
if (!empty($strings)) { | |
$keys[$strings[1]] = $strings[1]; | |
} | |
} | |
} | |
} | |
/** | |
* @return array | |
*/ | |
private function getProjectTranslationFiles() | |
{ | |
$path = resource_path('lang'); | |
$files = glob("{$path}/*.json", GLOB_BRACE); | |
return $files; | |
} | |
/** | |
* @param string $filePath | |
* @return array | |
*/ | |
private function getAlreadyTranslatedKeys($filePath) | |
{ | |
$current = json_decode(file_get_contents($filePath), true); | |
ksort($current); | |
return $current; | |
} | |
/** | |
* @param string $filePath | |
* @param array $translations | |
*/ | |
private function writeNewTranslationFile($filePath, $translations) | |
{ | |
file_put_contents($filePath, json_encode($translations, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE)); | |
} | |
/** | |
* @param string $filePath | |
* @return string | |
*/ | |
private function getSanitizedContent($filePath) | |
{ | |
return str_replace("\n", ' ', file_get_contents($filePath)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment