From 74a06f84b63e514536ce08f3c31e94dedd0c521f Mon Sep 17 00:00:00 2001 From: umpirsky Date: Thu, 13 Dec 2012 20:15:36 +0100 Subject: [PATCH] QtTranslationsLoader class renamed to QtFileLoader. --- .../Resources/config/translation.xml | 2 +- .../Component/Translation/CHANGELOG.md | 3 +- .../Translation/Loader/QtFileLoader.php | 95 +++++++++++++++++++ .../Loader/QtTranslationsLoader.php | 83 ++-------------- ...onsLoaderTest.php => QtFileLoaderTest.php} | 8 +- 5 files changed, 109 insertions(+), 82 deletions(-) create mode 100644 src/Symfony/Component/Translation/Loader/QtFileLoader.php rename src/Symfony/Component/Translation/Tests/Loader/{QtTranslationsLoaderTest.php => QtFileLoaderTest.php} (84%) diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/translation.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/translation.xml index 6a3778224b..48df23543c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/translation.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/translation.xml @@ -13,7 +13,7 @@ Symfony\Component\Translation\Loader\XliffFileLoader Symfony\Component\Translation\Loader\PoFileLoader Symfony\Component\Translation\Loader\MoFileLoader - Symfony\Component\Translation\Loader\QtTranslationsLoader + Symfony\Component\Translation\Loader\QtFileLoader Symfony\Component\Translation\Loader\CsvFileLoader Symfony\Component\Translation\Loader\IcuResFileLoader Symfony\Component\Translation\Loader\IcuDatFileLoader diff --git a/src/Symfony/Component/Translation/CHANGELOG.md b/src/Symfony/Component/Translation/CHANGELOG.md index a158f76edf..81402f557a 100644 --- a/src/Symfony/Component/Translation/CHANGELOG.md +++ b/src/Symfony/Component/Translation/CHANGELOG.md @@ -4,11 +4,12 @@ CHANGELOG 2.2.0 ----- + * QtTranslationsLoader class renamed to QtFileLoader. QtTranslationsLoader is deprecated and will be removed in 2.3. * [BC BREAK] uniformized the exception thrown by the load() method when an error occurs. The load() method now throws Symfony\Component\Translation\Exception\NotFoundResourceException when a resource cannot be found and Symfony\Component\Translation\Exception\InvalidResourceException when a resource is invalid. * changed the exception class thrown by some load() methods from \RuntimeException to \InvalidArgumentException - (IcuDatFileLoader, IcuResFileLoader, and QtTranslationsLoader) + (IcuDatFileLoader, IcuResFileLoader and QtFileLoader) 2.1.0 ----- diff --git a/src/Symfony/Component/Translation/Loader/QtFileLoader.php b/src/Symfony/Component/Translation/Loader/QtFileLoader.php new file mode 100644 index 0000000000..d64494b81c --- /dev/null +++ b/src/Symfony/Component/Translation/Loader/QtFileLoader.php @@ -0,0 +1,95 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Translation\Loader; + +use Symfony\Component\Translation\MessageCatalogue; +use Symfony\Component\Translation\Exception\InvalidResourceException; +use Symfony\Component\Translation\Exception\NotFoundResourceException; +use Symfony\Component\Config\Resource\FileResource; + +/** + * QtFileLoader loads translations from QT Translations XML files. + * + * @author Benjamin Eberlei + * + * @api + */ +class QtFileLoader implements LoaderInterface +{ + /** + * {@inheritdoc} + * + * @api + */ + public function load($resource, $locale, $domain = 'messages') + { + if (!stream_is_local($resource)) { + throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource)); + } + + if (!file_exists($resource)) { + throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource)); + } + + $dom = new \DOMDocument(); + $current = libxml_use_internal_errors(true); + if (!@$dom->load($resource, defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0)) { + throw new InvalidResourceException(implode("\n", $this->getXmlErrors())); + } + + $xpath = new \DOMXPath($dom); + $nodes = $xpath->evaluate('//TS/context/name[text()="'.$domain.'"]'); + + $catalogue = new MessageCatalogue($locale); + if ($nodes->length == 1) { + $translations = $nodes->item(0)->nextSibling->parentNode->parentNode->getElementsByTagName('message'); + foreach ($translations as $translation) { + $catalogue->set( + (string) $translation->getElementsByTagName('source')->item(0)->nodeValue, + (string) $translation->getElementsByTagName('translation')->item(0)->nodeValue, + $domain + ); + $translation = $translation->nextSibling; + } + $catalogue->addResource(new FileResource($resource)); + } + + libxml_use_internal_errors($current); + + return $catalogue; + } + + /** + * Returns the XML errors of the internal XML parser + * + * @return array An array of errors + */ + private function getXmlErrors() + { + $errors = array(); + foreach (libxml_get_errors() as $error) { + $errors[] = sprintf('[%s %s] %s (in %s - line %d, column %d)', + LIBXML_ERR_WARNING == $error->level ? 'WARNING' : 'ERROR', + $error->code, + trim($error->message), + $error->file ? $error->file : 'n/a', + $error->line, + $error->column + ); + } + + libxml_clear_errors(); + libxml_use_internal_errors(false); + + return $errors; + } +} diff --git a/src/Symfony/Component/Translation/Loader/QtTranslationsLoader.php b/src/Symfony/Component/Translation/Loader/QtTranslationsLoader.php index d15f3ce020..5c3dbba817 100644 --- a/src/Symfony/Component/Translation/Loader/QtTranslationsLoader.php +++ b/src/Symfony/Component/Translation/Loader/QtTranslationsLoader.php @@ -11,85 +11,16 @@ namespace Symfony\Component\Translation\Loader; -use Symfony\Component\Translation\MessageCatalogue; -use Symfony\Component\Translation\Exception\InvalidResourceException; -use Symfony\Component\Translation\Exception\NotFoundResourceException; -use Symfony\Component\Config\Resource\FileResource; - /** - * QtTranslationsLoader loads translations from QT Translations XML files. + * QtFileLoader loads translations from QT Translations XML files. * - * @author Benjamin Eberlei + * @author Саша Стаменковић * * @api + * + * @deprecated Deprecated since version 2.2, to be removed in 2.3. + * Use QtFileLoader instead. */ -class QtTranslationsLoader implements LoaderInterface -{ - /** - * {@inheritdoc} - * - * @api - */ - public function load($resource, $locale, $domain = 'messages') - { - if (!stream_is_local($resource)) { - throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource)); - } - - if (!file_exists($resource)) { - throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource)); - } - - $dom = new \DOMDocument(); - $current = libxml_use_internal_errors(true); - if (!@$dom->load($resource, defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0)) { - throw new InvalidResourceException(implode("\n", $this->getXmlErrors())); - } - - $xpath = new \DOMXPath($dom); - $nodes = $xpath->evaluate('//TS/context/name[text()="'.$domain.'"]'); - - $catalogue = new MessageCatalogue($locale); - if ($nodes->length == 1) { - $translations = $nodes->item(0)->nextSibling->parentNode->parentNode->getElementsByTagName('message'); - foreach ($translations as $translation) { - $catalogue->set( - (string) $translation->getElementsByTagName('source')->item(0)->nodeValue, - (string) $translation->getElementsByTagName('translation')->item(0)->nodeValue, - $domain - ); - $translation = $translation->nextSibling; - } - $catalogue->addResource(new FileResource($resource)); - } - - libxml_use_internal_errors($current); - - return $catalogue; - } - - /** - * Returns the XML errors of the internal XML parser - * - * @return array An array of errors - */ - private function getXmlErrors() - { - $errors = array(); - foreach (libxml_get_errors() as $error) { - $errors[] = sprintf('[%s %s] %s (in %s - line %d, column %d)', - LIBXML_ERR_WARNING == $error->level ? 'WARNING' : 'ERROR', - $error->code, - trim($error->message), - $error->file ? $error->file : 'n/a', - $error->line, - $error->column - ); - } - - libxml_clear_errors(); - libxml_use_internal_errors(false); - - return $errors; - } +class QtTranslationsLoader extends QtFileLoader +{ } diff --git a/src/Symfony/Component/Translation/Tests/Loader/QtTranslationsLoaderTest.php b/src/Symfony/Component/Translation/Tests/Loader/QtFileLoaderTest.php similarity index 84% rename from src/Symfony/Component/Translation/Tests/Loader/QtTranslationsLoaderTest.php rename to src/Symfony/Component/Translation/Tests/Loader/QtFileLoaderTest.php index aa1f0c4852..1dea7c91a7 100644 --- a/src/Symfony/Component/Translation/Tests/Loader/QtTranslationsLoaderTest.php +++ b/src/Symfony/Component/Translation/Tests/Loader/QtFileLoaderTest.php @@ -11,10 +11,10 @@ namespace Symfony\Component\Translation\Tests\Loader; -use Symfony\Component\Translation\Loader\QtTranslationsLoader; +use Symfony\Component\Translation\Loader\QtFileLoader; use Symfony\Component\Config\Resource\FileResource; -class QtTranslationsLoaderTest extends \PHPUnit_Framework_TestCase +class QtFileLoaderTest extends \PHPUnit_Framework_TestCase { protected function setUp() { @@ -25,7 +25,7 @@ class QtTranslationsLoaderTest extends \PHPUnit_Framework_TestCase public function testLoad() { - $loader = new QtTranslationsLoader(); + $loader = new QtFileLoader(); $resource = __DIR__.'/../fixtures/resources.ts'; $catalogue = $loader->load($resource, 'en', 'resources'); @@ -39,7 +39,7 @@ class QtTranslationsLoaderTest extends \PHPUnit_Framework_TestCase */ public function testLoadNonExistingResource() { - $loader = new QtTranslationsLoader(); + $loader = new QtFileLoader(); $resource = __DIR__.'/../fixtures/non-existing.ts'; $loader->load($resource, 'en', 'domain1'); }