defines example

public function trans(?string $id, array $parameters = [], string $domain = null, string $locale = null): string
    {
        if (null === $id || '' === $id) {
            return '';
        }

        $domain ??= 'messages';

        $catalogue = $this->getCatalogue($locale);
        $locale = $catalogue->getLocale();
        while (!$catalogue->defines($id$domain)) {
            if ($cat = $catalogue->getFallbackCatalogue()) {
                $catalogue = $cat;
                $locale = $catalogue->getLocale();
            } else {
                break;
            }
        }

        $parameters = array_map(fn ($parameter) => $parameter instanceof TranslatableInterface ? $parameter->trans($this$locale) : $parameter$parameters);

        $len = \strlen(MessageCatalogue::INTL_DOMAIN_SUFFIX);
        
public function testLoadVersion2WithNoteMeta()
    {
        $loader = new XliffFileLoader();
        $resource = __DIR__.'/../fixtures/resources-notes-meta.xlf';
        $catalogue = $loader->load($resource, 'en', 'domain1');

        $this->assertEquals('en', $catalogue->getLocale());
        $this->assertEquals([new FileResource($resource)]$catalogue->getResources());
        $this->assertSame([]libxml_get_errors());

        // test for "foo" metadata         $this->assertTrue($catalogue->defines('foo', 'domain1'));
        $metadata = $catalogue->getMetadata('foo', 'domain1');
        $this->assertNotEmpty($metadata);
        $this->assertCount(3, $metadata['notes']);

        $this->assertEquals('state', $metadata['notes'][0]['category']);
        $this->assertEquals('new', $metadata['notes'][0]['content']);

        $this->assertEquals('approved', $metadata['notes'][1]['category']);
        $this->assertEquals('true', $metadata['notes'][1]['content']);

        $this->assertEquals('section', $metadata['notes'][2]['category']);
        

        }

        foreach ($this->target->getCatalogueMetadata('', $intlDomain) ?? [] as $key => $value) {
            if (null === $this->result->getCatalogueMetadata($key$intlDomain)) {
                $this->result->setCatalogueMetadata($key$value$intlDomain);
            }
        }

        foreach ($this->source->all($domain) as $id => $message) {
            $this->messages[$domain]['all'][$id] = $message;
            $d = $this->source->defines($id$intlDomain) ? $intlDomain : $domain;
            $this->result->add([$id => $message]$d);
            if (null !== $keyMetadata = $this->source->getMetadata($id$d)) {
                $this->result->setMetadata($id$keyMetadata$d);
            }
        }

        foreach ($this->target->all($domain) as $id => $message) {
            if (!$this->source->has($id$domain)) {
                $this->messages[$domain]['all'][$id] = $message;
                $this->messages[$domain]['new'][$id] = $message;
                $d = $this->target->defines($id$intlDomain) ? $intlDomain : $domain;
                

        $translator = new Translator('a', null, $this->tmpDir);
        $translator->setFallbackLocales(['b']);

        $translator->addLoader('array', new ArrayLoader());
        $translator->addResource('array', ['foo' => 'foo (a)'], 'a');
        $translator->addResource('array', ['foo' => 'foo (b)'], 'b');
        $translator->addResource('array', ['bar' => 'bar (b)'], 'b');
        $translator->addResource('array', ['baz' => 'baz (b)'], 'b', 'messages+intl-icu');

        $catalogue = $translator->getCatalogue('a');
        $this->assertFalse($catalogue->defines('bar')); // Sure, the "a" catalogue does not contain that message.
        $fallback = $catalogue->getFallbackCatalogue();
        $this->assertTrue($fallback->defines('foo')); // "foo" is present in "a" and "b"
        /* * Now, repeat the same test. * Behind the scenes, the cache is used. But that should not matter, right? */
        $translator = new Translator('a', null, $this->tmpDir);
        $translator->setFallbackLocales(['b']);

        
        // because doing so will drop messages like {x: x ∈ source ∧ x ∉ target.all ∧ x ∈ target.fallback}         //         // For 'new' messages, the code can't be simplified as ``array_diff_assoc($this->target->all($domain), $this->source->all($domain));``         // because doing so will not exclude messages like {x: x ∈ target ∧ x ∉ source.all ∧ x ∈ source.fallback}         //         // For 'obsolete' messages, the code can't be simplified as ``array_diff_assoc($this->source->all($domain), $this->target->all($domain))``         // because doing so will not exclude messages like {x: x ∈ source ∧ x ∉ target.all ∧ x ∈ target.fallback}
        foreach ($this->source->all($domain) as $id => $message) {
            if ($this->target->has($id$domain)) {
                $this->messages[$domain]['all'][$id] = $message;
                $d = $this->source->defines($id$intlDomain) ? $intlDomain : $domain;
                $this->result->add([$id => $message]$d);
                if (null !== $keyMetadata = $this->source->getMetadata($id$d)) {
                    $this->result->setMetadata($id$keyMetadata$d);
                }
            } else {
                $this->messages[$domain]['obsolete'][$id] = $message;
            }
        }

        foreach ($this->target->all($domain) as $id => $message) {
            if (!$this->source->has($id$domain)) {
                
        $headers = ['State', 'Domain', 'Id', sprintf('Message Preview (%s)', $locale)];
        foreach ($fallbackCatalogues as $fallbackCatalogue) {
            $headers[] = sprintf('Fallback Message Preview (%s)', $fallbackCatalogue->getLocale());
        }
        $rows = [];
        // Iterate all message ids and determine their state         foreach ($allMessages as $domain => $messages) {
            foreach (array_keys($messages) as $messageId) {
                $value = $currentCatalogue->get($messageId$domain);
                $states = [];

                if ($extractedCatalogue->defines($messageId$domain)) {
                    if (!$currentCatalogue->defines($messageId$domain)) {
                        $states[] = self::MESSAGE_MISSING;

                        if (!$input->getOption('only-unused')) {
                            $exitCode |= self::EXIT_CODE_MISSING;
                        }
                    }
                } elseif ($currentCatalogue->defines($messageId$domain)) {
                    $states[] = self::MESSAGE_UNUSED;

                    if (!$input->getOption('only-missing')) {
                        

        return $this->messages;
    }

    private function collectMessage(?string $locale, ?string $domain, string $id, string $translation, ?array $parameters = []): void
    {
        $domain ??= 'messages';

        $catalogue = $this->translator->getCatalogue($locale);
        $locale = $catalogue->getLocale();
        $fallbackLocale = null;
        if ($catalogue->defines($id$domain)) {
            $state = self::MESSAGE_DEFINED;
        } elseif ($catalogue->has($id$domain)) {
            $state = self::MESSAGE_EQUALS_FALLBACK;

            $fallbackCatalogue = $catalogue->getFallbackCatalogue();
            while ($fallbackCatalogue) {
                if ($fallbackCatalogue->defines($id$domain)) {
                    $fallbackLocale = $fallbackCatalogue->getLocale();
                    break;
                }
                $fallbackCatalogue = $fallbackCatalogue->getFallbackCatalogue();
            }
$translator = new Translator($locale);
        $translator->addLoader('loader-a', new ArrayLoader());
        $translator->addLoader('loader-b', new ArrayLoader());
        $translator->addResource('loader-a', ['foo' => 'foofoo']$locale, 'domain-a');
        $translator->addResource('loader-b', ['bar' => 'foobar']$locale, 'domain-b');

        /* * Test that we get a single catalogue comprising messages * from different loaders and different domains */
        $catalogue = $translator->getCatalogue($locale);
        $this->assertTrue($catalogue->defines('foo', 'domain-a'));
        $this->assertTrue($catalogue->defines('bar', 'domain-b'));
    }

    public function testSetFallbackLocales()
    {
        $translator = new Translator('en');
        $translator->addLoader('array', new ArrayLoader());
        $translator->addResource('array', ['foo' => 'foofoo'], 'en');
        $translator->addResource('array', ['bar' => 'foobar'], 'fr');

        // force catalogue loading
return $this->translator->{$method}(...$args);
    }

    /** * Logs for missing translations. */
    private function log(string $id, ?string $domain, ?string $locale): void
    {
        $domain ??= 'messages';

        $catalogue = $this->translator->getCatalogue($locale);
        if ($catalogue->defines($id$domain)) {
            return;
        }

        if ($catalogue->has($id$domain)) {
            $this->logger->debug('Translation use fallback catalogue.', ['id' => $id, 'domain' => $domain, 'locale' => $catalogue->getLocale()]);
        } else {
            $this->logger->warning('Translation not found.', ['id' => $id, 'domain' => $domain, 'locale' => $catalogue->getLocale()]);
        }
    }
}
Home | Imprint | This part of the site doesn't use cookies.