|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Translation\Bridge\PoEditor; |
| 13 | + |
| 14 | +use Psr\Log\LoggerInterface; |
| 15 | +use Symfony\Component\Translation\Exception\ProviderException; |
| 16 | +use Symfony\Component\Translation\Loader\LoaderInterface; |
| 17 | +use Symfony\Component\Translation\Provider\ProviderInterface; |
| 18 | +use Symfony\Component\Translation\TranslatorBag; |
| 19 | +use Symfony\Component\Translation\TranslatorBagInterface; |
| 20 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 21 | + |
| 22 | +/** |
| 23 | + * @author Mathieu Santostefano <msantostefano@protonmail.com> |
| 24 | + * |
| 25 | + * In PoEditor: |
| 26 | + * * Terms refer to Symfony's translation keys; |
| 27 | + * * Translations refer to Symfony's translated messages; |
| 28 | + * * Context fields refer to Symfony's translation domains |
| 29 | + * |
| 30 | + * PoEditor's API always returns 200 status code, even in case of failure. |
| 31 | + * |
| 32 | + * @experimental in 5.3 |
| 33 | + */ |
| 34 | +final class PoEditorProvider implements ProviderInterface |
| 35 | +{ |
| 36 | + private $apiKey; |
| 37 | + private $projectId; |
| 38 | + private $client; |
| 39 | + private $loader; |
| 40 | + private $logger; |
| 41 | + private $defaultLocale; |
| 42 | + private $endpoint; |
| 43 | + |
| 44 | + public function __construct(string $apiKey, string $projectId, HttpClientInterface $client, LoaderInterface $loader, LoggerInterface $logger, string $defaultLocale, string $endpoint) |
| 45 | + { |
| 46 | + $this->apiKey = $apiKey; |
| 47 | + $this->projectId = $projectId; |
| 48 | + $this->client = $client; |
| 49 | + $this->loader = $loader; |
| 50 | + $this->logger = $logger; |
| 51 | + $this->defaultLocale = $defaultLocale; |
| 52 | + $this->endpoint = $endpoint; |
| 53 | + } |
| 54 | + |
| 55 | + public function __toString(): string |
| 56 | + { |
| 57 | + return sprintf('poeditor://%s', $this->endpoint); |
| 58 | + } |
| 59 | + |
| 60 | + public function write(TranslatorBagInterface $translatorBag): void |
| 61 | + { |
| 62 | + $defaultCatalogue = $translatorBag->getCatalogue($this->defaultLocale); |
| 63 | + |
| 64 | + if (!$defaultCatalogue) { |
| 65 | + $defaultCatalogue = $translatorBag->getCatalogues()[0]; |
| 66 | + } |
| 67 | + |
| 68 | + $terms = $translationsToAdd = []; |
| 69 | + foreach ($defaultCatalogue->all() as $domain => $messages) { |
| 70 | + foreach ($messages as $id => $message) { |
| 71 | + $terms[] = [ |
| 72 | + 'term' => $id, |
| 73 | + 'reference' =>
10000
; $id, |
| 74 | + // tags field is mandatory to export all translations in read method. |
| 75 | + 'tags' => [$domain], |
| 76 | + 'context' => $domain, |
| 77 | + ]; |
| 78 | + } |
| 79 | + } |
| 80 | + $this->addTerms($terms); |
| 81 | + |
| 82 | + foreach ($translatorBag->getCatalogues() as $catalogue) { |
| 83 | + $locale = $catalogue->getLocale(); |
| 84 | + foreach ($catalogue->all() as $domain => $messages) { |
| 85 | + foreach ($messages as $id => $message) { |
| 86 | + $translationsToAdd[$locale][] = [ |
| 87 | + 'term' => $id, |
| 88 | + 'context' => $domain, |
| 89 | + 'translation' => [ |
| 90 | + 'content' => $message, |
| 91 | + ], |
| 92 | + ]; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + $this->addTranslations($translationsToAdd); |
| 98 | + } |
| 99 | + |
| 100 | + public function read(array $domains, array $locales): TranslatorBag |
| 101 | + { |
| 102 | + $translatorBag = new TranslatorBag(); |
| 103 | + $exportResponses = $downloadResponses = []; |
| 104 | + |
| 105 | + foreach ($locales as $locale) { |
| 106 | + foreach ($domains as $domain) { |
| 107 | + $exportResponses[] = [ |
| 108 | + 'response' => $this->client->request('POST', 'projects/export', [ |
| 109 | + 'body' => [ |
| 110 | + 'api_token' => $this->apiKey, |
| 111 | + 'id' => $this->projectId, |
| 112 | + 'language' => $locale, |
| 113 | + 'type' => 'xlf', |
| 114 | + 'filters' => json_encode(['translated']), |
| 115 | + 'tags' => json_encode([$domain]), |
| 116 | + ], |
| 117 | + ]), |
| 118 | + 'locale' => $locale, |
| 119 | + 'domain' => $domain, |
| 120 | + ]; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + foreach ($exportResponses as $exportResponse) { |
| 125 | + $response = $exportResponse['response']; |
| 126 | + $responseContent = $response->toArray(false); |
| 127 | + |
| 128 | + if (200 !== $response->getStatusCode() || '200' !== (string) $responseContent['response']['code']) { |
| 129 | + $this->logger->error('Unable to read the PoEditor response: '.$response->getContent(false)); |
| 130 | + continue; |
| 131 | + } |
| 132 | + |
| 133 | + $fileUrl = $responseContent['result']['url']; |
| 134 | + $downloadResponses[] = [ |
| 135 | + 'response' => $this->client->request('GET', $fileUrl), |
| 136 | + 'locale' => $exportResponse['locale'], |
| 137 | + 'domain' => $exportResponse['domain'], |
| 138 | + 'fileUrl' => $fileUrl, |
| 139 | + ]; |
| 140 | + } |
| 141 | + |
| 142 | + foreach ($downloadResponses as $downloadResponse) { |
| 143 | + $response = $downloadResponse['response']; |
| 144 | + $locale = $downloadResponse['locale']; |
| 145 | + $domain = $downloadResponse['domain']; |
| 146 | + $fileUrl = $downloadResponse['fileUrl']; |
| 147 | + $responseContent = $response->getContent(false); |
| 148 | + |
| 149 | + if (200 !== $response->getStatusCode()) { |
| 150 | + $this->logger->error('Unable to download the PoEditor exported file: '.$responseContent); |
| 151 | + continue; |
| 152 | + } |
| 153 | + |
| 154 | + if (!$responseContent) { |
| 155 | + $this->logger->error(sprintf('The exported file "%s" from PoEditor is empty.', $fileUrl)); |
| 156 | + continue; |
| 157 | + } |
| 158 | + |
| 159 | + $translatorBag->addCatalogue($
10000
this->loader->load($responseContent, $locale, $domain)); |
| 160 | + } |
| 161 | + |
| 162 | + return $translatorBag; |
| 163 | + } |
| 164 | + |
| 165 | + public function delete(TranslatorBagInterface $translatorBag): void |
| 166 | + { |
| 167 | + $deletedIds = $termsToDelete = []; |
| 168 | + |
| 169 | + foreach ($translatorBag->getCatalogues() as $catalogue) { |
| 170 | + foreach ($catalogue->all() as $domain => $messages) { |
| 171 | + foreach ($messages as $id => $message) { |
| 172 | + if (\array_key_exists($domain, $deletedIds) && \in_array($id, $deletedIds[$domain], true)) { |
| 173 | + continue; |
| 174 | + } |
| 175 | + |
| 176 | + $deletedIds[$domain][] = $id; |
| 177 | + $termsToDelete[] = [ |
| 178 | + 'term' => $id, |
| 179 | + 'context' => $domain, |
| 180 | + ]; |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + $this->deleteTerms($termsToDelete); |
| 186 | + } |
| 187 | + |
| 188 | + private function addTerms(array $terms): void |
| 189 | + { |
| 190 | + $response = $this->client->request('POST', 'terms/add', [ |
| 191 | + 'body' => [ |
| 192 | + 'api_token' => $this->apiKey, |
| 193 | + 'id' => $this->projectId, |
| 194 | + 'data' => json_encode($terms), |
| 195 | + ], |
| 196 | + ]); |
| 197 | + |
| 198 | + if (200 !== $response->getStatusCode() || '200' !== (string) $response->toArray(false)['response']['code']) { |
| 199 | + throw new ProviderException(sprintf('Unable to add new translation keys to PoEditor: (status code: "%s") "%s".', $response->getStatusCode(), $response->getContent(false)), $response); |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + private function addTranslations(array $translationsPerLocale): void |
| 204 | + { |
| 205 | + $responses = []; |
| 206 | + |
| 207 | + foreach ($translationsPerLocale as $locale => $translations) { |
| 208 | + $responses = $this->client->request('POST', 'translations/add', [ |
| 209 | + 'body' => [ |
| 210 | + 'api_token' => $this->apiKey, |
| 211 | + 'id' => $this->projectId, |
| 212 | + 'language' => $locale, |
| 213 | + 'data' => json_encode($translations), |
| 214 | + ], |
| 215 | + ]); |
| 216 | + } |
| 217 | + |
| 218 | + foreach ($responses as $response) { |
| 219 | + if (200 !== $response->getStatusCode() || '200' !== (string) $response->toArray(false)['response']['code']) { |
| 220 | + $this->logger->error(sprintf('Unable to add translation messages to PoEditor: "%s".', $response->getContent(false))); |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + private function deleteTerms(array $ids): void |
| 226 | + { |
| 227 | + $response = $this->client->request('POST', 'terms/delete', [ |
| 228 | + 'body' => [ |
| 229 | + 'api_token' => $this->apiKey, |
| 230 | + 'id' => $this->projectId, |
| 231 | + 'data' => json_encode($ids), |
| 232 | + ], |
| 233 | + ]); |
| 234 | + |
| 235 | + if (200 !== $response->getStatusCode() || '200' !== (string) $response->toArray(false)['response']['code']) { |
| 236 | + throw new ProviderException(sprintf('Unable to delete translation keys on PoEditor: "%s".', $response->getContent(false)), $response); |
| 237 | + } |
| 238 | + } |
| 239 | +} |
0 commit comments