Skip to content

Commit 659b147

Browse files
authored
Create a new file if no resource exists (#9)
* Create a new file if no resource exists * cs * cs * cs
1 parent c32be25 commit 659b147

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

src/FileStorage.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,24 @@ private function writeCatalogue(MessageCatalogue $catalogue, $locale, $domain)
149149
{
150150
$resources = $catalogue->getResources();
151151
$options = $this->options;
152+
$written = false;
152153
foreach ($resources as $resource) {
153154
$path = (string) $resource;
154155
if (preg_match('|/'.$domain.'\.'.$locale.'\.([a-z]+)$|', $path, $matches)) {
155156
$options['path'] = str_replace($matches[0], '', $path);
156157
$this->writer->writeTranslations($catalogue, $matches[1], $options);
158+
$written = true;
157159
}
158160
}
161+
162+
if ($written) {
163+
// We have written the translation to a file.
164+
return;
165+
}
166+
167+
$options['path'] = reset($this->dir);
168+
$format = isset($options['default_output_format']) ? $options['default_output_format'] : 'xlf';
169+
$this->writer->writeTranslations($catalogue, $format, $options);
159170
}
160171

161172
/**

tests/Unit/FileStorageTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212
namespace Translation\SymfonyStorage\Tests\Unit;
1313

1414
use Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader;
15+
use Symfony\Component\Translation\MessageCatalogueInterface;
1516
use Symfony\Component\Translation\Writer\TranslationWriter;
17+
use Translation\Common\Model\Message;
1618
use Translation\SymfonyStorage\FileStorage;
19+
use Translation\SymfonyStorage\Loader\XliffLoader;
1720

1821
/**
1922
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
@@ -40,4 +43,58 @@ public function testConstructorEmptyArray()
4043
{
4144
new FileStorage(new TranslationWriter(), new TranslationLoader(), []);
4245
}
46+
47+
public function testCreateNewCatalogue()
48+
{
49+
$writer = $this->getMockBuilder(TranslationWriter::class)
50+
->setMethods(['writeTranslations'])
51+
->disableOriginalConstructor()
52+
->getMock();
53+
$writer->expects($this->once())
54+
->method('writeTranslations')
55+
->with(
56+
$this->isInstanceOf(MessageCatalogueInterface::class),
57+
'xlf',
58+
['path' => 'foo']
59+
);
60+
61+
$storage = new FileStorage($writer, new TranslationLoader(), ['foo']);
62+
$storage->create(new Message('key', 'domain', 'en', 'Message'));
63+
64+
$writer = $this->getMockBuilder(TranslationWriter::class)
65+
->setMethods(['writeTranslations'])
66+
->disableOriginalConstructor()
67+
->getMock();
68+
$writer->expects($this->once())
69+
->method('writeTranslations')
70+
->with(
71+
$this->isInstanceOf(MessageCatalogueInterface::class),
72+
'format',
73+
['path' => 'bar', 'default_output_format' => 'format']
74+
);
75+
76+
$storage = new FileStorage($writer, new TranslationLoader(), ['bar'], ['default_output_format' => 'format']);
77+
$storage->create(new Message('key', 'domain', 'en', 'Message'));
78+
}
79+
80+
public function testCreateExistingCatalogue()
81+
{
82+
$writer = $this->getMockBuilder(TranslationWriter::class)
83+
->setMethods(['writeTranslations'])
84+
->disableOriginalConstructor()
85+
->getMock();
86+
$writer->expects($this->once())
87+
->method('writeTranslations')
88+
->with(
89+
$this->isInstanceOf(MessageCatalogueInterface::class),
90+
'xlf',
91+
['path' => __DIR__]
92+
);
93+
94+
$loader = new TranslationLoader();
95+
$loader->addLoader('xlf', new XliffLoader());
96+
$storage = new FileStorage($writer, $loader, ['foo', __DIR__]);
97+
98+
$storage->create(new Message('key', 'messages', 'en', 'Translation'));
99+
}
43100
}

0 commit comments

Comments
 (0)