'en', 'de' => 'de', ]; function __construct() { $this->init(); } protected function init() { $this->path = __DIR__; $this->langPath = realpath(__DIR__ . '/../site/languages'); $this->initAutoloader(); } public function sync() { /** @var array [poName => ['msgId', ...], ...] */ $allFileWords = []; /** @var array [lang => ['poName', ...], ...] */ $langFileList = []; /** @var array [lang => [poName => Sepia\PoParser, ...], ...] */ $langFileParser = []; $poList = $this->getPOFileList($this->langPath . '/en'); foreach ($this->getLangList() as $lang) { $langPath = $this->langPath . '/' . $lang; // $poList = $this->getPOFileList($langPath); $langFileList[$lang] = $poList; foreach ($poList as $poName) { $poFilename = $langPath . '/' . $poName; file_exists($poFilename) or touch($poFilename); /** @var \Sepia\PoParser $parser */ $parser = Sepia\PoParser::parseFile($poFilename); $parser->filename = $poFilename; $langFileParser[$lang][$poName] = $parser; $entries = $parser->getEntries(); $entryKeys = array_keys($entries); $allFileWords[$poName] = (empty($allFileWords[$poName])) ? $entryKeys : array_merge($allFileWords[$poName], $entryKeys); } } foreach ($this->getLangList() as $lang) { foreach ($langFileList[$lang] as $poName) { /** @var \Sepia\PoParser $parser */ $parser = $langFileParser[$lang][$poName]; $entries = $parser->getEntries(); if (($diff = array_diff($allFileWords[$poName], array_keys($entries)))) { foreach ($diff as $msgId) { echo "adding '{$msgId}' to '{$lang}/{$poName}'\n"; $parser->setEntry($msgId, [ 'msgid' => $msgId, 'msgstr' => ('en' === $lang ? '' : $this->langPrefix[$lang] . ' ') . $msgId, ]); } echo "saving '{$lang}/{$poName}'\n"; $parser->writeFile($parser->filename); } } } } protected function getLangList() { if (null === $this->langList) { $this->langList = []; if (($list = scandir($this->langPath))) { foreach ($list as $name) { if ('.' !== $name[0] && is_dir($this->langPath . '/' . $name)) { $this->langList[] = $name; } } } } return $this->langList; } protected function getPOFileList($path) { $list = []; if (($rawList = scandir($path))) { foreach ($rawList as $name) { if ('.' !== $name[0] && false !== ($dotIdx = strrpos($name, '.')) && substr($name, $dotIdx) === '.po') { $list[] = $name; } } } return $list; } protected function initAutoloader() { $classPath = $this->path . '/lib/PHP-po-parser'; spl_autoload_register(function ($class) use ($classPath) { $class = str_replace('\\', '/', $class); $path = "{$classPath}/{$class}.php"; if (file_exists($path)) { require $path; } }); return $this; } } $sync = new PoSync(); $sync->sync();