Skip to content

Commit

Permalink
fix converting from symfony xliff => phraseapp format
Browse files Browse the repository at this point in the history
  • Loading branch information
prolic committed Jan 28, 2017
1 parent 4261dee commit e67ab75
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 233 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Translation\PlatformAdapter\PhraseApp\Bridge\Symfony\XliffFileDumper;
use Translation\PlatformAdapter\PhraseApp\PhraseApp;

/**
Expand All @@ -25,8 +24,6 @@ public function load(array $configs, ContainerBuilder $container)
$configuration = new Configuration($container);
$config = $this->processConfiguration($configuration, $configs);

$container->setParameter('translation.dumper.xliff.class', XliffFileDumper::class);

$requestBuilder = (new Definition(RequestBuilder::class))
->addArgument(new Reference($config['httplug_message_factory']));

Expand Down
12 changes: 4 additions & 8 deletions src/Bridge/Symfony/XliffConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
namespace Translation\PlatformAdapter\PhraseApp\Bridge\Symfony;

use Symfony\Component\Translation\MessageCatalogue;
use Translation\SymfonyStorage\Dumper\XliffDumper;
use Translation\SymfonyStorage\Loader\XliffLoader;

/**
* Utility class to convert between a MessageCatalogue and XLIFF file content.
*
* This class is used to use the custom XliffDumper
* This class can be removed when https://github.com/php-translation/symfony-storage/pull/8 is accepted and merged
*/
final class XliffConverter
{
Expand All @@ -33,19 +34,14 @@ public static function contentToCatalogue($content, $locale, $domain)
/**
* @param MessageCatalogue $catalogue
* @param string $domain
* @param array $options
*
* @return string
*/
public static function catalogueToContent(MessageCatalogue $catalogue, $domain, $defaultLocale = null)
public static function catalogueToContent(MessageCatalogue $catalogue, $domain, array $options = [])
{
$dumper = new XliffDumper();

if ($defaultLocale) {
$options = ['default_locale' => $defaultLocale];
} else {
$options = [];
}

return $dumper->getFormattedCatalogue($catalogue, $domain, $options);
}
}
29 changes: 0 additions & 29 deletions src/Bridge/Symfony/XliffDumper.php

This file was deleted.

191 changes: 0 additions & 191 deletions src/Bridge/Symfony/XliffFileDumper.php

This file was deleted.

28 changes: 26 additions & 2 deletions src/PhraseApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,17 @@ public function export(MessageCatalogueInterface $catalogue)
}

try {
$catalogue->addCatalogue(XliffConverter::contentToCatalogue($response, $locale, $domain));
$newCatalogue = XliffConverter::contentToCatalogue($response, $locale, $domain);

$messages = [];

foreach ($newCatalogue->all($domain) as $message => $translation) {
$messages[substr($message, strlen($domain) + 2)] = $translation;
}

$newCatalogue->replace($messages, $domain);

$catalogue->addCatalogue($newCatalogue);
} catch (\Throwable $e) {
// ignore empty translation files
}
Expand All @@ -110,7 +120,21 @@ public function import(MessageCatalogueInterface $catalogue)
$localeId = $this->getLocaleId($locale);

foreach ($this->domains as $domain) {
$data = XliffConverter::catalogueToContent($catalogue, $domain, $this->defaultLocale);
$messages = [];

foreach ($catalogue->all($domain) as $message => $translation) {
$messages[$domain . '::' . $message] = $translation;
}

$catalogue->replace($messages, $domain);

if ($this->defaultLocale) {
$options = ['default_locale' => $this->defaultLocale];
} else {
$options = [];
}

$data = XliffConverter::catalogueToContent($catalogue, $domain, $options);

$file = sys_get_temp_dir() . '/' . $domain . '.' . $locale . '.xlf';

Expand Down

0 comments on commit e67ab75

Please sign in to comment.