在 Laravel 中翻译特定语言
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29627878/
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
Translate in a specific language in Laravel
提问by MPikkle
I have a multilanguage website in Laravel 4.2, and would like to send an email notification to the admins in a specified language using the lang files.
我在 Laravel 4.2 中有一个多语言网站,我想使用 lang 文件以指定语言向管理员发送电子邮件通知。
How can I call Lang::get('group.key')
specifying the needed language ?
我如何调用Lang::get('group.key')
指定所需的语言?
Thank you for your help !
感谢您的帮助 !
Edit: existing code: (the lang items are option1, option2, .., option6)
编辑:现有代码:(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('/');
}
}
回答by Emeka Mbah
There are 3 ways to achieve this:
有3种方法可以实现这一点:
- You can change default language at runtime by doing this:
- 您可以通过执行以下操作在运行时更改默认语言:
App::setLocale('fr');
NB: This is not suitable for your current need as it will only take effect on next page load.
App::setLocale('fr');
注意:这不适合您当前的需求,因为它只会在下一页加载时生效。
- You can set default language here app/config/app.php
- 你可以在这里设置默认语言app/config/app.php
'fallback_locale' => 'fr'
'fallback_locale' => 'fr'
I took a deeper look at Illuminate\Translation\Translator:
get($key, array $replace = array(), $locale = null)
This means you can do this using Translator Facade:
Lang::get($key, array $replace = array(), $locale = null);
Example:
Lang::get('group.key',[],'fr');
我更深入地研究了 Illuminate\Translation\Translator:
get($key, array $replace = array(), $locale = null)
这意味着您可以使用 Translator Facade 执行此操作:
Lang::get($key, array $replace = array(), $locale = null);
例子:
Lang::get('group.key',[],'fr');
NB: You folder structure should look like this
注意:您的文件夹结构应如下所示
/app
/lang
/en
messages.php
/fr
messages.php
回答by alex toader
To get a language specific translation - different from the current locales without setting and unsetting the locales, just do
要获得特定于语言的翻译 - 与当前语言环境不同而无需设置和取消设置语言环境,只需执行
__('description_1', [], 'en')
回答by Limon Monte
Just set needed locale before calling Lang::get()
:
只需在调用之前设置所需的语言环境Lang::get()
:
App::setLocale('es');
回答by Alex
I would recommend something like this:
我会推荐这样的东西:
$savedLocale = App::getLocale();
App::setLocale($this->getUserMailingLanguage());
Mail::to($this->e_mail)->send($mailable);
App::setLocale($savedLocale);
回答by nrkz
<?php
return [
'welcome' => 'welcome :name',
];
trans('welcome', [ 'name' => 'xyz' ], 'fr');
回答by dns_nx
I had exactly the same issue and found the desired answer. This will provide you the desired result:
我遇到了完全相同的问题并找到了所需的答案。这将为您提供所需的结果:
trans('welcome',array(),null,'fr');
This is working since Laravel
vers. 5.2.
这是从Laravel
vers 开始工作的。5.2.