Add locale support to extract-strings console

- Remove reliance on available locales setting
- Add reliance on translation path setting
- Only create a new translation when a single language is provided
- Remove debug comments
This commit is contained in:
Hypolite Petovan 2018-11-18 11:35:04 -05:00
parent ce075c80e6
commit 0c3013adeb
2 changed files with 11 additions and 24 deletions

View file

@ -12,18 +12,18 @@ use Gettext\Translations;
class ExtractStrings extends \Asika\SimpleConsole\Console class ExtractStrings extends \Asika\SimpleConsole\Console
{ {
/** /**
* @var array * @var string
*/ */
protected $locales; protected $translationPath;
protected $helpOptions = ['h', 'help', '?']; protected $helpOptions = ['h', 'help', '?'];
public function __construct( public function __construct(
array $locales, string $translationPath,
?array $argv = null ?array $argv = null
) )
{ {
$this->locales = $locales; $this->translationPath = $translationPath;
parent::__construct($argv); parent::__construct($argv);
} }
@ -39,7 +39,7 @@ Description
Extract translation strings Extract translation strings
Options Options
--all Generate po files for all available languages --all Update PO files for all existing languages
--force Generate po file from scratch, discarding existing translations --force Generate po file from scratch, discarding existing translations
-h|--help|-? Show help information -h|--help|-? Show help information
-v Show more debug information. -v Show more debug information.
@ -54,7 +54,7 @@ HELP;
} }
if ($this->getOption('all')) { if ($this->getOption('all')) {
$langs = $this->locales; $langs = array_map('basename', glob(realpath($this->translationPath) . '/*', GLOB_ONLYDIR));
} else { } else {
$lang = $this->getArgument(0); $lang = $this->getArgument(0);
if (!$lang) { if (!$lang) {
@ -63,8 +63,6 @@ HELP;
$langs = [$lang]; $langs = [$lang];
} }
$outputDir = __DIR__ . '/../../../lang';
$dir_iterator = new \RecursiveDirectoryIterator(realpath(__DIR__ . '/../../../'), \FilesystemIterator::SKIP_DOTS); $dir_iterator = new \RecursiveDirectoryIterator(realpath(__DIR__ . '/../../../'), \FilesystemIterator::SKIP_DOTS);
$iterator = new \RecursiveIteratorIterator($dir_iterator, \RecursiveIteratorIterator::SELF_FIRST); $iterator = new \RecursiveIteratorIterator($dir_iterator, \RecursiveIteratorIterator::SELF_FIRST);
@ -88,31 +86,20 @@ HELP;
foreach ($langs as $locale) { foreach ($langs as $locale) {
$existingTranslations = new Translations(); $existingTranslations = new Translations();
$stringsPoFile = $outputDir . '/' . $locale . '/LC_MESSAGES/strings.po'; $stringsPoFile = $this->translationPath . '/' . $locale . '/LC_MESSAGES/strings.po';
if (is_file($stringsPoFile)) { if (is_file($stringsPoFile)) {
if (!$this->getOption('force')) { if (!$this->getOption('force')) {
$this->out('Loading existing ' . $locale . ' translations'); $this->out('Loading existing ' . $locale . ' translations');
$existingTranslations->addFromPoFile($stringsPoFile); $existingTranslations->addFromPoFile($stringsPoFile);
} }
} else { } else {
mkdir(dirname($stringsPoFile), true); $this->out('Creating directory ' . dirname($stringsPoFile));
mkdir(dirname($stringsPoFile), 0755, 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);
$updatedTranslations->setLanguage($locale); $updatedTranslations->setLanguage($locale);
if ($this->getOption('force')) { if ($this->getOption('force') || !is_file($stringsPoFile)) {
$existingTranslations = $updatedTranslations; $existingTranslations = $updatedTranslations;
} else { } else {
$this->out('Merging with existing translations'); $this->out('Merging with existing translations');

View file

@ -10,7 +10,7 @@ class ExtractStrings extends BaseRoute
public function __invoke(array $args) public function __invoke(array $args)
{ {
return (new \Friendica\Directory\Controllers\Console\ExtractStrings( return (new \Friendica\Directory\Controllers\Console\ExtractStrings(
$this->container->get('settings')['i18n']['locales'], $this->container->get('settings')['i18n']['path'],
$args $args
)); ));
} }