如何在Laravel中创建多语言翻译的路线 [英] How to create multilingual translated routes in Laravel
问题描述
我想创建具有许多翻译路线的应用程序,具体取决于所选的语言.我曾经在在多语言网站中创建URL的3种方法中对此进行了描述.
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:
- 我有一种默认语言
- 我可以有许多其他语言
- 当前语言应仅通过URL计算(不包含cookie/会话),以使其对搜索引擎也非常友好
- 对于默认语言,URL中不应包含前缀,对于其他语言,则应是域后的语言前缀
- 应根据当前语言翻译url的每个部分.
假设我设置了默认语言pl
和另外2种语言en
和fr
.我只有3页-主页,联系页和关于页.
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.
网站网址应如下所示:
/
/[about]
/[contact]
/en
/en/[about]
/en/[contact]
/fr
/fr/[about]
/fr/[contact]
[about]
和[contact]
应根据所选语言进行翻译,例如,英语应保留为contact
,而波兰语应为kontakt
,依此类推.
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.
如何做到尽可能简单?
推荐答案
第一步:
转到app/lang
目录,并在此处为每种语言的路线创建翻译.您需要创建3个routes.php
文件-每个文件都位于单独的语言目录(pl/en/fr)中,因为您要使用3种语言
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
对于波兰人:
<?php
// app/lang/pl/routes.php
return array(
'contact' => 'kontakt',
'about' => 'o-nas'
);
对于英语:
<?php
// app/lang/en/routes.php
return array(
'contact' => 'contact',
'about' => 'about-us'
);
对于法语:
<?php
// app/lang/fr/routes.php
return array(
'contact' => 'contact-fr',
'about' => 'about-fr'
);
第二步:
转到app/config/app.php
文件.
您应该找到以下行:
'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' => '',
在alt_langs
配置中,您设置了备用语言(在您的情况下为en
和fr
)-它们应该与创建带有翻译文件的第一步中的文件名相同.
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.
locale_prefix
是您的语言环境的前缀.您不需要默认语言环境的前缀,因此将其设置为空字符串.如果选择了默认语言以外的其他语言,则会在运行时修改此配置.
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.
第三步
转到您的app/routes.php
文件并放置其内容(这是app/routes.php
文件的全部内容):
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();
}
);
});
首先看到的是url的第一段是否与您的语言名称匹配-如果是,则更改语言环境和当前语言前缀.
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.
然后在微小循环中,为所有路由名称设置要求(您提到要在URL中转换about
和contact
),因此在此处将它们设置为与routes.php
文件中定义的相同当前语言.
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.
最后,您创建的路由组将具有与您的语言相同的前缀(对于默认语言,它将为空),而在组内部,您仅创建路径,但是将那些参数about
和contact
视为{about}
和{contact}
语法.
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.
您需要记住,在这种情况下,将检查所有路径中的{contact}
是否与您在第一步中为当前语言定义的相同.如果您不希望这种效果,并且希望使用where手动为每个路由设置路由,则可以使用另一个app\routes.php
文件(不带循环),在其中为每个路由分别设置contact
和about
:
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\routes.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'));
});
第四步:
您没有提到它,但是您还可以考虑另外一件事.如果有人使用something
不是正确的路由的URL /en/something
,我认为是进行重定向的最佳解决方案.但是,您不应该重定向到/
,因为它是默认语言,而是重定向到/en
.
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
.
因此,现在您可以打开app/start/global.php
文件并在此处为未知的url创建301重定向:
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屋!