Create folders inside resources/lang for different different languages and write message files with same name and also write same key among all the message files.
like I have 2 languages EN and ES.. then I created 2 folders, one is en and second is es.
Inside both folders I created password files. like:
en/passwords.php
1
2
3
4
5
6
|
<?php
return [
'type' =>'en'
];
|
es/passwords.php
1
2
3
4
5
6
|
<?php
return [
'type' =>'es'
];
|
Now I can call any of these 2 language messages by their file and key name like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class UserController extends Controller {
public function __construct() {
\App::setLocale('es');
// \App::setLocale('en');
}
public function index() {
$message = trans('passwords.type');
return view('welcome',['message'=>$message]);
}
}
|
here I can pass the language code dynamically in the constructor and message of that language will be called.
welcome.blade.php
1
2
3
|
@if(!empty($message))
{{$message}}
@endif
|
For changing message language all over the project, We can use session an can set the language or we also can create a middleware for this.
We also can get the current language of the project messages like:
1
|
$locale = App::getLocale();
|
We also can perform some operations conditionally like:
1
2
3
|
if (App::isLocale('en')) {
//
}
|