From fc434f450647b08ff9de438bc6440518d5f53d84 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 16 Nov 2018 00:00:19 -0500 Subject: [PATCH] Add extract-strings console --- src/classes/Controllers/Console.php | 1 + .../Controllers/Console/ExtractStrings.php | 135 ++++++++++++++++++ src/classes/Routes/Console/ExtractStrings.php | 17 +++ 3 files changed, 153 insertions(+) create mode 100644 src/classes/Controllers/Console/ExtractStrings.php create mode 100644 src/classes/Routes/Console/ExtractStrings.php diff --git a/src/classes/Controllers/Console.php b/src/classes/Controllers/Console.php index 6d8b2e3..3fdc382 100644 --- a/src/classes/Controllers/Console.php +++ b/src/classes/Controllers/Console.php @@ -31,6 +31,7 @@ class Console extends \Asika\SimpleConsole\Console 'install' => \Friendica\Directory\Routes\Console\Install::class, 'updatedb' => \Friendica\Directory\Routes\Console\UpdateDb::class, 'dbupdate' => \Friendica\Directory\Routes\Console\UpdateDb::class, + 'extract-strings' => \Friendica\Directory\Routes\Console\ExtractStrings::class, ]; public function __construct(\Slim\Container $container, ?array $argv = null) diff --git a/src/classes/Controllers/Console/ExtractStrings.php b/src/classes/Controllers/Console/ExtractStrings.php new file mode 100644 index 0000000..7a387d9 --- /dev/null +++ b/src/classes/Controllers/Console/ExtractStrings.php @@ -0,0 +1,135 @@ + + */ +class ExtractStrings extends \Asika\SimpleConsole\Console +{ + /** + * @var array + */ + protected $locales; + + protected $helpOptions = ['h', 'help', '?']; + + public function __construct( + array $locales, + ?array $argv = null + ) + { + $this->locales = $locales; + + parent::__construct($argv); + } + + protected function getHelp() + { + $help = <<args) > 1) { + throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments'); + } + + if ($this->getOption('all')) { + $langs = $this->locales; + } else { + $lang = $this->getArgument(0); + if (!$lang) { + throw new \RuntimeException('Missing language argument and --all isn\'t provided'); + } + $langs = [$lang]; + } + + $outputDir = __DIR__ . '/../../../lang'; + + $dir_iterator = new \RecursiveDirectoryIterator(realpath(__DIR__ . '/../../../'), \FilesystemIterator::SKIP_DOTS); + $iterator = new \RecursiveIteratorIterator($dir_iterator, \RecursiveIteratorIterator::SELF_FIRST); + + $updatedTranslations = new Translations(); + foreach ($iterator as $file) { + /** + * @var \SplFileInfo $file + */ + $extension = $file->getExtension(); + + if ($extension == 'php' || $extension == 'phtml') { + $translations = Translations::fromPhpCodeFile($file->getPathname()); + if (count($translations)) { + $updatedTranslations->mergeWith($translations); + } + } + } + + $this->out('Compiled up-to-date translations'); + + foreach ($langs as $locale) { + $existingTranslations = new Translations(); + + $stringsPoFile = $outputDir . '/' . $locale . '/LC_MESSAGES/strings.po'; + if (is_file($stringsPoFile)) { + if (!$this->getOption('force')) { + $this->out('Loading existing ' . $locale . ' translations'); + $existingTranslations->addFromPoFile($stringsPoFile); + } + } else { + mkdir(dirname($stringsPoFile), true); + } + +// $existingPoFile = $outputDir . '/' . $locale . '/LC_MESSAGES/existing.po'; +// $existingPoString = $existingTranslations->toPoString(); +// $existingPoString = str_replace(realpath(__DIR__ . '/../../../../') . DIRECTORY_SEPARATOR, '', $existingPoString); +// $this->out('Writing ' . realpath($existingPoFile)); +// file_put_contents($existingPoFile, $existingPoString); + +// $updatedPoFile = $outputDir . '/' . $locale . '/LC_MESSAGES/updated.po'; +// $updatedPoString = $updatedTranslations->toPoString(); +// $updatedPoString = str_replace(realpath(__DIR__ . '/../../../../') . DIRECTORY_SEPARATOR, '', $updatedPoString); +// $this->out('Writing ' . realpath($updatedPoFile)); +// file_put_contents($updatedPoFile, $updatedPoString); + + if ($this->getOption('force')) { + $existingTranslations = $updatedTranslations; + } else { + $this->out('Merging with existing translations'); + + $existingTranslations->mergeWith($updatedTranslations, Merge::ADD | Merge::REMOVE | Merge::REFERENCES_THEIRS); + } + + $existingTranslations->setLanguage($locale); + + $poString = $existingTranslations->toPoString(); + + // Strip absolute path to files + $poString = str_replace(realpath(__DIR__ . '/../../../../') . DIRECTORY_SEPARATOR, '', $poString); + + $this->out('Writing ' . realpath($stringsPoFile)); + file_put_contents($stringsPoFile, $poString); + } + + return 0; + } +} + diff --git a/src/classes/Routes/Console/ExtractStrings.php b/src/classes/Routes/Console/ExtractStrings.php new file mode 100644 index 0000000..2a69796 --- /dev/null +++ b/src/classes/Routes/Console/ExtractStrings.php @@ -0,0 +1,17 @@ + + */ +class ExtractStrings extends BaseRoute +{ + public function __invoke(array $args) + { + return (new \Friendica\Directory\Controllers\Console\ExtractStrings( + $this->container->get('settings')['i18n']['locales'], + $args + )); + } +}