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.
How can I call Lang::get('group.key')
specifying the needed language ?
Thank you for your help !
Edit: existing code: (the lang items are 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('/');
}
}
Laravel 5 Translation Using the Double Underscore (__) Helper Function. The __ helper function can be used to retrieve lines of text from language files.
Laravel-lang is a collection of over 68 language translations for Laravel by developer Fred Delrieu (caouecs), including authentication, pagination, passwords, and validation rules. This package also includes JSON files for many languages.
There are 3 ways to achieve this:
App::setLocale('fr');
NB: This is not suitable for your current need as it will only take effect on next page load.
'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');
NB: You folder structure should look like this
/app /lang /en messages.php /fr messages.php
To get a language specific translation - different from the current locales without setting and unsetting the locales, just do
__('description_1', [], 'en')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With