* @package tao */ class TranslationBundle { /** * The bundle langCode, formated as a locale: en-US, fr-FR, etc. * @var string */ private $langCode; /** * The list of extensions to generate the bundle for * @var common_ext_Extension[] */ private $extensions; /** * The TAO platform installation base path. * @var string */ private $basePath; /** * The TAO version in use. * @var string */ private $taoVersion; /** * Create a new bundle * * $extensions = ['tao', 'taoItems'] * * @param string $langCode * @param common_ext_Extension[] * @throws \InvalidArgumentException */ public function __construct($langCode, $extensions, $basePath, $taoVersion = '') { if (!is_string($langCode)) { throw new \InvalidArgumentException('$langCode argument should be a string.'); } if (!is_string($basePath)) { throw new \InvalidArgumentException('$basePath argument should be a string.'); } if (!is_string($taoVersion)) { throw new \InvalidArgumentException('$taoVersion argument should be a string.'); } if (!is_array($extensions)) { throw new \InvalidArgumentException('$extensions argument should be an array.'); } if (empty($langCode) || empty($extensions) || empty($basePath)) { throw new \InvalidArgumentException('$langCode, $extensions and $basePath arguments should not be empty.'); } $this->langCode = $langCode; $this->extensions = $extensions; $this->basePath = rtrim($basePath, '/\\'); $this->taoVersion = $taoVersion; } /** * Get a deterministic identifier from bundle data: one id for same langCode and extensions * @return string the identifier */ public function getSerial() { $ids = $this->extensions; sort($ids); return md5($this->langCode . '_' . implode('-', $ids)); } /** * Generates the bundle to the given directory. It will create a json file, named with the langCode: {$directory}/{$langCode}.json * @param string $directory the path * @return string|false the path of the generated bundle or false */ public function generateTo($directory) { $translations = []; foreach ($this->extensions as $extension) { $jsFilePath = $this->basePath . '/' . $extension . '/locales/' . $this->langCode . '/messages_po.js'; if (file_exists($jsFilePath)) { $translate = json_decode(file_get_contents($jsFilePath), false); if ($translate != null) { $translations = array_merge($translations, (array)$translate); } } } //the bundle contains as well some translations $content = [ 'serial' => $this->getSerial(), 'date' => time(), 'translations' => $translations ]; if (!empty($this->taoVersion)) { $content['version'] = $this->taoVersion; } if (is_dir($directory)) { if (!is_dir($directory . '/' . $this->langCode)) { mkdir($directory . '/' . $this->langCode); } $file = $directory . '/' . $this->langCode . '/messages.json'; if (@file_put_contents($file, json_encode($content))) { return $file; } } return false; } }