Translator example

public function testThatACacheIsUsed($debug)
    {
        if (!class_exists(\MessageFormatter::class)) {
            $this->markTestSkipped(sprintf('Skipping test as the required "%s" class does not exist. Consider installing the "intl" PHP extension or the "symfony/polyfill-intl-messageformatter" package.', \MessageFormatter::class));
        }

        $locale = 'any_locale';
        $format = 'some_format';
        $msgid = 'test';

        // Prime the cache         $translator = new Translator($locale, null, $this->tmpDir, $debug);
        $translator->addLoader($formatnew ArrayLoader());
        $translator->addResource($format[$msgid => 'OK']$locale);
        $translator->addResource($format[$msgid.'+intl' => 'OK']$locale, 'messages+intl-icu');
        $translator->trans($msgid);
        $translator->trans($msgid.'+intl', [], 'messages+intl-icu');

        // Try again and see we get a valid result whilst no loader can be used         $translator = new Translator($locale, null, $this->tmpDir, $debug);
        $translator->addLoader($format$this->createFailingLoader());
        $translator->addResource($format[$msgid => 'OK']$locale);
        $translator->addResource($format[$msgid.'+intl' => 'OK']$locale, 'messages+intl-icu');
        
public function testAuthenticationFailureWithoutTranslator()
    {
        $this->setUpAuthenticator();

        $response = $this->authenticator->onAuthenticationFailure(new Request()new AuthenticationException());
        $this->assertSame(['error' => 'An authentication exception occurred.']json_decode($response->getContent(), true));
    }

    public function testAuthenticationFailureWithTranslator()
    {
        $translator = new Translator('en');
        $translator->addLoader('array', new ArrayLoader());
        $translator->addResource('array', ['An authentication exception occurred.' => 'foo'], 'en', 'security');

        $this->setUpAuthenticator();
        $this->authenticator->setTranslator($translator);

        $response = $this->authenticator->onAuthenticationFailure(new Request()new AuthenticationException());
        $this->assertSame(['error' => 'foo']json_decode($response->getContent(), true));
    }

    public function testOnFailureReplacesMessageDataWithoutTranslator()
    {
class LoggingTranslatorTest extends TestCase
{
    public function testTransWithNoTranslationIsLogged()
    {
        $logger = $this->createMock(LoggerInterface::class);
        $logger->expects($this->exactly(1))
            ->method('warning')
            ->with('Translation not found.')
        ;

        $translator = new Translator('ar');
        $loggableTranslator = new LoggingTranslator($translator$logger);
        $loggableTranslator->trans('bar');
    }
}
'domain' => 'messages',
            'state' => DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK,
            'parameters' => ['foo' => 'bar'],
            'transChoiceNumber' => null,
        ];

        $this->assertEquals($expectedMessages$collector->getCollectedMessages());
    }

    private function createCollector()
    {
        $translator = new Translator('en');
        $translator->addLoader('array', new ArrayLoader());
        $translator->addResource('array', ['foo' => 'foo (en)'], 'en');
        $translator->addResource('array', ['bar' => 'bar (fr)'], 'fr');
        $translator->addResource('array', ['bar_ru' => 'bar (ru)'], 'ru');

        return new DataCollectorTranslator($translator);
    }
}
protected function tearDown(): void
    {
        \Locale::setDefault($this->defaultLocale);
    }

    /** * @dataProvider getInvalidLocalesTests */
    public function testConstructorInvalidLocale($locale)
    {
        $this->expectException(InvalidArgumentException::class);
        new Translator($locale);
    }

    /** * @dataProvider getValidLocalesTests */
    public function testConstructorValidLocale($locale)
    {
        $translator = new Translator($locale);

        $this->assertSame($locale ?: (class_exists(\Locale::class) ? \Locale::getDefault() : 'en')$translator->getLocale());
    }

    


    /** * @dataProvider getTransTests */
    public function testTrans($template$expected, array $variables = [])
    {
        if ($expected != $this->getTemplate($template)->render($variables)) {
            echo $template."\n";
            $loader = new TwigArrayLoader(['index' => $template]);
            $twig = new Environment($loader['debug' => true, 'cache' => false]);
            $twig->addExtension(new TranslationExtension(new Translator('en')));

            echo $twig->compile($twig->parse($twig->tokenize($twig->getLoader()->getSourceContext('index'))))."\n\n";
            $this->assertEquals($expected$this->getTemplate($template)->render($variables));
        }

        $this->assertEquals($expected$this->getTemplate($template)->render($variables));
    }

    public function testTransUnknownKeyword()
    {
        $this->expectException(\Twig\Error\SyntaxError::class);
        

        } else {
            $snippetServiceMock->expects(static::never())->method('getStorefrontSnippets');
        }

        $localeCodeProvider = $this->createMock(LanguageLocaleCodeProvider::class);
        $localeCodeProvider->expects(static::any())->method('getLocaleForLanguageId')->with(Defaults::LANGUAGE_SYSTEM)->willReturn('en-GB');

        $connection = $this->createMock(Connection::class);
        $connection->method('fetchFirstColumn')->willReturn([$snippetSetId]);

        $translator = new Translator(
            $decorated,
            $requestStack,
            $cache,
            $this->createMock(MessageFormatterInterface::class),
            'prod',
            $connection,
            $localeCodeProvider,
            $snippetServiceMock,
            false
        );

        
use Symfony\Component\Translation\Loader\ArrayLoader;
use Symfony\Component\Translation\TranslatableMessage;
use Symfony\Component\Translation\Translator;

class TranslatableTest extends TestCase
{
    /** * @dataProvider getTransTests */
    public function testTrans(string $expected, TranslatableMessage $translatable, array $translation, string $locale)
    {
        $translator = new Translator('en');
        $translator->addLoader('array', new ArrayLoader());
        $translator->addResource('array', $translation$locale$translatable->getDomain());

        $this->assertSame($expected$translatable->trans($translator$locale));
    }

    /** * @dataProvider getFlattenedTransTests */
    public function testFlattenedTrans($expected$messages$translatable)
    {
        
class TranslatorTest extends TestCase
{
    /** @dataProvider getXpathLiteralTestData */
    public function testXpathLiteral($value$literal)
    {
        $this->assertEquals($literal, Translator::getXpathLiteral($value));
    }

    /** @dataProvider getCssToXPathTestData */
    public function testCssToXPath($css$xpath)
    {
        $translator = new Translator();
        $translator->registerExtension(new HtmlExtension($translator));
        $this->assertEquals($xpath$translator->cssToXPath($css, ''));
    }

    public function testCssToXPathPseudoElement()
    {
        $this->expectException(ExpressionErrorException::class);
        $translator = new Translator();
        $translator->registerExtension(new HtmlExtension($translator));
        $translator->cssToXPath('e::first-line');
    }

    


        $translator = $this->getTranslator($loader['resource_files' => $resourceFiles], 'yml');
        $translator->setLocale('fr');

        $this->assertEquals('rĂ©pertoire', $translator->trans('folder'));
    }

    public function testGetDefaultLocale()
    {
        $container = $this->createMock(\Psr\Container\ContainerInterface::class);
        $translator = new Translator($containernew MessageFormatter(), 'en');

        $this->assertSame('en', $translator->getLocale());
    }

    public function testInvalidOptions()
    {
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('The Translator does not support the following options: \'foo\'');
        $container = $this->createMock(ContainerInterface::class);

        new Translator($containernew MessageFormatter(), 'en', []['foo' => 'bar']);
    }
private Translator $translator;
    private array $cache;

    private static array $xmlCache = [];
    private static array $htmlCache = [];

    /** * @param bool $html Whether HTML support should be enabled. Disable it for XML documents */
    public function __construct(bool $html = true)
    {
        $this->translator = new Translator();

        if ($html) {
            $this->translator->registerExtension(new HtmlExtension($this->translator));
            $this->cache = &self::$htmlCache;
        } else {
            $this->cache = &self::$xmlCache;
        }

        $this->translator
            ->registerParserShortcut(new EmptyStringParser())
            ->registerParserShortcut(new ElementParser())
            
Home | Imprint | This part of the site doesn't use cookies.