Skip to content

Commit

Permalink
Merge branch '7.1' into 7.2
Browse files Browse the repository at this point in the history
* 7.1:
  Add tests covering `trans_default_domain` with dynamic expressions
  • Loading branch information
nicolas-grekas committed Dec 7, 2024
2 parents 1343696 + abf649e commit d5cdf4d
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions Tests/Extension/TranslationExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,68 @@ public function testDefaultTranslationDomainWithNamedArguments()
$this->assertEquals('foo (custom)foo (foo)foo (custom)foo (custom)foo (fr)foo (custom)foo (fr)', trim($template->render([])));
}

public function testDefaultTranslationDomainWithExpression()
{
$templates = [
'index' => '
{%- extends "base" %}
{%- trans_default_domain custom_domain %}
{%- block content %}
{{- "foo"|trans }}
{%- endblock %}
',

'base' => '
{%- block content "" %}
',
];

$translator = new Translator('en');
$translator->addLoader('array', new ArrayLoader());
$translator->addResource('array', ['foo' => 'foo (messages)'], 'en');
$translator->addResource('array', ['foo' => 'foo (custom)'], 'en', 'custom');
$translator->addResource('array', ['foo' => 'foo (foo)'], 'en', 'foo');

$template = $this->getTemplate($templates, $translator);

$this->assertEquals('foo (foo)', trim($template->render(['custom_domain' => 'foo'])));
}

public function testDefaultTranslationDomainWithExpressionAndInheritance()
{
$templates = [
'index' => '
{%- extends "base" %}
{%- trans_default_domain foo_domain %}
{%- block content %}
{{- "foo"|trans }}
{%- endblock %}
',

'base' => '
{%- trans_default_domain custom_domain %}
{{- "foo"|trans }}
{%- block content "" %}
{{- "foo"|trans }}
',
];

$translator = new Translator('en');
$translator->addLoader('array', new ArrayLoader());
$translator->addResource('array', ['foo' => 'foo (messages)'], 'en');
$translator->addResource('array', ['foo' => 'foo (custom)'], 'en', 'custom');
$translator->addResource('array', ['foo' => 'foo (foo)'], 'en', 'foo');

$template = $this->getTemplate($templates, $translator);

$this->assertEquals('foo (custom)foo (foo)foo (custom)', trim($template->render(['foo_domain' => 'foo', 'custom_domain' => 'custom'])));
}

private function getTemplate($template, ?TranslatorInterface $translator = null): TemplateWrapper
{
$translator ??= new Translator('en');
Expand Down

0 comments on commit d5cdf4d

Please sign in to comment.