Skip to content

Commit 84f0902

Browse files
committed
[Translation] Added template for relative file paths
1 parent 2a15923 commit 84f0902

File tree

6 files changed

+143
-28
lines changed

6 files changed

+143
-28
lines changed

src/Symfony/Component/Translation/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
2.5.0
5+
-----
6+
7+
* added relative file path template to the file dumpers
8+
* changed IcuResFileDumper to extend FileDumper
9+
410
2.3.0
511
-----
612

src/Symfony/Component/Translation/Dumper/FileDumper.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@
2424
*/
2525
abstract class FileDumper implements DumperInterface
2626
{
27+
/**
28+
* A template for the relative paths to files.
29+
*
30+
* @var string
31+
*/
32+
protected $relativePathTemplate = '{domain}.{locale}.{extension}';
33+
34+
/**
35+
* Sets the template for the relative paths to files.
36+
*
37+
* @param string $relativePathTemplate A template for the relative paths to files
38+
*/
39+
public function setRelativePathTemplate($relativePathTemplate)
40+
{
41+
$this->relativePathTemplate = $relativePathTemplate;
42+
}
43+
2744
/**
2845
* {@inheritDoc}
2946
*/
@@ -35,11 +52,15 @@ public function dump(MessageCatalogue $messages, $options = array())
3552

3653
// save a file for each domain
3754
foreach ($messages->getDomains() as $domain) {
38-
$file = $domain.'.'.$messages->getLocale().'.'.$this->getExtension();
3955
// backup
40-
$fullpath = $options['path'].'/'.$file;
56+
$fullpath = $options['path'].'/'.$this->getRelativePath($domain, $messages->getLocale());
4157
if (file_exists($fullpath)) {
4258
copy($fullpath, $fullpath.'~');
59+
} else {
60+
$directory = dirname($fullpath);
61+
if (!file_exists($directory) && !@mkdir($directory, 0777, true)) {
62+
throw new \RuntimeException(sprintf('Cannot create the directory "%s"', $directory));
63+
}
4364
}
4465
// save file
4566
file_put_contents($fullpath, $this->format($messages, $domain));
@@ -62,4 +83,21 @@ abstract protected function format(MessageCatalogue $messages, $domain);
6283
* @return string file extension
6384
*/
6485
abstract protected function getExtension();
86+
87+
/**
88+
* Gets the relative file path using the template.
89+
*
90+
* @param string $domain The domain
91+
* @param string $locale The locale
92+
*
93+
* @return string The relative file path
94+
*/
95+
private function getRelativePath($domain, $locale)
96+
{
97+
return strtr($this->relativePathTemplate, array(
98+
'{domain}' => $domain,
99+
'{locale}' => $locale,
100+
'{extension}' => $this->getExtension()
101+
));
102+
}
65103
}

src/Symfony/Component/Translation/Dumper/IcuResFileDumper.php

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,12 @@
1818
*
1919
* @author Stealth35
2020
*/
21-
class IcuResFileDumper implements DumperInterface
21+
class IcuResFileDumper extends FileDumper
2222
{
2323
/**
2424
* {@inheritDoc}
2525
*/
26-
public function dump(MessageCatalogue $messages, $options = array())
27-
{
28-
if (!array_key_exists('path', $options)) {
29-
throw new \InvalidArgumentException('The file dumper need a path options.');
30-
}
31-
32-
// save a file for each domain
33-
foreach ($messages->getDomains() as $domain) {
34-
$file = $messages->getLocale().'.'.$this->getExtension();
35-
$path = $options['path'].'/'.$domain.'/';
36-
37-
if (!file_exists($path)) {
38-
mkdir($path);
39-
}
40-
41-
// backup
42-
if (file_exists($path.$file)) {
43-
copy($path.$file, $path.$file.'~');
44-
}
45-
46-
// save file
47-
file_put_contents($path.$file, $this->format($messages, $domain));
48-
}
49-
}
26+
protected $relativePathTemplate = '{domain}/{locale}.{extension}';
5027

5128
/**
5229
* {@inheritDoc}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Translation\Dumper;
13+
14+
use Symfony\Component\Translation\MessageCatalogue;
15+
16+
/**
17+
* Null file dumper used for testing purposes.
18+
*
19+
* @author Florian Voutzinos <florian@voutzinos.com>
20+
*/
21+
class NullFileDumper extends FileDumper
22+
{
23+
/**
24+
* {@inheritDoc}
25+
*/
26+
protected function format(MessageCatalogue $messages, $domain)
27+
{
28+
return '';
29+
}
30+
31+
/**
32+
* {@inheritDoc}
33+
*/
34+
protected function getExtension()
35+
{
36+
return 'null';
37+
}
38+
}

src/Symfony/Component/Translation/Tests/Dumper/IcuResFileDumperTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public function testDump()
2626
$catalogue->add(array('foo' => 'bar'));
2727

2828
$tempDir = sys_get_temp_dir() . '/IcuResFileDumperTest';
29-
mkdir($tempDir);
3029
$dumper = new IcuResFileDumper();
3130
$dumper->dump($catalogue, array('path' => $tempDir));
3231

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Translation\Tests\Dumper;
13+
14+
use Symfony\Component\Translation\Dumper\NullFileDumper;
15+
use Symfony\Component\Translation\MessageCatalogue;
16+
17+
class NullFileDumperTest extends \PHPUnit_Framework_TestCase
18+
{
19+
public function testDumpBackupsFileIfExisting()
20+
{
21+
$tempDir = sys_get_temp_dir();
22+
$file = $tempDir.'/messages.en.null';
23+
$backupFile = $file.'~';
24+
25+
@touch($file);
26+
27+
$catalogue = new MessageCatalogue('en');
28+
$catalogue->add(array('foo' => 'bar'));
29+
30+
$dumper = new NullFileDumper();
31+
$dumper->dump($catalogue, array('path' => $tempDir));
32+
33+
$this->assertTrue(file_exists($backupFile));
34+
35+
@unlink($file);
36+
@unlink($backupFile);
37+
}
38+
39+
public function testDumpCreatesNestedDirectoriesAndFile()
40+
{
41+
$tempDir = sys_get_temp_dir();
42+
$translationsDir = $tempDir.'/test/translations';
43+
$file = $translationsDir.'/messages.en.null';
44+
45+
$catalogue = new MessageCatalogue('en');
46+
$catalogue->add(array('foo' => 'bar'));
47+
48+
$dumper = new NullFileDumper();
49+
$dumper->setRelativePathTemplate('test/translations/{domain}.{locale}.{extension}');
50+
$dumper->dump($catalogue, array('path' => $tempDir));
51+
52+
$this->assertTrue(file_exists($file));
53+
54+
@unlink($file);
55+
@rmdir($translationsDir);
56+
}
57+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy