Laravel 应用程序语言更改?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27482803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel application language change?
提问by Rohan
I have recently developed and application in Laravel for a German client. Now the client wishes to know if the application can viewed in German instead of English. I have designed all the views of the front end using Blade in English of course. Now is there a way in which the views translate to the desired language? Is there a package or another way to accomplish this?
我最近在 Laravel 中为德国客户开发和应用。现在客户想知道应用程序是否可以用德语而不是英语查看。前端的所有视图我当然都是用英文的Blade设计的。现在有没有办法将视图转换为所需的语言?是否有软件包或其他方法可以实现此目的?
回答by Joe
Laravel provides localisation functionality baked in http://laravel.com/docs/4.2/localizationthough you are going to need to re-do your views to use the lang strings and add language detection to your controllers.
Laravel 提供了在http://laravel.com/docs/4.2/localization 中烘焙的本地化功能,尽管您需要重新创建视图以使用 lang 字符串并向控制器添加语言检测。
I would avoid "on-the-fly" translations as they are rarely reliable.
我会避免“即时”翻译,因为它们很少可靠。
回答by Eduardo Russo
I'm using this composer package: https://github.com/mcamara/laravel-localization
我正在使用这个作曲家包:https: //github.com/mcamara/laravel-localization
But… it's not as simple as it was in Laravel 3.
但是……它不像在 Laravel 3 中那么简单。
You need to manually check user's lang and change the used language.
您需要手动检查用户的语言并更改使用的语言。
First, set the default locale in config/app.php:
首先,在 config/app.php 中设置默认语言环境:
'locale' => 'en',
Then, create a new array of used languages in the same file (config/app.php):
然后,在同一个文件 (config/app.php) 中创建一个新的使用语言数组:
'languages' => array(
'en' => 'en_US',
'pt' => 'pt_BR',
'pl' => 'pl_PL',
'es' => 'es_LA',
'ru' => 'ru_RU',
'de' => 'de_DE',
'nl' => 'nl_NL',
'fi' => 'fi_FI',
'it' => 'it_IT',
'fr' => 'fr_FR',
),
In the route file, you'll verify the user language in a route group:
在路由文件中,您将验证路由组中的用户语言:
Route::group(
array('prefix' => LaravelLocalization::setLocale(), //Set the language using the package
'before' => 'LaravelLocalizationRedirectFilter', //Change the URL to match the language
),
function () {
Route::controller('upload', 'ImageUploadController');
Route::controller('faviconit', 'CreateController');
Route::controller('download/{folder?}', 'DownloadController');
Route::controller('/', 'HomeController');
}
);
And I have a helper class called LocalizationHelper.php with this code:
我有一个名为 LocalizationHelper.php 的辅助类,其中包含以下代码:
<?php
class LocalizationHelper
{
/**
* To use localization in an easier way, it's set as 'pt' instead of 'pt_BR'. To get the correct Brazilian
* localization in aviary, this function return 'pt_BR' when locale is set to 'pt'
*
* @return string The correct Aviary locale string
*/
public static function getUserLanguageFromSupportedLanguages()
{
$userLanguages = Request::getLanguages();
$supportedLanguages = Config::get('app.languages');
$userLanguage = Config::get('app.locale');
foreach ($userLanguages as $language) {
$language = substr($language, 0, 2);
if (false !== array_search($language, $supportedLanguages, true)) {
$userLanguage = $language;
break;
}
}
return $userLanguage;
}
/**
* Returns html with language selector. This code removes languages without URL.
* @return \Illuminate\View\View
*/
public static function getLanguageSelector()
{
//START - Delete in v1.0
$languages = LaravelLocalization::getSupportedLocales();
$active = LaravelLocalization::getCurrentLocale();
foreach ($languages as $localeCode => $language) {
$langUrl = LaravelLocalization::getLocalizedURL($localeCode);
// check if the url is set for the language
if ($langUrl) {
$language['url'] = $langUrl;
} else {
// the url is not set for the language (check lang/$lang/routes.php)
unset($languages[$localeCode]);
}
// fill the active language with it's data
if ($active == $localeCode)
$native = $language['native'];
}
return View::make('templates.languagebar', compact('languages', 'active', 'native'));
}
}
The selector setted on the code above is a blade file called in the header view:
上面代码中设置的选择器是在头部视图中调用的刀片文件:
<li class="dropdown">
{{ LocalizationHelper::getLanguageSelector() }}
</li>
Then, you have to set the config file in config/packages/mcamara/laravel-localization/config.php.
然后,您必须在 config/packages/mcamara/laravel-localization/config.php 中设置配置文件。
Just copy the languages you'll use to the uncommented array.
只需将您将使用的语言复制到未注释的数组中。
There's a thing that sucks… you have to add the used language in some links like this:
有一件事很糟糕……你必须在一些链接中添加使用的语言,如下所示:
{{ Form::open(array('url' => LaravelLocalization::getCurrentLocale() . '/faviconit', 'files' => true, 'class' => 'form-horizontal')) }}
Last, all you have to do is add the code to translate the texts:
最后,您要做的就是添加代码来翻译文本:
<span class="help-block">
{{ trans('faviconit.formFileHelp') }}</strong>
</span>
The language are located in the lang folder, with the country code.
语言位于 lang 文件夹中,带有国家/地区代码。
Hope it helps you :)
希望对你有帮助:)