我在Laravel 4.2中有一个多语言网站,并希望使用lang文件以指定语言向管理员发送电子邮件通知。
如何调用Lang::get('group.key')
指定所需的语言?
谢谢您的帮助 !
编辑:现有代码:( lang项为option1,option2,..,option6)
class EmailController extends BaseController {
public static function contact(){
$rules = [
'name' => 'required',
'email' => 'required|email',
'subject' => 'required|digits_between:1,6',
'message' => 'required'
];
$validator = Validator::make(Input::all(), $rules);
if (!$validator->fails()){
$data = ['subject' => Input::get('subject'),
'email' => Input::get('email'),
'content' => Input::get('message')];
Mail::send('emails.contact', $data, function($message){
$message->from(Input::get('email'), Input::get('name'));
$message->to('[email protected]', 'Admin');
$message->subject(Lang::get('contact.option'.Input::get('subject')));
});
}
return Redirect::to('/');
}
}
有3种方法可以实现这一目标:
App::setLocale('fr');
注意:这不适合您当前的需要,因为它只会对下一页加载生效。
'fallback_locale' => 'fr'
get($key, array $replace = array(), $locale = null)
这意味着您可以使用Translator Facade执行此操作:
Lang::get($key, array $replace = array(), $locale = null);
例:
Lang::get('group.key',[],'fr');
注意:你的文件夹结构应该是这样的
/app
/lang
/en
messages.php
/fr
messages.php
只需在调用Lang::get()
之前设置所需的语言环境:
App::setLocale('es');
我会推荐这样的东西:
$savedLocale = App::getLocale();
App::setLocale($this->getUserMailingLanguage());
Mail::to($this->e_mail)->send($mailable);
App::setLocale($savedLocale);
<?php
return [
'welcome' => 'welcome :name',
];
trans('welcome', [ 'name' => 'xyz' ], 'fr');