我正在使用Symfony 2.8的WebApp項目。我想爲頁面添加不同的語言。根據用戶區域設置,所有路由/ URL應該從/some/url
更改爲/ locale/some/url , e.g.
/zh/some/url`。Symfony Routing:何時_locale屬性設置爲
添加主路由文件看起來像這樣的語言之前:
<?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">
<!-- Routes for the public pages -->
<import
resource="@AppBundle/Resources/config/routing/public_routes.xml" />
...
</routes>
而且public_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="home" path="" methods="GET">
<default key="_controller">AppBundle:Default:home</default>
</route>
<route id="public_price" path="/price" methods="GET">
<default key="_controller">AppBundle:Default:price</default>
</route>
<route id="public_about" path="/about" methods="GET">
<default key="_controller">AppBundle:Default:about</default>
</route>
...
</routes>
到目前爲止,就這麼簡單。現在,我已經添加了以下到路由添加本地化:
<?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">
<!-- Home route to redirect to the right route -->
<route id="home_redirect" path="" methods="GET">
<default key="_controller">AppBundle:Default:home</default>
</route>
<!-- Routes for the localized public pages -->
<import
resource="@AppBundle/Resources/config/routing/public_routes.xml"
prefix="/{_locale}" />
...
</routes>
所以公共頁面的進口被擴展,prefix="/{_locale}"
它會自動將當前的區域從public_routes.xml
所有路由。
這工作正常。我現在可以導航到/en
,/en/price
,/en/about
等
然而,仍然必須是一個「空」的路線。否則,域本身將不再有效的路由。 (BTW:有path=""
和path="/"
之間有什麼區別?)
這是通過其控制的新home_redirect
路線handeled:
public function homeAction(Request $request) {
$locale = $request->attributes->get('_locale');
if (!$locale) {
// Try to get the preferred language from the request
$locale = MySettings::getLanguage($request);
return $this->redirectToRoute('home', array('_locale' => $locale));
} elseif (!MySettings::checkLanguage($locale)) {
// Invalid Locale...
throw $this->createNotFoundException();
}
return $this->render('AppBundle:Default:homepage.html.twig');
}
所以,如果_locale
設置爲請求,homeAction
檢查如果語言被支持或拋出錯誤。如果未設置_locale
,homeAction
會嘗試從請求(例如,來自瀏覽器接受的語言)獲取當前_locale。
問題: 上述解決方案適用於所有呼叫到/
。在這種情況下,_locale
未被設置,因此該呼叫被重定向到/someLocale/
,例如, `/ EN /。
但是:所有對/somepage
的呼叫也由homeAction
處理。這是因爲somepage
被解釋爲語言環境!因此homeAction
被調用$request->attributes->get('_locale') == 'somepage'
。這顯然不能工作。
因此,如果有人呼籲舊的URL,而不是新/en/about
,路由系統這一呼籲與路線home
匹配與_locale about
和頁面沒有找到。
Symfony從URL中提取_locale
的位置?是否有可能攔截此提取並使其更加智能一點?例如。忽略長度超過2個字母的所有「語言環境」?
謝謝!以「FR」如果沒有_locale給予
您是否嘗試過設置'requirements = {'_ locale':'en | fr | ...'}'?可能不是一個完美的解決方案,但它應該工作... –