Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commitc4b8e03

Browse 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 placeholders623d149 Added a ConcreteDumper84f0902 [Translation] Added template for relative file paths
2 parents786c956 +a04175e commitc4b8e03

File tree

5 files changed

+121
-31
lines changed

5 files changed

+121
-31
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
abstractclass FileDumperimplements 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+
publicfunctionsetRelativePathTemplate($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+
thrownew \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
abstractprotectedfunctiongetExtension();
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+
privatefunctiongetRelativePath($domain,$locale)
96+
{
97+
returnstrtr($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 IcuResFileDumperimplements DumperInterface
21+
class IcuResFileDumperextends FileDumper
2222
{
2323
/**
2424
* {@inheritDoc}
2525
*/
26-
publicfunctiondump(MessageCatalogue$messages,$options =array())
27-
{
28-
if (!array_key_exists('path',$options)) {
29-
thrownew \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: 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+
namespaceSymfony\Component\Translation\Tests\Dumper;
13+
14+
useSymfony\Component\Translation\MessageCatalogue;
15+
useSymfony\Component\Translation\Dumper\FileDumper;
16+
17+
class FileDumperTestextends \PHPUnit_Framework_TestCase
18+
{
19+
publicfunctiontestDumpBackupsFileIfExisting()
20+
{
21+
$tempDir =sys_get_temp_dir();
22+
$file =$tempDir.'/messages.en.concrete';
23+
$backupFile =$file.'~';
24+
25+
@touch($file);
26+
27+
$catalogue =newMessageCatalogue('en');
28+
$catalogue->add(array('foo' =>'bar'));
29+
30+
$dumper =newConcreteFileDumper();
31+
$dumper->dump($catalogue,array('path' =>$tempDir));
32+
33+
$this->assertTrue(file_exists($backupFile));
34+
35+
@unlink($file);
36+
@unlink($backupFile);
37+
}
38+
39+
publicfunctiontestDumpCreatesNestedDirectoriesAndFile()
40+
{
41+
$tempDir =sys_get_temp_dir();
42+
$translationsDir =$tempDir.'/test/translations';
43+
$file =$translationsDir.'/messages.en.concrete';
44+
45+
$catalogue =newMessageCatalogue('en');
46+
$catalogue->add(array('foo' =>'bar'));
47+
48+
$dumper =newConcreteFileDumper();
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 ConcreteFileDumperextends FileDumper
60+
{
61+
protectedfunctionformat(MessageCatalogue$messages,$domain)
62+
{
63+
return'';
64+
}
65+
66+
protectedfunctiongetExtension()
67+
{
68+
return'concrete';
69+
}
70+
}

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

Lines 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 =newIcuResFileDumper();
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)

[8]ページ先頭

©2009-2025 Movatter.jp