Commit b4bb327c authored by Fabien Potencier's avatar Fabien Potencier

merged branch project-a/master (PR #791)

This PR was merged into the master branch.

Discussion
----------

Change deprecated functions and remove unused imports

I changed the deprecated translation function setFallbackLocale() and the debug Exception- and Error-Handler.
Therefore i added symfony/debug to composer.json.

I also removed unused imports and fixed some classnames in docblock comments.

Commits
-------

d214e2e0 Test for TranslationServiceProvider
67c7e3fe Handle locale_fallback
380792b9 Also update doc for translation changes
60baeadc Remove unused imports and fix docblock for exceptions
69756f95 Use new debug component
92d4f0f1 Remove usage of deprecated function setFallbackLocale
parents 5e957d33 d214e2e0
......@@ -13,7 +13,7 @@ Parameters
* **locale** (optional): The locale for the translator. You will most likely
want to set this based on some request parameter. Defaults to ``en``.
* **locale_fallback** (optional): Fallback locale for the translator. It will
* **locale_fallbacks** (optional): Fallback locales for the translator. It will
be used when the current locale has no messages set. Defaults to ``en``.
Services
......@@ -38,7 +38,7 @@ Registering
.. code-block:: php
$app->register(new Silex\Provider\TranslationServiceProvider(), array(
'locale_fallback' => 'en',
'locale_fallbacks' => array('en'),
));
.. note::
......
......@@ -11,7 +11,7 @@
namespace Silex;
use Symfony\Component\HttpKernel\Debug\ExceptionHandler as DebugExceptionHandler;
use Symfony\Component\Debug\ExceptionHandler as DebugExceptionHandler;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\KernelEvents;
......
......@@ -31,7 +31,12 @@ class TranslationServiceProvider implements ServiceProviderInterface
$app['translator'] = $app->share(function ($app) {
$translator = new Translator($app['locale'], $app['translator.message_selector']);
$translator->setFallbackLocale($app['locale_fallback']);
// Handle deprecated 'locale_fallback'
if (isset($app['locale_fallback'])) {
$app['locale_fallbacks'] = (array) $app['locale_fallback'];
}
$translator->setFallbackLocales($app['locale_fallbacks']);
$translator->addLoader('array', new ArrayLoader());
$translator->addLoader('xliff', new XliffFileLoader());
......@@ -51,7 +56,9 @@ class TranslationServiceProvider implements ServiceProviderInterface
$app['translator.domains'] = array();
$app['locale_fallback'] = 'en';
$app['locale_fallbacks'] = array(
'en'
);
}
public function boot(Application $app)
......
......@@ -18,9 +18,8 @@ use Silex\Provider\MonologServiceProvider;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Debug\ErrorHandler;
use Symfony\Component\Debug\ErrorHandler;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\EventDispatcher\Event;
......@@ -378,7 +377,7 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
}
/**
* @expectedException RuntimeException
* @expectedException \RuntimeException
*/
public function testNonResponseAndNonNullReturnFromRouteBeforeMiddlewareShouldThrowRuntimeException()
{
......@@ -397,7 +396,7 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
}
/**
* @expectedException RuntimeException
* @expectedException \RuntimeException
*/
public function testNonResponseAndNonNullReturnFromRouteAfterMiddlewareShouldThrowRuntimeException()
{
......@@ -416,7 +415,7 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
}
/**
* @expectedException RuntimeException
* @expectedException \RuntimeException
*/
public function testAccessingRequestOutsideOfScopeShouldThrowRuntimeException()
{
......@@ -426,7 +425,7 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
}
/**
* @expectedException RuntimeException
* @expectedException \RuntimeException
*/
public function testAccessingRequestOutsideOfScopeShouldThrowRuntimeExceptionAfterHandling()
{
......
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Silex\Tests\Provider;
use Silex\Application;
use Silex\Provider\TranslationServiceProvider;
use Symfony\Component\HttpFoundation\Request;
/**
* TranslationProvider test cases.
*
* @author Daniel Tschinder <daniel@tschinder.de>
*/
class TranslationServiceProviderTest extends \PHPUnit_Framework_TestCase
{
/**
* @return Application
*/
protected function getPreparedApp()
{
$app = new Application();
$app->register(new TranslationServiceProvider());
$app['translator.domains'] = array(
'messages' => array(
'en' => array (
'key1' => 'The translation',
'key_only_english' => 'Foo',
'key2' => 'One apple|%count% apples',
'test' => array(
'key' => 'It works'
)
),
'de' => array (
'key1' => 'The german translation',
'key2' => 'One german apple|%count% german apples',
'test' => array(
'key' => 'It works in german'
)
)
)
);
return $app;
}
public function transChoiceProvider()
{
return array(
array('key2', 0, null, '0 apples'),
array('key2', 1, null, 'One apple'),
array('key2', 2, null, '2 apples'),
array('key2', 0, 'de', '0 german apples'),
array('key2', 1, 'de', 'One german apple'),
array('key2', 2, 'de', '2 german apples'),
array('key2', 0, 'ru', '0 apples'), // fallback
array('key2', 1, 'ru', 'One apple'), // fallback
array('key2', 2, 'ru', '2 apples'), // fallback
);
}
public function transProvider()
{
return array(
array('key1', null, 'The translation'),
array('key1', 'de', 'The german translation'),
array('key1', 'ru', 'The translation'), // fallback
array('test.key', null, 'It works'),
array('test.key', 'de', 'It works in german'),
array('test.key', 'ru', 'It works'), // fallback
);
}
/**
* @dataProvider transProvider
*/
public function testTransForDefaultLanguage($key, $locale, $expected)
{
$app = $this->getPreparedApp();
$result = $app['translator']->trans($key, array(), null, $locale);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider transChoiceProvider
*/
public function testTransChoiceForDefaultLanguage($key, $number, $locale, $expected)
{
$app = $this->getPreparedApp();
$result = $app['translator']->transChoice($key, $number, array('%count%' => $number), null, $locale);
$this->assertEquals($expected, $result);
}
public function testBackwardCompatiblityForFallback()
{
$app = $this->getPreparedApp();
$app['locale_fallback'] = 'de';
$result = $app['translator']->trans('key1', array(), null, 'ru');
$this->assertEquals('The german translation', $result);
}
public function testFallbacks()
{
$app = $this->getPreparedApp();
$app['locale_fallbacks'] = array('de', 'en');
// fallback to english
$result = $app['translator']->trans('key_only_english', array(), null, 'ru');
$this->assertEquals('Foo', $result);
// fallback to german
$result = $app['translator']->trans('key1', array(), null, 'ru');
$this->assertEquals('The german translation', $result);
}
}
......@@ -16,7 +16,6 @@ use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Router test cases.
......@@ -97,7 +96,7 @@ class RouterTest extends \PHPUnit_Framework_TestCase
}
/**
* @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException
* @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public function testMissingRoute()
{
......
......@@ -14,7 +14,6 @@ namespace Silex\Tests;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\StreamedResponse;
/**
* Stream test cases.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment