Skip to content

Commit f4acc0d

Browse files
committed
minor #9515 [Routing] i18n routing. (frankdejonge, javiereguiluz)
This PR was merged into the 4.1 branch. Discussion ---------- [Routing] i18n routing. Commits ------- fe8027c Minor tweak 2341bf9 Minor rewords and fixes f7c8839 handle => handy 87be2eb Initial docs on i18n routing.
2 parents ced5949 + fe8027c commit f4acc0d

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

routing.rst

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,80 @@ use them later to :ref:`generate URLs <routing-generate>`.
136136
configure your routes in YAML, XML or PHP, that's no problem! Just create a
137137
new routing file (e.g. ``routes.xml``) and Symfony will automatically use it.
138138

139+
.. _i18n-routing:
140+
141+
Localized Routing (i18n)
142+
------------------------
143+
144+
.. versionadded:: 4.1
145+
The feature to localize routes was introduced in Symfony 4.1.
146+
147+
Routes can be localized to provide unique paths per :doc:`locale </translation/locale>`.
148+
Symfony provides a handy way to declare localized routes without duplication.
149+
150+
.. configuration-block::
151+
152+
.. code-block:: php-annotations
153+
154+
// src/Controller/BlogController.php
155+
namespace App\Controller;
156+
157+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
158+
use Symfony\Component\Routing\Annotation\Route;
159+
160+
class CompanyController extends Controller
161+
{
162+
/**
163+
* @Route({
164+
* "nl": "/over-ons",
165+
* "en": "/about-us"
166+
* }, name="about_us")
167+
*/
168+
public function about()
169+
{
170+
// ...
171+
}
172+
}
173+
174+
.. code-block:: yaml
175+
176+
# config/routes.yaml
177+
about_us:
178+
path:
179+
nl: /over-ons
180+
en: /about-us
181+
controller: App\Controller\CompanyController::about
182+
183+
.. code-block:: xml
184+
185+
<!-- config/routes.xml -->
186+
<?xml version="1.0" encoding="UTF-8" ?>
187+
<routes xmlns="http://symfony.com/schema/routing"
188+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
189+
xsi:schemaLocation="http://symfony.com/schema/routing
190+
http://symfony.com/schema/routing/routing-1.0.xsd">
191+
192+
<route id="about_us" controller="App\Controller\CompanyController::about">
193+
<path locale="nl">/over-ons</path>
194+
<path locale="en">/about-us</path>
195+
</route>
196+
</routes>
197+
198+
.. code-block:: php
199+
200+
// config/routes.php
201+
namespace Symfony\Component\Routing\Loader\Configurator;
202+
203+
return function (RoutingConfigurator $routes) {
204+
$routes->add('about_us', ['nl' => '/over-ons', 'en' => '/about-us'])
205+
->controller('App\Controller\CompanyController::about');
206+
};
207+
208+
When a localized route is matched Symfony automatically knows which locale
209+
should be used during the request. Defining routes this way also eliminated the
210+
need for duplicate registration of routes which minimizes the risk for any bugs
211+
caused by definition inconsistency.
212+
139213
.. _routing-requirements:
140214

141215
Adding {wildcard} Requirements
@@ -745,6 +819,18 @@ But if you pass extra ones, they will be added to the URI as a query string::
745819
));
746820
// /blog/2?category=Symfony
747821

822+
Generating Localized URLs
823+
~~~~~~~~~~~~~~~~~~~~~~~~~
824+
825+
When a route is localized, Symfony uses by default the current request locale to
826+
generate the URL. In order to generate the URL for a different locale you must
827+
pass the ``_locale`` in the parameters array::
828+
829+
$this->router->generate('about_us', array(
830+
'_locale' => 'nl',
831+
));
832+
// generates: /over-ons
833+
748834
Generating URLs from a Template
749835
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
750836

0 commit comments

Comments
 (0)