-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[Routing] i18n routing. #9515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Routing] i18n routing. #9515
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -136,6 +136,89 @@ use them later to :ref:`generate URLs <routing-generate>`. | |
configure your routes in YAML, XML or PHP, that's no problem! Just create a | ||
new routing file (e.g. ``routes.xml``) and Symfony will automatically use it. | ||
|
||
.. _i18n-routing: | ||
|
||
Localized Routing (i18n) | ||
------------------------ | ||
|
||
Routes can be localized to provide unique paths per *locale*. Symfony provides a | ||
handy way to declare localized routes without duplication. | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: php-annotations | ||
|
||
// src/Controller/BlogController.php | ||
namespace App\Controller; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | ||
use Symfony\Component\Routing\Annotation\Route; | ||
|
||
class BlogController extends Controller | ||
{ | ||
/** | ||
* Matches /blog exactly | ||
* | ||
* @Route("/blog", name="blog_list") | ||
*/ | ||
public function list() | ||
{ | ||
// ... | ||
} | ||
|
||
/** | ||
* Matches /blog/* | ||
* | ||
* @Route("/blog/{slug}", name="blog_show") | ||
*/ | ||
public function show($slug) | ||
{ | ||
// $slug will equal the dynamic part of the URL | ||
// e.g. at /blog/yay-routing, then $slug='yay-routing' | ||
|
||
// ... | ||
} | ||
} | ||
|
||
.. code-block:: yaml | ||
|
||
# config/routes.yaml | ||
about_us: | ||
path: | ||
nl: /over-ons | ||
en: /about-us | ||
controller: App\Controller\CompanyController::about | ||
|
||
.. code-block:: xml | ||
|
||
<!-- config/routes.xml --> | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<routes xmlns="http://symfony.com/schema/routing" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/routing | ||
http://symfony.com/schema/routing/routing-1.0.xsd"> | ||
|
||
<route id="about_us" controller="App\Controller\CompanyController::about"> | ||
<path locale="nl">/over-ons</path> | ||
<path locale="en">/about-us</path> | ||
</route> | ||
</routes> | ||
|
||
.. code-block:: php | ||
|
||
// config/routes.php | ||
namespace Symfony\Component\Routing\Loader\Configurator; | ||
|
||
return function (RoutingConfigurator $routes) { | ||
$routes->add('about_us', ['nl' => '/over-ons', 'en' => '/about-us']) | ||
->controller('App\Controller\CompanyController::about'); | ||
}; | ||
|
||
When a localized route is matched Symfony will automatically know which locale | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
should be used during the request. Defining routes this way also eliminated the | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "eliminates"? |
||
need for duplicate registration of routes which minimizes the risk for any bugs | ||
caused by definition inconsistency. | ||
|
||
.. _routing-requirements: | ||
|
||
Adding {wildcard} Requirements | ||
|
@@ -611,6 +694,18 @@ But if you pass extra ones, they will be added to the URI as a query string:: | |
)); | ||
// /blog/2?category=Symfony | ||
|
||
Generating Localized URLs | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be stripped to match the length of the headline |
||
|
||
When you've defined localized routes Symfony will use the current request locale | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [...] routes, Symfony [...] |
||
as the default when generating routes. In order to generate the route for alternative | ||
locale you must pass the ``_locale`` in the parameters array:: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "for alternative locales" or "for an alternative locale" |
||
|
||
$this->router->generate('about_us', array( | ||
'_locale' => 'nl', | ||
)); | ||
// /over-ons | ||
|
||
Generating URLs from a Template | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The annotation example look wrong to me. It should have the same routes as the YAML, XML, and PHP examples.