DevelopmentLoader.php
1 month ago
JsonFileLoader.php
1 month ago
LoaderCache.php
2 years ago
LoaderInterface.php
1 month ago
JsonFileLoader.php
48 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | */ |
| 9 | namespace Piwik\Translation\Loader; |
| 10 | |
| 11 | use Piwik\Common; |
| 12 | /** |
| 13 | * Loads translations from JSON files. |
| 14 | */ |
| 15 | class JsonFileLoader implements \Piwik\Translation\Loader\LoaderInterface |
| 16 | { |
| 17 | public function load($language, array $directories) |
| 18 | { |
| 19 | if (empty($language)) { |
| 20 | return array(); |
| 21 | } |
| 22 | $translations = array(); |
| 23 | foreach ($directories as $directory) { |
| 24 | $filename = $directory . '/' . $language . '.json'; |
| 25 | if (!file_exists($filename)) { |
| 26 | continue; |
| 27 | } |
| 28 | $translations = array_replace_recursive($translations, $this->loadFile($filename)); |
| 29 | } |
| 30 | return $translations; |
| 31 | } |
| 32 | /** |
| 33 | * @return array<string, array<string, string>> |
| 34 | */ |
| 35 | private function loadFile(string $filename) : array |
| 36 | { |
| 37 | $data = file_get_contents($filename); |
| 38 | $translations = json_decode($data, \true); |
| 39 | if (is_null($translations) && Common::hasJsonErrorOccurred()) { |
| 40 | throw new \Exception(sprintf('Not able to load translation file %s: %s', $filename, Common::getLastJsonError())); |
| 41 | } |
| 42 | if (!is_array($translations)) { |
| 43 | return []; |
| 44 | } |
| 45 | return $translations; |
| 46 | } |
| 47 | } |
| 48 |