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'
);
英语:
<?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'
);
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' => '',
在alt_langs
config中,您可以设置其他语言(在您的示例en
和fr
中),它们应该与第一步创建带有翻译的文件时的文件名相同.
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
转到你的app/routes.php
文件并放入他们的内容(这是app/routes.php
文件的全部内容):
<?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.
最后,您创建的路由组的前缀将与您的语言相同(对于默认语言,它将是空的),在组内您只需创建路径,但这些参数about
和contact
被视为variables
,所以您使用{about}
和{contact}
语法.
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'));
});
Fourth step:
你没有提到它,但是还有一件事你可以考虑.如果有人使用url /en/something
,而something
不是正确的路由,我认为最好的解决方案是重定向.但是你不应该重定向到/
,因为它是默认语言,而是/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);
});