feature #9852 [Translation] Added template for relative file paths in FileDumper (florianv)

This PR was merged into the 2.5-dev branch.

Discussion
----------

[Translation] Added template for relative file paths in FileDumper

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | https://github.com/symfony/symfony/issues/9845
| License       | MIT
| Doc PR        |

Added ability to support templates for relative paths to translation files.
The file dumpers will try to create the directories if not existing.
Also made `IcuResFileDumper` extending `FileDumper`.

Commits
-------

a04175e Changed placeholders
623d149 Added a ConcreteDumper
84f0902 [Translation] Added template for relative file paths
This commit is contained in:
Fabien Potencier 2014-03-03 18:46:58 +01:00
commit c4b8e03332
5 changed files with 121 additions and 31 deletions

View File

@ -1,6 +1,12 @@
CHANGELOG
=========
2.5.0
-----
* added relative file path template to the file dumpers
* changed IcuResFileDumper to extend FileDumper
2.3.0
-----

View File

@ -24,6 +24,23 @@ use Symfony\Component\Translation\MessageCatalogue;
*/
abstract class FileDumper implements DumperInterface
{
/**
* A template for the relative paths to files.
*
* @var string
*/
protected $relativePathTemplate = '%domain%.%locale%.%extension%';
/**
* Sets the template for the relative paths to files.
*
* @param string $relativePathTemplate A template for the relative paths to files
*/
public function setRelativePathTemplate($relativePathTemplate)
{
$this->relativePathTemplate = $relativePathTemplate;
}
/**
* {@inheritDoc}
*/
@ -35,11 +52,15 @@ abstract class FileDumper implements DumperInterface
// save a file for each domain
foreach ($messages->getDomains() as $domain) {
$file = $domain.'.'.$messages->getLocale().'.'.$this->getExtension();
// backup
$fullpath = $options['path'].'/'.$file;
$fullpath = $options['path'].'/'.$this->getRelativePath($domain, $messages->getLocale());
if (file_exists($fullpath)) {
copy($fullpath, $fullpath.'~');
} else {
$directory = dirname($fullpath);
if (!file_exists($directory) && !@mkdir($directory, 0777, true)) {
throw new \RuntimeException(sprintf('Cannot create the directory "%s"', $directory));
}
}
// save file
file_put_contents($fullpath, $this->format($messages, $domain));
@ -62,4 +83,21 @@ abstract class FileDumper implements DumperInterface
* @return string file extension
*/
abstract protected function getExtension();
/**
* Gets the relative file path using the template.
*
* @param string $domain The domain
* @param string $locale The locale
*
* @return string The relative file path
*/
private function getRelativePath($domain, $locale)
{
return strtr($this->relativePathTemplate, array(
'%domain%' => $domain,
'%locale%' => $locale,
'%extension%' => $this->getExtension()
));
}
}

View File

@ -18,35 +18,12 @@ use Symfony\Component\Translation\MessageCatalogue;
*
* @author Stealth35
*/
class IcuResFileDumper implements DumperInterface
class IcuResFileDumper extends FileDumper
{
/**
* {@inheritDoc}
*/
public function dump(MessageCatalogue $messages, $options = array())
{
if (!array_key_exists('path', $options)) {
throw new \InvalidArgumentException('The file dumper need a path options.');
}
// save a file for each domain
foreach ($messages->getDomains() as $domain) {
$file = $messages->getLocale().'.'.$this->getExtension();
$path = $options['path'].'/'.$domain.'/';
if (!file_exists($path)) {
mkdir($path);
}
// backup
if (file_exists($path.$file)) {
copy($path.$file, $path.$file.'~');
}
// save file
file_put_contents($path.$file, $this->format($messages, $domain));
}
}
protected $relativePathTemplate = '%domain%/%locale%.%extension%';
/**
* {@inheritDoc}

View File

@ -0,0 +1,70 @@
<?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\Tests\Dumper;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Dumper\FileDumper;
class FileDumperTest extends \PHPUnit_Framework_TestCase
{
public function testDumpBackupsFileIfExisting()
{
$tempDir = sys_get_temp_dir();
$file = $tempDir.'/messages.en.concrete';
$backupFile = $file.'~';
@touch($file);
$catalogue = new MessageCatalogue('en');
$catalogue->add(array('foo' => 'bar'));
$dumper = new ConcreteFileDumper();
$dumper->dump($catalogue, array('path' => $tempDir));
$this->assertTrue(file_exists($backupFile));
@unlink($file);
@unlink($backupFile);
}
public function testDumpCreatesNestedDirectoriesAndFile()
{
$tempDir = sys_get_temp_dir();
$translationsDir = $tempDir.'/test/translations';
$file = $translationsDir.'/messages.en.concrete';
$catalogue = new MessageCatalogue('en');
$catalogue->add(array('foo' => 'bar'));
$dumper = new ConcreteFileDumper();
$dumper->setRelativePathTemplate('test/translations/%domain%.%locale%.%extension%');
$dumper->dump($catalogue, array('path' => $tempDir));
$this->assertTrue(file_exists($file));
@unlink($file);
@rmdir($translationsDir);
}
}
class ConcreteFileDumper extends FileDumper
{
protected function format(MessageCatalogue $messages, $domain)
{
return '';
}
protected function getExtension()
{
return 'concrete';
}
}

View File

@ -26,14 +26,13 @@ class IcuResFileDumperTest extends \PHPUnit_Framework_TestCase
$catalogue->add(array('foo' => 'bar'));
$tempDir = sys_get_temp_dir() . '/IcuResFileDumperTest';
mkdir($tempDir);
$dumper = new IcuResFileDumper();
$dumper->dump($catalogue, array('path' => $tempDir));
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resourcebundle/res/en.res'), file_get_contents($tempDir.'/messages/en.res'));
unlink($tempDir.'/messages/en.res');
rmdir($tempDir.'/messages');
rmdir($tempDir);
@unlink($tempDir.'/messages/en.res');
@rmdir($tempDir.'/messages');
@rmdir($tempDir);
}
}