我最近在Laravel为德国客户开发和应用。现在,客户希望知道应用程序是否可以用德语而不是英语查看。我当然使用Blade in English设计了前端的所有视图。现在有一种方法可以将视图转换为所需的语言吗?是否有包装或其他方式来实现这一目标?
答案 0 :(得分:4)
Laravel提供http://laravel.com/docs/4.2/localization中的本地化功能,但您需要重新执行视图以使用lang字符串并向控制器添加语言检测。
我会避免“即时”翻译,因为它们很少可靠。
答案 1 :(得分:2)
我正在使用这个作曲家包:https://github.com/mcamara/laravel-localization
但是......它并不像Laravel 3那么简单。
您需要手动检查用户的语言并更改使用的语言。
首先,在config / app.php中设置默认语言环境:
'locale' => 'en',
然后,在同一个文件(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',
),
在路线文件中,您将验证路线组中的用户语言:
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');
}
);
我有一个名为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'));
}
}
上面代码中设置的选择器是在标题视图中调用的刀片文件:
<li class="dropdown">
{{ LocalizationHelper::getLanguageSelector() }}
</li>
然后,你必须在config / packages / mcamara / laravel-localization / config.php中设置配置文件。
只需将您使用的语言复制到未注释的数组中。
有一件事很糟糕......你必须在这样的链接中添加使用过的语言:
{{ Form::open(array('url' => LaravelLocalization::getCurrentLocale() . '/faviconit', 'files' => true, 'class' => 'form-horizontal')) }}
最后,您所要做的就是添加代码来翻译文本:
<span class="help-block">
{{ trans('faviconit.formFileHelp') }}</strong>
</span>
该语言位于lang文件夹中,带有国家/地区代码。
希望它可以帮助你:)