Skip to content

[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

Merged
merged 4 commits into from
May 27, 2018
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions routing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Copy link
Member

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.

*/
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a localized route is matched, Symfony will automatically know, which ... and perhaps matches instead of is matched?

should be used during the request. Defining routes this way also eliminated the
Copy link
Member

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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::
Copy link
Member

Choose a reason for hiding this comment

The 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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down