如何在 Laravel 中创建多语言翻译路线 [英] How to create multilingual translated routes in Laravel
问题描述
我想根据所选语言创建具有许多翻译路线的应用程序.我曾经在在多语言网站中创建 URL 的 3 种方法中描述过它.
在这种情况下,它应该是提到的主题中的第一个方法所以:
- 我有一种默认语言
- 我会说很多其他语言
- 当前语言应仅通过 URL(无 cookie/会话)计算,以使其对搜索引擎也非常友好
- 对于默认语言,URL 中不应有前缀,对于其他语言,应在域后添加语言前缀
- url 的每一部分都应根据当前语言进行翻译.
假设我已经设置了默认语言 pl
和 2 种其他语言 en
和 fr
.我只有 3 个页面 - 主页、联系页面和关于页面.
网站的网址应该是这样的:
<预><代码>//[关于]/[接触]/zh/zh/[关于]/zh/[联系方式]/fr/fr/[关于]/fr/[联系方式]而 [about]
和 [contact]
应根据所选语言进行翻译,例如在英语中应保留 contact
但对于波兰语,它应该是 kontakt
等等.
如何做到尽可能简单?
第一步:
转到 app/lang
目录并在此处为您的每种语言的路由创建翻译.您需要创建 3 个 routes.php
文件 - 每个文件都在单独的语言目录 (pl/en/fr) 中,因为您想使用 3 种语言
波兰语:
'联系','关于' =>'o-nas');
英语:
'接触','关于' =>'关于我们');
法语:
'联系-fr','关于' =>'关于-fr');
第二步:
转到 app/config/app.php
文件.
你应该找到一行:
'locale' =>'恩',
并将其更改为您的主要网站语言(在您的情况下为波兰语):
'locale' =>'pl',
您还需要将以下几行放入此文件:
/*** 替代语言列表(不包括指定为语言环境"的语言)*/'alt_langs' =>数组('en','fr'),/*** 选定语言环境的前缀 - 留空(在运行时设置)*/'locale_prefix' =>'',
在 alt_langs
配置中,您设置替代语言(在您的情况下为 en
和 fr
) - 它们应该与最初的文件名相同创建带有翻译的文件的步骤.
并且 locale_prefix
是您的语言环境的前缀.您不需要默认语言环境的前缀,因此将其设置为空字符串.如果选择默认语言以外的其他语言,将在运行时修改此配置.
第三步
转到您的 app/routes.php
文件并放置它们的内容(这是 app/routes.php
文件的全部内容):
$v) {路线::模式($k, $v);}Route::group(array('prefix' => Config::get('app.locale_prefix')), function(){路线::获取('/',功能 () {返回主页-".App::getLocale();});路线::获取('/{接触}/',功能 () {返回联系页面".App::getLocale();});路线::获取('/{关于}/',功能 () {返回关于页面".App::getLocale();});});
正如您首先看到的,您检查 url 的第一段是否与您的语言名称匹配 - 如果是,则更改区域设置和当前语言前缀.
然后在小循环中,您为所有路由名称设置要求(您提到要在 URL 中翻译 about
和 contact
),因此在这里将它们设置为与当前语言的 routes.php
文件中定义的相同.
最后,您创建的路由组的前缀将与您的语言相同(对于默认语言,它将为空)并且在组内您只需创建路径,但这些参数 about
和 contact
你把它当作 variables
所以你对它们使用 {about}
和 {contact}
语法.
您需要记住,在这种情况下,将检查所有路由中的 {contact}
是否与您在第一步中为当前语言定义的相同.如果您不想要这种效果并且想要使用 where 为每条路线手动设置路线,则有替代的 app
outes.php
文件,没有循环,您可以在其中设置 contact
和about
分别为每条路线:
Config::get('app.locale_prefix')), function(){路线::获取('/',功能 () {返回主页-".App::getLocale();});路线::获取('/{接触}/',功能 () {返回联系页面".App::getLocale();})-> where('contact', Lang::get('routes.contact'));路线::获取('/{关于}/',功能 () {返回关于页面".App::getLocale();})-> where('about', Lang::get('routes.about'));});
第四步:
您尚未提及此事,但您可以考虑另外一件事.如果有人将使用 url /en/something
其中 something
不是正确的路由,我认为进行重定向的最佳解决方案.但是你不应该重定向到 /
因为它是默认语言而是到 /en
.
所以现在您可以打开 app/start/global.php
文件并在此处为未知 url 创建 301 重定向:
//app/start/global.php应用程序::缺少(功能(){return Redirect::to(Config::get('app.locale_prefix'),301);});
I would like to create application with many translated routes depending on selected language. I've once described it at 3 methods of creating URLs in multilingual websites.
In this case it should be the first method from mentioned topic so:
- I have one default language
- I can have many other languages
- Current language should be calculated only by URL (without cookies/sessions) to make it really friendly also for search engines
- For default language there should be no prefix in URL, for other languages should be language prefix after domain
- Each part of url should be translated according to the current language.
Let's assume I have set default language pl
and 2 other languages en
and fr
. I have only 3 pages - mainpage, contact page and about page.
Urls for site should look then this way:
/
/[about]
/[contact]
/en
/en/[about]
/en/[contact]
/fr
/fr/[about]
/fr/[contact]
whereas [about]
and [contact]
should be translated according to selected language, for example in English it should be left contact
but for Polish it should be kontakt
and so on.
How can it be done as simple as possible?
First step:
Go to app/lang
directory and create here translations for your routes for each language. You need to create 3 routes.php
files - each in separate language directory (pl/en/fr) because you want to use 3 languages
For Polish:
<?php
// app/lang/pl/routes.php
return array(
'contact' => 'kontakt',
'about' => 'o-nas'
);
For English:
<?php
// app/lang/en/routes.php
return array(
'contact' => 'contact',
'about' => 'about-us'
);
For French:
<?php
// app/lang/fr/routes.php
return array(
'contact' => 'contact-fr',
'about' => 'about-fr'
);
Second step:
Go to app/config/app.php
file.
You should find line:
'locale' => 'en',
and change it into language that should be your primary site language (in your case Polish):
'locale' => 'pl',
You also need to put into this file the following lines:
/**
* List of alternative languages (not including the one specified as 'locale')
*/
'alt_langs' => array ('en', 'fr'),
/**
* Prefix of selected locale - leave empty (set in runtime)
*/
'locale_prefix' => '',
In alt_langs
config you set alternative languages (in your case en
and fr
) - they should be the same as file names from first step where you created files with translations.
And locale_prefix
is the prefix for your locale. You wanted no prefix for your default locale so it's set to empty string. This config will be modified in runtime if other language than default will be selected.
Third step
Go to your app/routes.php
file and put their content (that's the whole content of app/routes.php
file):
<?php
// app/routes.php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the Closure to execute when that URI is requested.
|
*/
/*
* Set up locale and locale_prefix if other language is selected
*/
if (in_array(Request::segment(1), Config::get('app.alt_langs'))) {
App::setLocale(Request::segment(1));
Config::set('app.locale_prefix', Request::segment(1));
}
/*
* Set up route patterns - patterns will have to be the same as in translated route for current language
*/
foreach(Lang::get('routes') as $k => $v) {
Route::pattern($k, $v);
}
Route::group(array('prefix' => Config::get('app.locale_prefix')), function()
{
Route::get(
'/',
function () {
return "main page - ".App::getLocale();
}
);
Route::get(
'/{contact}/',
function () {
return "contact page ".App::getLocale();
}
);
Route::get(
'/{about}/',
function () {
return "about page ".App::getLocale();
}
);
});
As you see first you check if the first segment of url matches name of your languages - if yes, you change locale and current language prefix.
Then in tiny loop, you set requirements for your all route names (you mentioned that you want have about
and contact
translated in URL) so here you set them as the same as defined in routes.php
file for current language.
At last you create Route group that will have prefix as the same as your language (for default language it will be empty) and inside group you simply create paths but those parameters about
and contact
you treat as variables
so you use {about}
and {contact}
syntax for them.
You need to remember that in that case {contact}
in all routes will be checked if it's the same as you defined it in first step for current language. If you don't want this effect and want to set up routes manually for each route using where, there's alternative app
outes.php
file without loop where you set contact
and about
separately for each route:
<?php
// app/routes.php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the Closure to execute when that URI is requested.
|
*/
/*
* Set up locale and locale_prefix if other language is selected
*/
if (in_array(Request::segment(1), Config::get('app.alt_langs'))) {
App::setLocale(Request::segment(1));
Config::set('app.locale_prefix', Request::segment(1));
}
Route::group(array('prefix' => Config::get('app.locale_prefix')), function()
{
Route::get(
'/',
function () {
return "main page - ".App::getLocale();
}
);
Route::get(
'/{contact}/',
function () {
return "contact page ".App::getLocale();
}
)->where('contact', Lang::get('routes.contact'));
Route::get(
'/{about}/',
function () {
return "about page ".App::getLocale();
}
)->where('about', Lang::get('routes.about'));
});
Fourth step:
You haven't mentioned about it, but there's one extra thing you could consider. If someone will use url /en/something
where something
isn't correct Route, I think the best solution to make redirection. But you should make redirection not to /
because it's default language but to /en
.
So now you can open app/start/global.php
file and create here 301 redirection for unknown urls:
// app/start/global.php
App::missing(function()
{
return Redirect::to(Config::get('app.locale_prefix'),301);
});
这篇关于如何在 Laravel 中创建多语言翻译路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!