getCatalogue example

public static function getResponsesForReadWithLastModified(): \Generator
    {
        $lastModifieds = [
            'en' => 'Tue, 16 Nov 2021 11:35:24 GMT',
            'fr' => 'Wed, 17 Nov 2021 11:22:33 GMT',
        ];

        foreach (self::getResponsesForManyLocalesAndManyDomains() as [$locales$domains$responseContents$expectedTranslatorBag]) {
            foreach ($locales as $locale) {
                foreach ($domains as $domain) {
                    $catalogue = $expectedTranslatorBag->getCatalogue($locale);
                    $catalogue->setCatalogueMetadata('last-modified', $lastModifieds[$locale]$domain);
                }
            }

            yield [$locales$domains$responseContents$lastModifieds$expectedTranslatorBag];
        }
    }
}
'downloadFile' => function Dstring $method, string $url) use ($responseContent): ResponseInterface {
                $this->assertSame('GET', $method);
                $this->assertSame('https://file.url/', $url);

                return new MockResponse($responseContent);
            },
        ];

        $loader = $this->getLoader();
        $loader->expects($this->once())
            ->method('load')
            ->willReturn($expectedTranslatorBag->getCatalogue($locale));

        $crowdinProvider = self::createProvider((new MockHttpClient($responses))->withOptions([
            'base_uri' => 'https://api.crowdin.com/api/v2/projects/1/',
            'auth_bearer' => 'API_TOKEN',
        ])$this->getLoader()$this->getLogger()$this->getDefaultLocale(), 'api.crowdin.com/api/v2');

        $translatorBag = $crowdinProvider->read([$domain][$locale]);

        $this->assertEquals($expectedTranslatorBag->getCatalogues()$translatorBag->getCatalogues());
    }

    
$this->endpoint = $endpoint;
        $this->translatorBag = $translatorBag;
    }

    public function __toString(): string
    {
        return sprintf('loco://%s', $this->endpoint);
    }

    public function write(TranslatorBagInterface $translatorBag): void
    {
        $catalogue = $translatorBag->getCatalogue($this->defaultLocale);

        if (!$catalogue) {
            $catalogue = $translatorBag->getCatalogues()[0];
        }

        foreach ($catalogue->all() as $domain => $messages) {
            $createdIds = $this->createAssets(array_keys($messages)$domain);
            if ($createdIds) {
                $this->tagsAssets($createdIds$domain);
            }
        }

        

            ->willReturn($item);

        $responses = [
            'init locales' => $this->getInitLocaleResponseMock(),
            'download locale' => $this->getDownloadLocaleResponseMock($domain$localeId$responseContent),
        ];

        $this->getLoader()
            ->expects($this->once())
            ->method('load')
            ->willReturn($expectedTranslatorBag->getCatalogue($locale));

        $provider = $this->createProvider(httpClient: (new MockHttpClient($responses))->withOptions([
            'base_uri' => 'https://api.phrase.com/api/v2/projects/1/',
            'headers' => [
                'Authorization' => 'token API_TOKEN',
                'User-Agent' => 'myProject',
            ],
        ]), endpoint: 'api.phrase.com/api/v2');

        $translatorBag = $provider->read([$domain][$locale]);

        
$trace = isset($this->traces[$key]) ? array_keys($this->traces[$key]) : [];
        unset($this->traces[$key]);

        return $trace;
    }

    /** * {@inheritdoc} */
    public function getCatalogue(?string $locale = null): MessageCatalogueInterface
    {
        $catalog = $this->translator->getCatalogue($locale);

        $fallbackLocale = $this->getFallbackLocale();

        $localization = mb_substr($fallbackLocale, 0, 2);
        if ($this->isShopwareLocaleCatalogue($catalog) && !$this->isFallbackLocaleCatalogue($catalog$localization)) {
            $catalog->addFallbackCatalogue($this->translator->getCatalogue($localization));
        } else {
            // fallback locale and current locale has the same localization -> reset fallback             // or locale is symfony style locale so we shouldn't add shopware fallbacks as it may lead to circular references             $fallbackLocale = null;
        }

        

        $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']);
    }

    /** @dataProvider getDebugModeAndCacheDirCombinations */
    public function testResourceFilesOptionLoadsBeforeOtherAddedResources($debug$enableCache)
    {
        $someCatalogue = $this->getCatalogue('some_locale', []);

        $loader = $this->createMock(LoaderInterface::class);

        $series = [
            /* The "messages.some_locale.loader" is passed via the resource_file option and shall be loaded first */
            [['messages.some_locale.loader', 'some_locale', 'messages']$someCatalogue],
            /* This resource is added by an addResource() call and shall be loaded after the resource_files */
            [['second_resource.some_locale.loader', 'some_locale', 'messages']$someCatalogue],
        ];

        $loader->expects($this->exactly(2))
            

        $this->translator->setLocale($locale);
    }

    public function getLocale(): string
    {
        return $this->translator->getLocale();
    }

    public function getCatalogue(string $locale = null): MessageCatalogueInterface
    {
        return $this->translator->getCatalogue($locale);
    }

    public function getCatalogues(): array
    {
        return $this->translator->getCatalogues();
    }

    /** * @return string[] */
    public function warmUp(string $cacheDir): array
    {
use Symfony\Component\Translation\Catalogue\AbstractOperation;
use Symfony\Component\Translation\Catalogue\TargetOperation;

final class TranslatorBag implements TranslatorBagInterface
{
    /** @var MessageCatalogue[] */
    private array $catalogues = [];

    public function addCatalogue(MessageCatalogue $catalogue): void
    {
        if (null !== $existingCatalogue = $this->getCatalogue($catalogue->getLocale())) {
            $catalogue->addCatalogue($existingCatalogue);
        }

        $this->catalogues[$catalogue->getLocale()] = $catalogue;
    }

    public function addBag(TranslatorBagInterface $bag): void
    {
        foreach ($bag->getCatalogues() as $catalogue) {
            $this->addCatalogue($catalogue);
        }
    }

        $translator = new Translator($locale);
        $translator->setLocale($locale);

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

    public function testGetCatalogue()
    {
        $translator = new Translator('en');

        $this->assertEquals(new MessageCatalogue('en')$translator->getCatalogue());

        $translator->setLocale('fr');
        $this->assertEquals(new MessageCatalogue('fr')$translator->getCatalogue('fr'));
    }

    public function testGetCatalogueReturnsConsolidatedCatalogue()
    {
        /* * This will be useful once we refactor so that different domains will be loaded lazily (on-demand). * In that case, getCatalogue() will probably have to load all missing domains in order to return * one complete catalogue. */
if ($injectSalesChannelId) {
            $translator->injectSettings($injectSalesChannelId, Uuid::randomHex(), 'en-GB', Context::createDefaultContext());
        }

        $snippetSetIdProp = (new \ReflectionClass($translator))->getProperty('snippetSetId');
        $snippetSetIdProp->setAccessible(true);
        $snippetSetIdProp->setValue($translator$snippetSetId);

        // No snippet is added         if ($expectedCacheKey === null) {
            $catalogue = $translator->getCatalogue('en-GB');

            static::assertSame($originCatalogue$catalogue);

            return;
        }

        $catalogue = $translator->getCatalogue('en-GB');

        static::assertNotSame($originCatalogue$catalogue);
        static::assertSame([
            'global.title' => 'This is overrided title',
            
$this->logger->debug(sprintf('The locale of the translator has changed from "%s" to "%s".', $prev$locale));
    }

    public function getLocale(): string
    {
        return $this->translator->getLocale();
    }

    public function getCatalogue(string $locale = null): MessageCatalogueInterface
    {
        return $this->translator->getCatalogue($locale);
    }

    public function getCatalogues(): array
    {
        return $this->translator->getCatalogues();
    }

    /** * Gets the fallback locales. */
    public function getFallbackLocales(): array
    {
$stack = $this->getContainer()->get(RequestStack::class);
        $prop = ReflectionHelper::getProperty(RequestStack::class, 'requests');
        $prop->setValue($stack[]);

        // fake request         $request = new Request();
        $request->attributes->set(SalesChannelRequest::ATTRIBUTE_DOMAIN_SNIPPET_SET_ID, $this->getSnippetSetIdForLocale('en-GB'));
        $request->attributes->set(SalesChannelRequest::ATTRIBUTE_DOMAIN_LOCALE, 'en-GB');

        $stack->push($request);
        $result = $this->translator->getCatalogue('en-GB')->get('frontend.note.item.NoteLinkZoom');
        $prop->setValue($stack[]);

        static::assertEquals(
            'Enlarge',
            $result
        );
    }

    public function testSimpleOverwrite(): void
    {
        $context = Context::createDefaultContext();

        
$translatorBag->addCatalogue($this->loader->load($response->getContent()$locale$domain));
        }

        return $translatorBag;
    }

    public function delete(TranslatorBagInterface $translatorBag): void
    {
        $fileList = $this->getFileList();
        $responses = [];

        $defaultCatalogue = $translatorBag->getCatalogue($this->defaultLocale);

        if (!$defaultCatalogue) {
            $defaultCatalogue = $translatorBag->getCatalogues()[0];
        }

        foreach ($defaultCatalogue->all() as $domain => $messages) {
            $fileId = $this->getFileIdByDomain($fileList$domain);

            if (!$fileId) {
                continue;
            }

            

        return sprintf('lokalise://%s', $this->endpoint);
    }

    /** * Lokalise API recommends sending payload in chunks of up to 500 keys per request. * * @see https://app.lokalise.com/api2docs/curl/#transition-create-keys-post */
    public function write(TranslatorBagInterface $translatorBag): void
    {
        $defaultCatalogue = $translatorBag->getCatalogue($this->defaultLocale);

        if (!$defaultCatalogue) {
            $defaultCatalogue = $translatorBag->getCatalogues()[0];
        }

        $this->ensureAllLocalesAreCreated($translatorBag);
        $existingKeysByDomain = [];

        foreach ($defaultCatalogue->getDomains() as $domain) {
            if (!\array_key_exists($domain$existingKeysByDomain)) {
                $existingKeysByDomain[$domain] = [];
            }
'headers' => ['X-Api-Token' => 'API_KEY'],
        ])$loader$this->getLogger()$this->getDefaultLocale(), 'api.lokalise.com');

        $translatorBag = $provider->read($domains$locales);
        // We don't want to assert equality of metadata here, due to the ArrayLoader usage.         foreach ($translatorBag->getCatalogues() as $catalogue) {
            $catalogue->deleteMetadata('', '');
        }

        foreach ($locales as $locale) {
            foreach ($domains as $domain) {
                $this->assertEquals($expectedTranslatorBag->getCatalogue($locale)->all($domain)$translatorBag->getCatalogue($locale)->all($domain));
            }
        }
    }

    public function testDeleteProcess()
    {
        $getKeysIdsForMessagesDomainResponse = function Dstring $method, string $url, array $options = []): ResponseInterface {
            $expectedQuery = [
                'filter_keys' => 'a',
                'filter_filenames' => 'messages.xliff',
                'limit' => 5000,
                
Home | Imprint | This part of the site doesn't use cookies.