Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Translation Contracts #528

Merged
merged 1 commit into from
Mar 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Tests/Twig/TranslationExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace JMS\TranslationBundle\Tests\Twig;

use JMS\TranslationBundle\Twig\TranslationExtension;
use PHPUnit\Framework\TestCase;

final class TranslationExtensionTest extends TestCase
{
public function testInvalidConstruction(): void
{
$this->expectException(\InvalidArgumentException::class);

new TranslationExtension(new \StdClass());
}
}
32 changes: 26 additions & 6 deletions Twig/TranslationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Translation\TranslatorInterface as ComponentTranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

class TranslationExtension extends AbstractExtension
{
/**
* @var TranslatorInterface
* @var TranslatorInterface|ComponentTranslatorInterface
*/
private $translator;

Expand All @@ -41,11 +42,21 @@ class TranslationExtension extends AbstractExtension

/**
* TranslationExtension constructor.
* @param TranslatorInterface $translator
* @param TranslatorInterface|ComponentTranslatorInterface $translator
* @param bool $debug
*/
public function __construct(TranslatorInterface $translator, $debug = false)
public function __construct($translator, $debug = false)
{
if (!$translator instanceof ComponentTranslatorInterface && !$translator instanceof TranslatorInterface) {
throw new \InvalidArgumentException(sprintf(
'Argument 1 must be an instance of %s or %s, instance of %s given'
,
TranslatorInterface::class,
ComponentTranslatorInterface::class,
get_class($translator)
));
}

$this->translator = $translator;
$this->debug = $debug;
}
Expand Down Expand Up @@ -94,10 +105,19 @@ public function transchoiceWithDefault($message, $defaultMessage, $count, array
}

if (false == $this->translator->getCatalogue($locale)->defines($message, $domain)) {
return $this->translator->transChoice($defaultMessage, $count, array_merge(array('%count%' => $count), $arguments), $domain, $locale);
return $this->transChoice($defaultMessage, $count, array_merge(array('%count%' => $count), $arguments), $domain, $locale);
}

return $this->transChoice($message, $count, array_merge(array('%count%' => $count), $arguments), $domain, $locale);
}

private function transChoice($message, $count, array $arguments, $domain, $locale)
{
if (method_exists($this->translator, 'transChoice')) {
return $this->translator->transChoice($message, $count, array_merge(array('%count%' => $count), $arguments), $domain, $locale);
}

return $this->translator->transChoice($message, $count, array_merge(array('%count%' => $count), $arguments), $domain, $locale);
return $this->translator->trans($message, array_merge(array('%count%' => $count), $arguments), $domain, $locale);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
"nikic/php-parser": "^1.4 || ^2.0 || ^3.0 || ^4.0",
"symfony/console": "^3.4 || ^4.3",
"symfony/framework-bundle": "^3.4.31 || ^4.3",
"symfony/translation": "^3.4 || ^4.3",
"symfony/translation-contracts": "^1.1 || ^2.0",
"symfony/validator": "^3.4 || ^4.3",
"twig/twig": "^1.42.4 || ^2.12.5"
},
Expand All @@ -42,7 +44,6 @@
"symfony/form": "^3.4 || ^4.3",
"symfony/security-csrf": "^3.4 || ^4.3",
"symfony/templating": "^3.4 || ^4.3",
"symfony/translation": "^3.4 || ^4.3",
"symfony/twig-bundle": "^3.4.37 || ^4.3.11"
},
"config": {
Expand Down