Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[Translation] added FileLoader.
  • Loading branch information
aitboudad committed Apr 15, 2015
1 parent 222701f commit 3694e5e
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 182 deletions.
21 changes: 3 additions & 18 deletions src/Symfony/Component/Translation/Loader/CsvFileLoader.php
Expand Up @@ -11,9 +11,7 @@

namespace Symfony\Component\Translation\Loader;

use Symfony\Component\Translation\Exception\InvalidResourceException;
use Symfony\Component\Translation\Exception\NotFoundResourceException;
use Symfony\Component\Config\Resource\FileResource;

/**
* CsvFileLoader loads translations from CSV files.
Expand All @@ -22,27 +20,17 @@
*
* @api
*/
class CsvFileLoader extends ArrayLoader
class CsvFileLoader extends FileLoader
{
private $delimiter = ';';
private $enclosure = '"';
private $escape = '\\';

/**
* {@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();

try {
Expand Down Expand Up @@ -70,10 +58,7 @@ public function load($resource, $locale, $domain = 'messages')
}
}

$catalogue = parent::load($messages, $locale, $domain);
$catalogue->addResource(new FileResource($resource));

return $catalogue;
return $messages;
}

/**
Expand Down
62 changes: 62 additions & 0 deletions src/Symfony/Component/Translation/Loader/FileLoader.php
@@ -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);
}
23 changes: 3 additions & 20 deletions src/Symfony/Component/Translation/Loader/IniFileLoader.php
Expand Up @@ -11,35 +11,18 @@

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.
*
* @author stealth35
*/
class IniFileLoader extends ArrayLoader
class IniFileLoader extends FileLoader
{
/**
* {@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 = parse_ini_file($resource, true);

$catalogue = parent::load($messages, $locale, $domain);
$catalogue->addResource(new FileResource($resource));

return $catalogue;
return parse_ini_file($resource, true);
}
}
23 changes: 3 additions & 20 deletions src/Symfony/Component/Translation/Loader/JsonFileLoader.php
Expand Up @@ -12,43 +12,26 @@
namespace Symfony\Component\Translation\Loader;

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.
*
* @author singles
*/
class JsonFileLoader extends ArrayLoader implements LoaderInterface
class JsonFileLoader extends FileLoader
{
/**
* {@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);

if (0 < $errorCode = json_last_error()) {
throw new InvalidResourceException(sprintf('Error parsing JSON - %s', $this->getJSONErrorMessage($errorCode)));
}

if (null === $messages) {
$messages = array();
}

$catalogue = parent::load($messages, $locale, $domain);
$catalogue->addResource(new FileResource($resource));

return $catalogue;
return $messages;
}

/**
Expand Down
40 changes: 3 additions & 37 deletions src/Symfony/Component/Translation/Loader/MoFileLoader.php
Expand Up @@ -12,13 +12,11 @@
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/)
*/
class MoFileLoader extends ArrayLoader
class MoFileLoader extends FileLoader
{
/**
* Magic used for validating the format of a MO file as well as
Expand All @@ -43,45 +41,13 @@ class MoFileLoader extends ArrayLoader
*/
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
* was created on. Both 32bit and 64bit systems are supported.
*
* @param resource $resource
*
* @return array
*
* @throws InvalidResourceException If stream content has an invalid format.
* {@inheritdoc}
*/
private function parse($resource)
protected function loadResource($resource)
{
$stream = fopen($resource, 'r');

Expand Down
25 changes: 3 additions & 22 deletions src/Symfony/Component/Translation/Loader/PhpFileLoader.php
Expand Up @@ -11,39 +11,20 @@

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.
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @api
*/
class PhpFileLoader extends ArrayLoader
class PhpFileLoader extends FileLoader
{
/**
* {@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 = require $resource;

$catalogue = parent::load($messages, $locale, $domain);
$catalogue->addResource(new FileResource($resource));

return $catalogue;
return require $resource;
}
}
40 changes: 3 additions & 37 deletions src/Symfony/Component/Translation/Loader/PoFileLoader.php
Expand Up @@ -11,44 +11,12 @@

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) 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.
*
Expand Down Expand Up @@ -90,11 +58,9 @@ public function load($resource, $locale, $domain = 'messages')
*
* Items with an empty id are ignored.
*
* @param resource $resource
*
* @return array
* {@inheritdoc}
*/
private function parse($resource)
protected function loadResource($resource)
{
$stream = fopen($resource, 'r');

Expand Down

0 comments on commit 3694e5e

Please sign in to comment.