Skip to content
Open
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
5 changes: 5 additions & 0 deletions .github/get-modified-packages.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
$allPackages = json_decode($_SERVER['argv'][1], true, 512, \JSON_THROW_ON_ERROR);
$modifiedFiles = json_decode($_SERVER['argv'][2], true, 512, \JSON_THROW_ON_ERROR);

// Sort to get the longest name first (match bridge not component)
usort($allPackages, function($a, $b) {
return strlen($b) - strlen($a);
});

function isComponentBridge(string $packageDir): bool
{
return 0 < preg_match('@Symfony/Component/.*/Bridge/@', $packageDir);
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/package-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ jobs:
- name: Fetch branch from where the PR started
run: git fetch --no-tags --prune --depth=1 origin +refs/heads/*:refs/remotes/origin/*

- name: Debug
run: |
echo $(find src/Symfony -mindepth 2 -type f -name composer.json -printf '%h\n')
echo $(git diff --name-only origin/${{ github.base_ref }} HEAD | grep src/)

- name: Find packages
id: find-packages
run: echo "::set-output name=packages::$(php .github/get-modified-packages.php $(find src/Symfony -mindepth 2 -type f -name composer.json -printf '%h\n' | jq -R -s -c 'split("\n")[:-1]') $(git diff --name-only origin/${{ github.base_ref }} HEAD | grep src/ | jq -R -s -c 'split("\n")[:-1]'))"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@
use Symfony\Component\String\LazyString;
use Symfony\Component\String\Slugger\SluggerInterface;
use Symfony\Component\Translation\Bridge\Loco\Provider\LocoProviderFactory;
use Symfony\Component\Translation\Bridge\Lokalise\Provider\LokaliseProviderFactory;
use Symfony\Component\Translation\Command\XliffLintCommand as BaseXliffLintCommand;
use Symfony\Component\Translation\PseudoLocalizationTranslator;
use Symfony\Component\Translation\Translator;
Expand Down Expand Up @@ -1355,14 +1356,18 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder

$classToServices = [
LocoProviderFactory::class => 'translation.provider_factory.loco',
LokaliseProviderFactory::class => 'translation.provider_factory.lokalise',
];

$parentPackages = ['symfony/framework-bundle', 'symfony/translation', 'symfony/http-client'];

foreach ($classToServices as $class => $service) {
$package = sprintf('symfony/%s-translation', substr($service, \strlen('translation.provider_factory.')));
switch ($package = substr($service, \strlen('translation.provider_factory.'))) {
case 'loco': $package = 'loco'; break;
case 'lokalise': $package = 'lokalise'; break;
}

if (!$container->hasDefinition('http_client') || !ContainerBuilder::willBeAvailable($package, $class, $parentPackages)) {
if (!$container->hasDefinition('http_client') || !ContainerBuilder::willBeAvailable(sprintf('symfony/%s-translation', $package), $class, $parentPackages)) {
$container->removeDefinition($service);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/Tests export-ignore
/phpunit.xml.dist export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
3 changes: 3 additions & 0 deletions src/Symfony/Component/Translation/Bridge/Lokalise/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
composer.lock
phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CHANGELOG
=========

5.3
---

* Create the bridge
19 changes: 19 additions & 0 deletions src/Symfony/Component/Translation/Bridge/Lokalise/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2021 Fabien Potencier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Translation\Bridge\Lokalise\Provider;

use Psr\Log\LoggerInterface;
use Symfony\Component\Translation\Exception\ProviderException;
use Symfony\Component\Translation\Loader\LoaderInterface;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Provider\ProviderInterface;
use Symfony\Component\Translation\TranslatorBag;
use Symfony\Component\Translation\TranslatorBagInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
* @author Mathieu Santostefano <[email protected]>
*
* @experimental in 5.3
*
* In Lokalise:
* * Filenames refers to Symfony's translation domains;
* * Keys refers to Symfony's translation keys;
* * Translations refers to Symfony's translated messages
*/
final class LokaliseProvider implements ProviderInterface
{
private $projectId;
private $client;
private $loader;
private $logger;
private $defaultLocale;
private $endpoint;

public function __construct(string $projectId, HttpClientInterface $client, LoaderInterface $loader, LoggerInterface $logger, string $defaultLocale, string $endpoint)
{
$this->projectId = $projectId;
$this->client = $client;
$this->loader = $loader;
$this->logger = $logger;
$this->defaultLocale = $defaultLocale;
$this->endpoint = $endpoint;
}

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

public function getName(): string
{
return LokaliseProviderFactory::SCHEME;
}

/**
* {@inheritdoc}
*/
public function write(TranslatorBagInterface $translatorBag): void
{
$this->createKeysWithTranslations($translatorBag);
}

public function read(array $domains, array $locales): TranslatorBagInterface
{
$translatorBag = new TranslatorBag();
$translations = $this->exportFiles($locales, $domains);

foreach ($translations as $locale => $files) {
foreach ($files as $filename => $content) {
$intlDomain = $domain = str_replace('.xliff', '', $filename);
$suffixLength = \strlen(MessageCatalogue::INTL_DOMAIN_SUFFIX);
if (\strlen($domain) > $suffixLength && false !== strpos($domain, MessageCatalogue::INTL_DOMAIN_SUFFIX, -$suffixLength)) {
$intlDomain .= MessageCatalogue::INTL_DOMAIN_SUFFIX;
}

if (\in_array($intlDomain, $domains, true)) {
$translatorBag->addCatalogue($this->loader->load($content['content'], $locale, $intlDomain));
} else {
$this->logger->info(sprintf('The translations fetched from Lokalise under the filename "%s" does not match with any domains of your application.', $filename));
}
}
}

return $translatorBag;
}

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

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

$keysIds = [];
foreach ($catalogue->all() as $messagesByDomains) {
foreach ($messagesByDomains as $domain => $messages) {
$keysToDelete = [];
foreach ($messages as $message) {
$keysToDelete[] = $message;
}
$keysIds += $this->getKeysIds($keysToDelete, $domain);
}
}

$response = $this->client->request('DELETE', sprintf('/projects/%s/keys', $this->projectId), [
'json' => ['keys' => $keysIds],
]);

if (200 !== $response->getStatusCode()) {
throw new ProviderException(sprintf('Unable to delete keys from Lokalise: "%s".', $response->getContent(false)), $response);
}
}

/**
* 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
*/
private function createKeysWithTranslations(TranslatorBag $translatorBag): void
{
$keys = [];
$catalogue = $translatorBag->getCatalogue($this->defaultLocale);

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

foreach ($translatorBag->getDomains() as $domain) {
foreach ($catalogue->all($domain) as $key => $message) {
$keys[] = [
'key_name' => $key,
'platforms' => ['web'],
'filenames' => [
'web' => $this->generateLokaliseFilenameFromDomain($domain),
// There is a bug in Lokalise with "Per platform key names" option enabled,
// we need to provide a filename for all platforms.
'ios' => null,
'android' => null,
'other' => null,
],
'translations' => array_map(function ($catalogue) use ($key, $domain) {
return [
'language_iso' => $catalogue->getLocale(),
'translation' => $catalogue->get($key, $domain),
];
}, $translatorBag->getCatalogues()),
];
}
}

$chunks = array_chunk($keys, 500);

foreach ($chunks as $chunk) {
$response = $this->client->request('POST', sprintf('/projects/%s/keys', $this->projectId), [
'json' => ['keys' => $chunk],
]);

if (200 !== $response->getStatusCode()) {
throw new ProviderException(sprintf('Unable to add keys and translations to Lokalise: "%s".', $response->getContent(false)), $response);
}
}
}

/**
* @see https://app.lokalise.com/api2docs/curl/#transition-download-files-post
*/
private function exportFiles(array $locales, array $domains): array
{
$response = $this->client->request('POST', sprintf('/projects/%s/files/export', $this->projectId), [
'json' => [
'format' => 'symfony_xliff',
'original_filenames' => true,
'directory_prefix' => '%LANG_ISO%',
'filter_langs' => array_values($locales),
'filter_filenames' => array_map([$this, 'generateLokaliseFilenameFromDomain'], $domains),
],
]);

$responseContent = $response->toArray(false);

if (406 === $response->getStatusCode()
&& 'No keys found with specified filenames.' === $responseContent['error']['message']
) {
return [];
}

if (200 !== $response->getStatusCode()) {
throw new ProviderException(sprintf('Unable to export translations from Lokalise: "%s".', $response->getContent(false)), $response);
}

return $responseContent['files'];
}

private function getKeysIds(array $keys, string $domain): array
{
$response = $this->client->request('GET', sprintf('/projects/%s/keys', $this->projectId), [
'query' => [
'filter_keys' => $keys,
'filter_filenames' => $this->generateLokaliseFilenameFromDomain($domain),
],
]);

$responseContent = $response->toArray(false);

if (200 !== $response->getStatusCode()) {
throw new ProviderException(sprintf('Unable to get keys ids from Lokalise: "%s".', $response->getContent(false)), $response);
}

return array_reduce($responseContent['keys'], function ($keysIds, array $keyItem) {
$keysIds[] = $keyItem['key_id'];

return $keysIds;
}, []);
}

private function generateLokaliseFilenameFromDomain(string $domain): string
{
return sprintf('%s.xliff', $domain);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Translation\Bridge\Lokalise\Provider;

use Psr\Log\LoggerInterface;
use Symfony\Component\Translation\Exception\UnsupportedSchemeException;
use Symfony\Component\Translation\Loader\LoaderInterface;
use Symfony\Component\Translation\Provider\AbstractProviderFactory;
use Symfony\Component\Translation\Provider\Dsn;
use Symfony\Component\Translation\Provider\ProviderInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
* @author Mathieu Santostefano <[email protected]>
*
* @experimental in 5.3
*/
final class LokaliseProviderFactory extends AbstractProviderFactory
{
public const SCHEME = 'lokalise';
private const HOST = 'api.lokalise.com/api2/';

private $client;
private $logger;
private $defaultLocale;
private $loader;

public function __construct(HttpClientInterface $client, LoggerInterface $logger, string $defaultLocale, LoaderInterface $loader)
{
$this->client = $client;
$this->logger = $logger;
$this->defaultLocale = $defaultLocale;
$this->loader = $loader;
}

/**
* @return LokaliseProvider
*/
public function create(Dsn $dsn): ProviderInterface
{
if (self::SCHEME === $dsn->getScheme()) {
$endpoint = sprintf('%s%s', 'default' === $dsn->getHost() ? self::HOST : $dsn->getHost(), $dsn->getPort() ? ':' . $dsn->getPort() : '');
$client = $this->client->withOptions([
'base_uri' => 'https://' . $endpoint,
'headers' => [
'X-Api-Token' => $this->getPassword($dsn),
],
]);

return new LokaliseProvider($this->getUser($dsn), $client, $this->loader, $this->logger, $this->defaultLocale, $endpoint);
}

throw new UnsupportedSchemeException($dsn, self::SCHEME, $this->getSupportedSchemes());
}

protected function getSupportedSchemes(): array
{
return [self::SCHEME];
}
}
Loading