4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Translation\Loader;
14 use Symfony\Component\Translation\Exception\InvalidResourceException;
17 * JsonFileLoader loads translations from an json file.
21 class JsonFileLoader extends FileLoader
26 protected function loadResource($resource)
29 if ($data = file_get_contents($resource)) {
30 $messages = json_decode($data, true);
32 if (0 < $errorCode = json_last_error()) {
33 throw new InvalidResourceException(sprintf('Error parsing JSON - %s', $this->getJSONErrorMessage($errorCode)));
41 * Translates JSON_ERROR_* constant into meaningful message.
43 * @param int $errorCode Error code returned by json_last_error() call
45 * @return string Message string
47 private function getJSONErrorMessage($errorCode)
50 case JSON_ERROR_DEPTH:
51 return 'Maximum stack depth exceeded';
52 case JSON_ERROR_STATE_MISMATCH:
53 return 'Underflow or the modes mismatch';
54 case JSON_ERROR_CTRL_CHAR:
55 return 'Unexpected control character found';
56 case JSON_ERROR_SYNTAX:
57 return 'Syntax error, malformed JSON';
59 return 'Malformed UTF-8 characters, possibly incorrectly encoded';
61 return 'Unknown error';