Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c4b8e03

Browse filesBrowse files
committed
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 | #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
2 parents 786c956 + a04175e commit c4b8e03
Copy full SHA for c4b8e03

File tree

Expand file treeCollapse file tree

5 files changed

+121
-31
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+121
-31
lines changed

‎src/Symfony/Component/Translation/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/CHANGELOG.md
+6Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Dumper/FileDumper.php
+40-2Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Dumper/IcuResFileDumper.php
+2-25Lines 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}
+70Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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\MessageCatalogue;
15+
use Symfony\Component\Translation\Dumper\FileDumper;
16+
17+
class FileDumperTest extends \PHPUnit_Framework_TestCase
18+
{
19+
public function testDumpBackupsFileIfExisting()
20+
{
21+
$tempDir = sys_get_temp_dir();
22+
$file = $tempDir.'/messages.en.concrete';
23+
$backupFile = $file.'~';
24+
25+
@touch($file);
26+
27+
$catalogue = new MessageCatalogue('en');
28+
$catalogue->add(array('foo' => 'bar'));
29+
30+
$dumper = new ConcreteFileDumper();
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.concrete';
44+
45+
$catalogue = new MessageCatalogue('en');
46+
$catalogue->add(array('foo' => 'bar'));
47+
48+
$dumper = new ConcreteFileDumper();
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+
}
58+
59+
class ConcreteFileDumper extends FileDumper
60+
{
61+
protected function format(MessageCatalogue $messages, $domain)
62+
{
63+
return '';
64+
}
65+
66+
protected function getExtension()
67+
{
68+
return 'concrete';
69+
}
70+
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Tests/Dumper/IcuResFileDumperTest.php
+3-4Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,13 @@ 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

3332
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resourcebundle/res/en.res'), file_get_contents($tempDir.'/messages/en.res'));
3433

35-
unlink($tempDir.'/messages/en.res');
36-
rmdir($tempDir.'/messages');
37-
rmdir($tempDir);
34+
@unlink($tempDir.'/messages/en.res');
35+
@rmdir($tempDir.'/messages');
36+
@rmdir($tempDir);
3837
}
3938
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.