feature #14359 [Translation] added FileLoader. (aitboudad)

This PR was merged into the 2.8 branch.

Discussion
----------

[Translation] added FileLoader.

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | ~
| License       | MIT
| Doc PR        | ~

Commits
-------

3694e5e [Translation] added FileLoader.
This commit is contained in:
Fabien Potencier 2015-04-18 12:18:19 +02:00
commit 862bdf1098
8 changed files with 83 additions and 182 deletions

View File

@ -11,9 +11,7 @@
namespace Symfony\Component\Translation\Loader; namespace Symfony\Component\Translation\Loader;
use Symfony\Component\Translation\Exception\InvalidResourceException;
use Symfony\Component\Translation\Exception\NotFoundResourceException; use Symfony\Component\Translation\Exception\NotFoundResourceException;
use Symfony\Component\Config\Resource\FileResource;
/** /**
* CsvFileLoader loads translations from CSV files. * CsvFileLoader loads translations from CSV files.
@ -22,7 +20,7 @@ use Symfony\Component\Config\Resource\FileResource;
* *
* @api * @api
*/ */
class CsvFileLoader extends ArrayLoader class CsvFileLoader extends FileLoader
{ {
private $delimiter = ';'; private $delimiter = ';';
private $enclosure = '"'; private $enclosure = '"';
@ -30,19 +28,9 @@ class CsvFileLoader extends ArrayLoader
/** /**
* {@inheritdoc} * {@inheritdoc}
*
* @api
*/ */
public function load($resource, $locale, $domain = 'messages') protected function loadResource($resource)
{ {
if (!stream_is_local($resource)) {
throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
}
if (!file_exists($resource)) {
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
}
$messages = array(); $messages = array();
try { try {
@ -70,10 +58,7 @@ class CsvFileLoader extends ArrayLoader
} }
} }
$catalogue = parent::load($messages, $locale, $domain); return $messages;
$catalogue->addResource(new FileResource($resource));
return $catalogue;
} }
/** /**

View File

@ -0,0 +1,62 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Translation\Loader;
use Symfony\Component\Translation\Exception\InvalidResourceException;
use Symfony\Component\Translation\Exception\NotFoundResourceException;
use Symfony\Component\Config\Resource\FileResource;
/**
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
*/
abstract class FileLoader extends ArrayLoader
{
/**
* {@inheritdoc}
*/
public function load($resource, $locale, $domain = 'messages')
{
if (!stream_is_local($resource)) {
throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
}
if (!file_exists($resource)) {
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
}
$messages = $this->loadResource($resource);
// empty resource
if (null === $messages) {
$messages = array();
}
// not an array
if (!is_array($messages)) {
throw new InvalidResourceException(sprintf('Unable to load file "%s".', $resource));
}
$catalogue = parent::load($messages, $locale, $domain);
$catalogue->addResource(new FileResource($resource));
return $catalogue;
}
/*
* @param string $resource
*
* @return array
*
* @throws InvalidResourceException If stream content has an invalid format.
*/
abstract protected function loadResource($resource);
}

View File

@ -11,35 +11,18 @@
namespace Symfony\Component\Translation\Loader; namespace Symfony\Component\Translation\Loader;
use Symfony\Component\Translation\Exception\InvalidResourceException;
use Symfony\Component\Translation\Exception\NotFoundResourceException;
use Symfony\Component\Config\Resource\FileResource;
/** /**
* IniFileLoader loads translations from an ini file. * IniFileLoader loads translations from an ini file.
* *
* @author stealth35 * @author stealth35
*/ */
class IniFileLoader extends ArrayLoader class IniFileLoader extends FileLoader
{ {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function load($resource, $locale, $domain = 'messages') protected function loadResource($resource)
{ {
if (!stream_is_local($resource)) { return parse_ini_file($resource, true);
throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
}
if (!file_exists($resource)) {
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
}
$messages = parse_ini_file($resource, true);
$catalogue = parent::load($messages, $locale, $domain);
$catalogue->addResource(new FileResource($resource));
return $catalogue;
} }
} }

View File

@ -12,43 +12,26 @@
namespace Symfony\Component\Translation\Loader; namespace Symfony\Component\Translation\Loader;
use Symfony\Component\Translation\Exception\InvalidResourceException; use Symfony\Component\Translation\Exception\InvalidResourceException;
use Symfony\Component\Translation\Exception\NotFoundResourceException;
use Symfony\Component\Config\Resource\FileResource;
/** /**
* JsonFileLoader loads translations from an json file. * JsonFileLoader loads translations from an json file.
* *
* @author singles * @author singles
*/ */
class JsonFileLoader extends ArrayLoader implements LoaderInterface class JsonFileLoader extends FileLoader
{ {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function load($resource, $locale, $domain = 'messages') protected function loadResource($resource)
{ {
if (!stream_is_local($resource)) {
throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
}
if (!file_exists($resource)) {
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
}
$messages = json_decode(file_get_contents($resource), true); $messages = json_decode(file_get_contents($resource), true);
if (0 < $errorCode = json_last_error()) { if (0 < $errorCode = json_last_error()) {
throw new InvalidResourceException(sprintf('Error parsing JSON - %s', $this->getJSONErrorMessage($errorCode))); throw new InvalidResourceException(sprintf('Error parsing JSON - %s', $this->getJSONErrorMessage($errorCode)));
} }
if (null === $messages) { return $messages;
$messages = array();
}
$catalogue = parent::load($messages, $locale, $domain);
$catalogue->addResource(new FileResource($resource));
return $catalogue;
} }
/** /**

View File

@ -12,13 +12,11 @@
namespace Symfony\Component\Translation\Loader; namespace Symfony\Component\Translation\Loader;
use Symfony\Component\Translation\Exception\InvalidResourceException; use Symfony\Component\Translation\Exception\InvalidResourceException;
use Symfony\Component\Translation\Exception\NotFoundResourceException;
use Symfony\Component\Config\Resource\FileResource;
/** /**
* @copyright Copyright (c) 2010, Union of RAD http://union-of-rad.org (http://lithify.me/) * @copyright Copyright (c) 2010, Union of RAD http://union-of-rad.org (http://lithify.me/)
*/ */
class MoFileLoader extends ArrayLoader class MoFileLoader extends FileLoader
{ {
/** /**
* Magic used for validating the format of a MO file as well as * Magic used for validating the format of a MO file as well as
@ -43,45 +41,13 @@ class MoFileLoader extends ArrayLoader
*/ */
const MO_HEADER_SIZE = 28; const MO_HEADER_SIZE = 28;
public function load($resource, $locale, $domain = 'messages')
{
if (!stream_is_local($resource)) {
throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
}
if (!file_exists($resource)) {
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
}
$messages = $this->parse($resource);
// empty file
if (null === $messages) {
$messages = array();
}
// not an array
if (!is_array($messages)) {
throw new InvalidResourceException(sprintf('The file "%s" must contain a valid mo file.', $resource));
}
$catalogue = parent::load($messages, $locale, $domain);
$catalogue->addResource(new FileResource($resource));
return $catalogue;
}
/** /**
* Parses machine object (MO) format, independent of the machine's endian it * Parses machine object (MO) format, independent of the machine's endian it
* was created on. Both 32bit and 64bit systems are supported. * was created on. Both 32bit and 64bit systems are supported.
* *
* @param resource $resource * {@inheritdoc}
*
* @return array
*
* @throws InvalidResourceException If stream content has an invalid format.
*/ */
private function parse($resource) protected function loadResource($resource)
{ {
$stream = fopen($resource, 'r'); $stream = fopen($resource, 'r');

View File

@ -11,10 +11,6 @@
namespace Symfony\Component\Translation\Loader; namespace Symfony\Component\Translation\Loader;
use Symfony\Component\Translation\Exception\InvalidResourceException;
use Symfony\Component\Translation\Exception\NotFoundResourceException;
use Symfony\Component\Config\Resource\FileResource;
/** /**
* PhpFileLoader loads translations from PHP files returning an array of translations. * PhpFileLoader loads translations from PHP files returning an array of translations.
* *
@ -22,28 +18,13 @@ use Symfony\Component\Config\Resource\FileResource;
* *
* @api * @api
*/ */
class PhpFileLoader extends ArrayLoader class PhpFileLoader extends FileLoader
{ {
/** /**
* {@inheritdoc} * {@inheritdoc}
*
* @api
*/ */
public function load($resource, $locale, $domain = 'messages') protected function loadResource($resource)
{ {
if (!stream_is_local($resource)) { return require $resource;
throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
}
if (!file_exists($resource)) {
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
}
$messages = require $resource;
$catalogue = parent::load($messages, $locale, $domain);
$catalogue->addResource(new FileResource($resource));
return $catalogue;
} }
} }

View File

@ -11,44 +11,12 @@
namespace Symfony\Component\Translation\Loader; namespace Symfony\Component\Translation\Loader;
use Symfony\Component\Translation\Exception\InvalidResourceException;
use Symfony\Component\Translation\Exception\NotFoundResourceException;
use Symfony\Component\Config\Resource\FileResource;
/** /**
* @copyright Copyright (c) 2010, Union of RAD http://union-of-rad.org (http://lithify.me/) * @copyright Copyright (c) 2010, Union of RAD http://union-of-rad.org (http://lithify.me/)
* @copyright Copyright (c) 2012, Clemens Tolboom * @copyright Copyright (c) 2012, Clemens Tolboom
*/ */
class PoFileLoader extends ArrayLoader class PoFileLoader extends FileLoader
{ {
public function load($resource, $locale, $domain = 'messages')
{
if (!stream_is_local($resource)) {
throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
}
if (!file_exists($resource)) {
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
}
$messages = $this->parse($resource);
// empty file
if (null === $messages) {
$messages = array();
}
// not an array
if (!is_array($messages)) {
throw new InvalidResourceException(sprintf('The file "%s" must contain a valid po file.', $resource));
}
$catalogue = parent::load($messages, $locale, $domain);
$catalogue->addResource(new FileResource($resource));
return $catalogue;
}
/** /**
* Parses portable object (PO) format. * Parses portable object (PO) format.
* *
@ -90,11 +58,9 @@ class PoFileLoader extends ArrayLoader
* *
* Items with an empty id are ignored. * Items with an empty id are ignored.
* *
* @param resource $resource * {@inheritdoc}
*
* @return array
*/ */
private function parse($resource) protected function loadResource($resource)
{ {
$stream = fopen($resource, 'r'); $stream = fopen($resource, 'r');

View File

@ -12,8 +12,6 @@
namespace Symfony\Component\Translation\Loader; namespace Symfony\Component\Translation\Loader;
use Symfony\Component\Translation\Exception\InvalidResourceException; use Symfony\Component\Translation\Exception\InvalidResourceException;
use Symfony\Component\Translation\Exception\NotFoundResourceException;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Yaml\Parser as YamlParser; use Symfony\Component\Yaml\Parser as YamlParser;
use Symfony\Component\Yaml\Exception\ParseException; use Symfony\Component\Yaml\Exception\ParseException;
@ -24,25 +22,15 @@ use Symfony\Component\Yaml\Exception\ParseException;
* *
* @api * @api
*/ */
class YamlFileLoader extends ArrayLoader class YamlFileLoader extends FileLoader
{ {
private $yamlParser; private $yamlParser;
/** /**
* {@inheritdoc} * {@inheritdoc}
*
* @api
*/ */
public function load($resource, $locale, $domain = 'messages') protected function loadResource($resource)
{ {
if (!stream_is_local($resource)) {
throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
}
if (!file_exists($resource)) {
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
}
if (null === $this->yamlParser) { if (null === $this->yamlParser) {
$this->yamlParser = new YamlParser(); $this->yamlParser = new YamlParser();
} }
@ -53,19 +41,6 @@ class YamlFileLoader extends ArrayLoader
throw new InvalidResourceException(sprintf('Error parsing YAML, invalid file "%s"', $resource), 0, $e); throw new InvalidResourceException(sprintf('Error parsing YAML, invalid file "%s"', $resource), 0, $e);
} }
// empty file return $messages;
if (null === $messages) {
$messages = array();
}
// not an array
if (!is_array($messages)) {
throw new InvalidResourceException(sprintf('The file "%s" must contain a YAML array.', $resource));
}
$catalogue = parent::load($messages, $locale, $domain);
$catalogue->addResource(new FileResource($resource));
return $catalogue;
} }
} }