用Laravel中的特定语言翻译

问题描述 投票:11回答:4

我在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('/');
    }
}
php laravel
4个回答
22
投票

有3种方法可以实现这一目标:

  1. 您可以通过执行以下操作在运行时更改默认语言:

App::setLocale('fr');注意:这不适合您当前的需要,因为它只会对下一页加载生效。

  1. 您可以在此处设置默认语言app / config / app.php

'fallback_locale' => 'fr'

  1. 我深入研究了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');

注意:你的文件夹结构应该是这样的

/app
    /lang
        /en
            messages.php
        /fr
            messages.php

2
投票

只需在调用Lang::get()之前设置所需的语言环境:

App::setLocale('es');

0
投票

我会推荐这样的东西:

    $savedLocale = App::getLocale();
    App::setLocale($this->getUserMailingLanguage());
    Mail::to($this->e_mail)->send($mailable);
    App::setLocale($savedLocale);

0
投票
<?php

return [
 'welcome' => 'welcome :name',
];

trans('welcome', [ 'name' => 'xyz' ], 'fr');
© www.soinside.com 2019 - 2024. All rights reserved.