Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
S
Silex
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
common
Silex
Commits
38c84add
Commit
38c84add
authored
Nov 03, 2013
by
Fabien Potencier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
moved locale management to a locale service provider
parent
88e44ea4
Changes
9
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
86 additions
and
8 deletions
+86
-8
doc/changelog.rst
doc/changelog.rst
+2
-0
doc/providers/locale.rst
doc/providers/locale.rst
+24
-0
doc/providers/translation.rst
doc/providers/translation.rst
+1
-0
doc/services.rst
doc/services.rst
+0
-5
src/Silex/Application.php
src/Silex/Application.php
+0
-3
src/Silex/Provider/LocaleServiceProvider.php
src/Silex/Provider/LocaleServiceProvider.php
+46
-0
src/Silex/Provider/TranslationServiceProvider.php
src/Silex/Provider/TranslationServiceProvider.php
+4
-0
tests/Silex/Tests/LocaleTest.php
tests/Silex/Tests/LocaleTest.php
+7
-0
tests/Silex/Tests/Provider/TranslationServiceProviderTest.php
...s/Silex/Tests/Provider/TranslationServiceProviderTest.php
+2
-0
No files found.
doc/changelog.rst
View file @
38c84add
...
...
@@ -5,6 +5,8 @@ Changelog
------------------
* Updated session listeners to extends HttpKernel ones
* [BC BREAK] Locale management has been moved to LocaleServiceProvider which must be registered
if you want Silex to manage your locale (must also be registered for the translation service provider)
1.2.0 (2014-03-29)
...
...
doc/providers/locale.rst
0 → 100644
View file @
38c84add
LocaleServiceProvider
=====================
The *LocaleServiceProvider* manages the locale of an application.
Parameters
----------
* **locale**: The locale of the user. When set before any request handling, it
defines the default locale (``en`` by default). When a request is being
handled, it is automatically set according to the ``_locale`` request
attribute of the current route.
Services
--------
* n/a
Registering
-----------
.. code-block:: php
$app->register(new Silex\Provider\LocaleServiceProvider());
doc/providers/translation.rst
View file @
38c84add
...
...
@@ -37,6 +37,7 @@ Registering
.. code-block:: php
$app->register(new Silex\Provider\LocaleServiceProvider());
$app->register(new Silex\Provider\TranslationServiceProvider(), array(
'locale_fallbacks' => array('en'),
));
...
...
doc/services.rst
View file @
38c84add
...
...
@@ -253,11 +253,6 @@ Core parameters
This parameter can be used by the ``UrlGeneratorProvider``.
* **locale** (optional): The locale of the user. When set before any request
handling, it defines the default locale (``en`` by default). When a request
is being handled, it is automatically set according to the ``_locale``
request attribute of the current route.
* **debug** (optional): Returns whether or not the application is running in
debug mode.
...
...
src/Silex/Application.php
View file @
38c84add
...
...
@@ -30,7 +30,6 @@ use Symfony\Component\HttpFoundation\StreamedResponse;
use
Symfony\Component\HttpFoundation\JsonResponse
;
use
Symfony\Component\Routing\RouteCollection
;
use
Symfony\Component\Routing\RequestContext
;
use
Silex\EventListener\LocaleListener
;
use
Silex\EventListener\MiddlewareListener
;
use
Silex\EventListener\ConverterListener
;
use
Silex\EventListener\StringToResponseListener
;
...
...
@@ -94,7 +93,6 @@ class Application extends \Pimple implements HttpKernelInterface, TerminableInte
return
$app
[
'url_matcher'
];
});
$dispatcher
->
addSubscriber
(
new
RouterListener
(
$urlMatcher
,
$app
[
'request_context'
],
$app
[
'logger'
],
$app
[
'request_stack'
]));
$dispatcher
->
addSubscriber
(
new
LocaleListener
(
$app
,
$urlMatcher
,
$app
[
'request_stack'
]));
if
(
isset
(
$app
[
'exception_handler'
]))
{
$dispatcher
->
addSubscriber
(
$app
[
'exception_handler'
]);
}
...
...
@@ -147,7 +145,6 @@ class Application extends \Pimple implements HttpKernelInterface, TerminableInte
$this
[
'request.https_port'
]
=
443
;
$this
[
'debug'
]
=
false
;
$this
[
'charset'
]
=
'UTF-8'
;
$this
[
'locale'
]
=
'en'
;
foreach
(
$values
as
$key
=>
$value
)
{
$this
[
$key
]
=
$value
;
...
...
src/Silex/Provider/LocaleServiceProvider.php
0 → 100644
View file @
38c84add
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace
Silex\Provider
;
use
Silex\Application
;
use
Silex\LazyUrlMatcher
;
use
Silex\ServiceProviderInterface
;
use
Silex\EventListener\LocaleListener
;
/**
* Locale Provider.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class
LocaleServiceProvider
implements
ServiceProviderInterface
{
public
function
register
(
Application
$app
)
{
$app
[
'locale.listener'
]
=
$app
->
share
(
function
(
$app
)
{
$urlMatcher
=
null
;
if
(
isset
(
$app
[
'url_matcher'
]))
{
$urlMatcher
=
new
LazyUrlMatcher
(
function
()
use
(
$app
)
{
return
$app
[
'url_matcher'
];
});
}
return
new
LocaleListener
(
$app
,
$urlMatcher
,
$app
[
'request_stack'
]);
});
$app
[
'locale'
]
=
'en'
;
}
public
function
boot
(
Application
$app
)
{
$app
[
'dispatcher'
]
->
addSubscriber
(
$app
[
'locale.listener'
]);
}
}
src/Silex/Provider/TranslationServiceProvider.php
View file @
38c84add
...
...
@@ -28,6 +28,10 @@ class TranslationServiceProvider implements ServiceProviderInterface
public
function
register
(
Application
$app
)
{
$app
[
'translator'
]
=
$app
->
share
(
function
(
$app
)
{
if
(
!
isset
(
$app
[
'locale'
]))
{
throw
new
\LogicException
(
'You must register the LocaleServiceProvider to use the TranslationServiceProvider'
);
}
$translator
=
new
Translator
(
$app
,
$app
[
'translator.message_selector'
]);
// Handle deprecated 'locale_fallback'
...
...
tests/Silex/Tests/LocaleTest.php
View file @
38c84add
...
...
@@ -12,6 +12,7 @@
namespace
Silex\Tests
;
use
Silex\Application
;
use
Silex\Provider\LocaleServiceProvider
;
use
Symfony\Component\HttpFoundation\Request
;
use
Symfony\Component\HttpKernel\HttpKernelInterface
;
...
...
@@ -25,17 +26,20 @@ class LocaleTest extends \PHPUnit_Framework_TestCase
public
function
testLocale
()
{
$app
=
new
Application
();
$app
->
register
(
new
LocaleServiceProvider
());
$app
->
get
(
'/'
,
function
(
Request
$request
)
{
return
$request
->
getLocale
();
});
$response
=
$app
->
handle
(
Request
::
create
(
'/'
));
$this
->
assertEquals
(
'en'
,
$response
->
getContent
());
$app
=
new
Application
();
$app
->
register
(
new
LocaleServiceProvider
());
$app
[
'locale'
]
=
'fr'
;
$app
->
get
(
'/'
,
function
(
Request
$request
)
{
return
$request
->
getLocale
();
});
$response
=
$app
->
handle
(
Request
::
create
(
'/'
));
$this
->
assertEquals
(
'fr'
,
$response
->
getContent
());
$app
=
new
Application
();
$app
->
register
(
new
LocaleServiceProvider
());
$app
->
get
(
'/{_locale}'
,
function
(
Request
$request
)
{
return
$request
->
getLocale
();
});
$response
=
$app
->
handle
(
Request
::
create
(
'/es'
));
$this
->
assertEquals
(
'es'
,
$response
->
getContent
());
...
...
@@ -44,6 +48,7 @@ class LocaleTest extends \PHPUnit_Framework_TestCase
public
function
testLocaleInSubRequests
()
{
$app
=
new
Application
();
$app
->
register
(
new
LocaleServiceProvider
());
$app
->
get
(
'/embed/{_locale}'
,
function
(
Request
$request
)
{
return
$request
->
getLocale
();
});
$app
->
get
(
'/{_locale}'
,
function
(
Request
$request
)
use
(
$app
)
{
return
$request
->
getLocale
()
.
$app
->
handle
(
Request
::
create
(
'/embed/es'
),
HttpKernelInterface
::
SUB_REQUEST
)
->
getContent
()
.
$request
->
getLocale
();
...
...
@@ -52,6 +57,7 @@ class LocaleTest extends \PHPUnit_Framework_TestCase
$this
->
assertEquals
(
'fresfr'
,
$response
->
getContent
());
$app
=
new
Application
();
$app
->
register
(
new
LocaleServiceProvider
());
$app
->
get
(
'/embed'
,
function
(
Request
$request
)
{
return
$request
->
getLocale
();
});
$app
->
get
(
'/{_locale}'
,
function
(
Request
$request
)
use
(
$app
)
{
return
$request
->
getLocale
()
.
$app
->
handle
(
Request
::
create
(
'/embed'
),
HttpKernelInterface
::
SUB_REQUEST
)
->
getContent
()
.
$request
->
getLocale
();
...
...
@@ -64,6 +70,7 @@ class LocaleTest extends \PHPUnit_Framework_TestCase
public
function
testLocaleWithBefore
()
{
$app
=
new
Application
();
$app
->
register
(
new
LocaleServiceProvider
());
$app
->
before
(
function
(
Request
$request
)
use
(
$app
)
{
$request
->
setLocale
(
'fr'
);
});
$app
->
get
(
'/embed'
,
function
(
Request
$request
)
{
return
$request
->
getLocale
();
});
$app
->
get
(
'/'
,
function
(
Request
$request
)
use
(
$app
)
{
...
...
tests/Silex/Tests/Provider/TranslationServiceProviderTest.php
View file @
38c84add
...
...
@@ -13,6 +13,7 @@ namespace Silex\Tests\Provider;
use
Silex\Application
;
use
Silex\Provider\TranslationServiceProvider
;
use
Silex\Provider\LocaleServiceProvider
;
/**
* TranslationProvider test cases.
...
...
@@ -28,6 +29,7 @@ class TranslationServiceProviderTest extends \PHPUnit_Framework_TestCase
{
$app
=
new
Application
();
$app
->
register
(
new
LocaleServiceProvider
());
$app
->
register
(
new
TranslationServiceProvider
());
$app
[
'translator.domains'
]
=
array
(
'messages'
=>
array
(
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment