diff --git a/src/FileStorage.php b/src/FileStorage.php index 818b644..8c99dcf 100644 --- a/src/FileStorage.php +++ b/src/FileStorage.php @@ -16,6 +16,7 @@ use Symfony\Component\Translation\MessageCatalogueInterface; use Symfony\Component\Translation\Reader\TranslationReader; use Symfony\Component\Translation\Writer\TranslationWriter; +use Symfony\Component\Translation\Writer\TranslationWriterInterface; use Translation\Common\Model\Message; use Translation\Common\Storage; use Translation\Common\TransferableStorage; @@ -28,7 +29,7 @@ final class FileStorage implements Storage, TransferableStorage { /** - * @var TranslationWriter + * @var TranslationWriterInterface|LegacyTranslationWriter */ private $writer; @@ -60,6 +61,10 @@ final class FileStorage implements Storage, TransferableStorage */ public function __construct(TranslationWriter $writer, $loader, array $dir, array $options = []) { + // Create a legacy writer which is a wrapper for TranslationWriter + if (!$writer instanceof TranslationWriterInterface) { + $writer = new LegacyTranslationWriter($writer); + } // Create a legacy loader which is a wrapper for TranslationReader if ($loader instanceof TranslationReader) { $loader = new LegacyTranslationLoader($loader); @@ -164,7 +169,7 @@ private function writeCatalogue(MessageCatalogue $catalogue, $locale, $domain) $path = (string) $resource; if (preg_match('|/'.$domain.'\.'.$locale.'\.([a-z]+)$|', $path, $matches)) { $options['path'] = str_replace($matches[0], '', $path); - $this->writeTranslations($catalogue, $matches[1], $options); + $this->writer->write($catalogue, $matches[1], $options); $written = true; } } @@ -176,7 +181,7 @@ private function writeCatalogue(MessageCatalogue $catalogue, $locale, $domain) $options['path'] = reset($this->dir); $format = isset($options['default_output_format']) ? $options['default_output_format'] : 'xlf'; - $this->writeTranslations($catalogue, $format, $options); + $this->writer->write($catalogue, $format, $options); } /** @@ -210,23 +215,4 @@ private function loadCatalogue($locale, array $dirs) $this->catalogues[$locale] = $currentCatalogue; } - - /** - * This method calls the new TranslationWriter::write() if exist, - * otherwise fallback to TranslationWriter::writeTranslations() call - * to avoid BC breaks. - * - * @param MessageCatalogue $catalogue - * @param string $format - * @param array $options - */ - private function writeTranslations(MessageCatalogue $catalogue, $format, array $options) - { - if (method_exists($this->writer, 'write')) { - $this->writer->write($catalogue, $format, $options); - } else { - // This method is deprecated since 3.4, maintained to avoid BC breaks - $this->writer->writeTranslations($catalogue, $format, $options); - } - } } diff --git a/src/LegacyTranslationLoader.php b/src/LegacyTranslationLoader.php index 9f5dca1..add29c2 100644 --- a/src/LegacyTranslationLoader.php +++ b/src/LegacyTranslationLoader.php @@ -16,7 +16,7 @@ /** * This loader is just a legacy wrapper for Symfony TranslationReader - * and provider a BC layer for Symfony 4. + * and provide a BC layer for Symfony 4. * * @author Victor Bocharsky */ diff --git a/src/LegacyTranslationWriter.php b/src/LegacyTranslationWriter.php new file mode 100644 index 0000000..7a0fdfc --- /dev/null +++ b/src/LegacyTranslationWriter.php @@ -0,0 +1,39 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Translation\SymfonyStorage; + +use Symfony\Component\Translation\MessageCatalogue; +use Symfony\Component\Translation\Writer\TranslationWriter; + +/** + * This writer is just a legacy wrapper for Symfony TranslationWriter + * and provide a BC layer for Symfony 4. + * + * @author Victor Bocharsky + */ +class LegacyTranslationWriter +{ + /** + * @var TranslationWriter + */ + private $writer; + + public function __construct(TranslationWriter $writer) + { + $this->writer = $writer; + } + + public function write(MessageCatalogue $catalogue, $format, $options = []) + { + $this->writer->writeTranslations($catalogue, $format, $options); + } +}