Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
78.95% |
30 / 38 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| TranslationBundle | |
78.95% |
30 / 38 |
|
33.33% |
1 / 3 |
19.70 | |
0.00% |
0 / 1 |
| __construct | |
78.57% |
11 / 14 |
|
0.00% |
0 / 1 |
8.63 | |||
| getSerial | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| generateTo | |
76.19% |
16 / 21 |
|
0.00% |
0 / 1 |
8.86 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2008-2010 (original work) Deutsche Institut für Internationale Pädagogische Forschung |
| 19 | * (under the project TAO-TRANSFER); |
| 20 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 21 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 22 | * 2013-2017 (update and modification) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 23 | */ |
| 24 | |
| 25 | namespace oat\tao\helpers\translation; |
| 26 | |
| 27 | use common_exception_Error; |
| 28 | use common_exception_InvalidArgumentType; |
| 29 | use common_Logger; |
| 30 | |
| 31 | /** |
| 32 | * This class enables you to generate a bundle of translations, for a language and extensions. |
| 33 | * The bundle contains the translations of all the defined extensions. |
| 34 | * |
| 35 | * @access public |
| 36 | * @author Bertrand Chevrier <bertrand@taotesting.com> |
| 37 | * @package tao |
| 38 | */ |
| 39 | class TranslationBundle |
| 40 | { |
| 41 | /** |
| 42 | * The bundle langCode, formated as a locale: en-US, fr-FR, etc. |
| 43 | * @var string |
| 44 | */ |
| 45 | private $langCode; |
| 46 | |
| 47 | /** |
| 48 | * The list of extensions to generate the bundle for |
| 49 | * @var common_ext_Extension[] |
| 50 | */ |
| 51 | private $extensions; |
| 52 | |
| 53 | /** |
| 54 | * The TAO platform installation base path. |
| 55 | * @var string |
| 56 | */ |
| 57 | private $basePath; |
| 58 | |
| 59 | /** |
| 60 | * The TAO version in use. |
| 61 | * @var string |
| 62 | */ |
| 63 | private $taoVersion; |
| 64 | |
| 65 | /** |
| 66 | * Create a new bundle |
| 67 | * |
| 68 | * $extensions = ['tao', 'taoItems'] |
| 69 | * |
| 70 | * @param string $langCode |
| 71 | * @param common_ext_Extension[] |
| 72 | * @throws \InvalidArgumentException |
| 73 | */ |
| 74 | public function __construct($langCode, $extensions, $basePath, $taoVersion = '') |
| 75 | { |
| 76 | if (!is_string($langCode)) { |
| 77 | throw new \InvalidArgumentException('$langCode argument should be a string.'); |
| 78 | } |
| 79 | if (!is_string($basePath)) { |
| 80 | throw new \InvalidArgumentException('$basePath argument should be a string.'); |
| 81 | } |
| 82 | if (!is_string($taoVersion)) { |
| 83 | throw new \InvalidArgumentException('$taoVersion argument should be a string.'); |
| 84 | } |
| 85 | if (!is_array($extensions)) { |
| 86 | throw new \InvalidArgumentException('$extensions argument should be an array.'); |
| 87 | } |
| 88 | if (empty($langCode) || empty($extensions) || empty($basePath)) { |
| 89 | throw new \InvalidArgumentException('$langCode, $extensions and $basePath arguments should not be empty.'); |
| 90 | } |
| 91 | |
| 92 | $this->langCode = $langCode; |
| 93 | $this->extensions = $extensions; |
| 94 | $this->basePath = rtrim($basePath, '/\\'); |
| 95 | $this->taoVersion = $taoVersion; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Get a deterministic identifier from bundle data: one id for same langCode and extensions |
| 100 | * @return string the identifier |
| 101 | */ |
| 102 | public function getSerial() |
| 103 | { |
| 104 | $ids = $this->extensions; |
| 105 | sort($ids); |
| 106 | return md5($this->langCode . '_' . implode('-', $ids)); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Generates the bundle to the given directory. It will create a json file, named with the langCode: |
| 111 | * {$directory}/{$langCode}.json |
| 112 | * @param string $directory the path |
| 113 | * @return string|false the path of the generated bundle or false |
| 114 | */ |
| 115 | public function generateTo($directory) |
| 116 | { |
| 117 | $translations = []; |
| 118 | |
| 119 | foreach ($this->extensions as $extension) { |
| 120 | $jsFilePath = $this->basePath . '/' . $extension . '/locales/' . $this->langCode . '/messages_po.js'; |
| 121 | if (file_exists($jsFilePath)) { |
| 122 | $translate = json_decode(file_get_contents($jsFilePath), false); |
| 123 | if ($translate != null) { |
| 124 | $translations = array_merge($translations, (array)$translate); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | //the bundle contains as well some translations |
| 129 | $content = [ |
| 130 | 'serial' => $this->getSerial(), |
| 131 | 'date' => time(), |
| 132 | 'translations' => $translations |
| 133 | ]; |
| 134 | |
| 135 | if (!empty($this->taoVersion)) { |
| 136 | $content['version'] = $this->taoVersion; |
| 137 | } |
| 138 | |
| 139 | if (is_dir($directory)) { |
| 140 | if (!is_dir($directory . '/' . $this->langCode)) { |
| 141 | mkdir($directory . '/' . $this->langCode); |
| 142 | } |
| 143 | $file = $directory . '/' . $this->langCode . '/messages.json'; |
| 144 | if (@file_put_contents($file, json_encode($content))) { |
| 145 | return $file; |
| 146 | } |
| 147 | } |
| 148 | return false; |
| 149 | } |
| 150 | } |