Skip to content

Commit

Permalink
[Translation] Improve handling of non-string messages in ArrayLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
rob006 committed May 13, 2023
1 parent d61b959 commit 3952042
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/Symfony/Component/Translation/Loader/ArrayLoader.php
Expand Up @@ -46,10 +46,12 @@ private function flatten(array $messages): array
foreach ($messages as $key => $value) {
if (\is_array($value)) {
foreach ($this->flatten($value) as $k => $v) {
$result[$key.'.'.$k] = $v;
if (null !== $v) {
$result[$key.'.'.$k] = (string) $v;
}
}
} else {
$result[$key] = $value;
} elseif (null !== $value) {
$result[$key] = (string) $value;
}
}

Expand Down
Expand Up @@ -30,6 +30,15 @@ public function testLoad()
$this->assertEquals([new FileResource($resource)], $catalogue->getResources());
}

public function testLoadNonStringMessages()
{
$loader = new YamlFileLoader();
$resource = __DIR__.'/../fixtures/non-string.yml';
$catalogue = $loader->load($resource, 'en', 'domain1');

$this->assertSame(['root.foo2' => '', 'root.bar' => 'bar', 'root.baz' => '1'], $catalogue->all('domain1'));
}

public function testLoadDoesNothingIfEmpty()
{
$loader = new YamlFileLoader();
Expand Down
@@ -0,0 +1,5 @@
root:
foo1:
foo2: ''
bar: 'bar'
baz: 1

0 comments on commit 3952042

Please sign in to comment.