Skip to content

Commit 0060123

Browse files
authored
make sure we can load from resource (#17)
* make sure we can load from resource * Added changelog
1 parent 7ecb24e commit 0060123

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

Changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.
44

5+
## 0.3.1
6+
7+
### Fixed
8+
9+
- Make sure XliffLoader can load from resource
10+
511
## 0.3.0
612

713
### Added

src/Loader/XliffLoader.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Translation\SymfonyStorage\Loader;
1313

1414
use Nyholm\NSA;
15+
use Symfony\Component\Config\Resource\FileResource;
16+
use Symfony\Component\Translation\Exception\NotFoundResourceException;
1517
use Symfony\Component\Translation\Loader\XliffFileLoader;
1618
use Symfony\Component\Translation\MessageCatalogue;
1719
use Symfony\Component\Translation\Exception\InvalidResourceException;
@@ -29,6 +31,30 @@ class XliffLoader extends XliffFileLoader
2931
*/
3032
private $sfPort;
3133

34+
/**
35+
* {@inheritdoc}
36+
*/
37+
public function load($resource, $locale, $domain = 'messages')
38+
{
39+
if (!stream_is_local($resource)) {
40+
throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
41+
}
42+
43+
if (!file_exists($resource)) {
44+
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
45+
}
46+
47+
$catalogue = new MessageCatalogue($locale);
48+
$content = file_get_contents($resource);
49+
$this->extractFromContent($content, $catalogue, $domain);
50+
51+
if (class_exists('Symfony\Component\Config\Resource\FileResource')) {
52+
$catalogue->addResource(new FileResource($resource));
53+
}
54+
55+
return $catalogue;
56+
}
57+
3258
/**
3359
* @param string $content xml content
3460
* @param MessageCatalogue $catalogue

tests/Unit/Loader/XliffLoaderTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,25 @@ public function testXliff20Meta()
105105
$this->assertEquals('1', $metadata['notes'][2]['priority']);
106106
$this->assertEquals('user login', $metadata['notes'][2]['content']);
107107
}
108+
109+
public function testXliff20FromResource()
110+
{
111+
$file = __DIR__.'/../../Fixtures/meta.en.xlf';
112+
113+
$catalogue = (new XliffLoader())->load($file, 'en', 'messages');
114+
$this->assertTrue($catalogue->defines('foo'));
115+
$metadata = $catalogue->getMetadata('foo');
116+
$this->assertNotEmpty($metadata);
117+
$this->assertCount(3, $metadata['notes']);
118+
119+
$this->assertEquals('state', $metadata['notes'][0]['category']);
120+
$this->assertEquals('new', $metadata['notes'][0]['content']);
121+
122+
$this->assertEquals('approved', $metadata['notes'][1]['category']);
123+
$this->assertEquals('true', $metadata['notes'][1]['content']);
124+
125+
$this->assertEquals('section', $metadata['notes'][2]['category']);
126+
$this->assertEquals('1', $metadata['notes'][2]['priority']);
127+
$this->assertEquals('user login', $metadata['notes'][2]['content']);
128+
}
108129
}

0 commit comments

Comments
 (0)