I am working on multilingualism using the mcamara
library
This is what I did: In the laravellocalization.php file I activated the Arabic and English languages
In the boostrab/app.php file, I added the middleware as shown in the library itself
->withMiddleware(function (Middleware $middleware) {
$middleware->alias([
/**** OTHER MIDDLEWARE ALIASES ****/
'localize' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRoutes::class,
'localizationRedirect' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter::class,
'localeSessionRedirect' => \Mcamara\LaravelLocalization\Middleware\LocaleSessionRedirect::class,
'localeCookieRedirect' => \Mcamara\LaravelLocalization\Middleware\LocaleCookieRedirect::class,
'localeViewPath' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationViewPath::class,
]);
})
In the routes/web.php file
Route::prefix(LaravelLocalization::setLocale())
->middleware([ 'localeSessionRedirect',
'localizationRedirect',
'localeViewPath',
'localeCookieRedirect'])
->group(function(){
Route::get('create_offer',[OfferController::class,'create']);
});
In the lang/ar/message.php file
'name_required' => 'الاسم مطلوب',
'next' => 'Next »',
In the lang/en/message/php file
'name_required' => 'The Name Required',
'next' => 'Next »',
In the controller file
class OfferController extends Controller
{
public function store(Request $request){
$rq = $request->validate([
'name'=>'required|unique:offers',
'price'=>'required',
'details'=>'required'
],
[
'name.required'=>__('message.name_required')
]);
$inserted = Offer::create(
$rq
);
return redirect()->back()->with(['message' => 'تم اضافة العرض بنجاح']);
}
public function create(){
return view('offers.create');
}
}
In the blade file
@foreach(LaravelLocalization::getSupportedLocales() as $localeCode => $properties)
<li class="nav-item">
<a class="nav-link" rel="alternate" hreflang="{{ $localeCode }}" href="{{ LaravelLocalization::getLocalizedURL($localeCode, null, [], true) }}">{{ $properties['native'] }}</a>
</li>
@endforeach
What I expect is that when I press the English language button, the language is converted to English, and when I press the Arabic language button, it is converted to Arabic.
Expected when pressing the english
button:
The word الاسم مطلوب
has been changed from Arabic to English
But this did not happen. What happens when you press the English button, the word remains the same in Arabic
I don't know how to solve this problem
I was able to solve the problem after many attempts, and I would like to share the solution with you so that everyone can benefit from it
What I did was omit the Mcamara laravel-localization
library
Then I did this thing inside the store
function:
public function store(Request $request , $lang=''){
if(in_array($lang,['en','ar'])){
App::setLocale($lang);
Session::put('lang',$lang);
}else{
App::setLocale(Session::get('lang'));
Session::put('lang',App::getLocale());
}
//.............
}
What was added inside the function store
is a variable named lang
whose value is sent when entering the route
of the function store
.
The variable values are checked and the value is added inside App::getLocale()
and also inside Session::put()
. If the value of the variable lang
is null, App::setLoale()
is made to take the value from Session
.
This is the Route
of the store
function:
Route::post('insert_offer/{lang?}',[OfferController::class,'store'])->name('store_offer');
The same thing was done inside the function create
public function create($lang=''){
if(in_array($lang,['en','ar'])){
App::setLocale($lang);
Session::put('lang',$lang);
}else{
App::setLocale(Session::get('lang'));
Session::put('lang',App::getLocale());
}
return view('offers.create',['language'=>['العربية'=>'ar','English'=>'en']]);
}
This is the Route
of the create
function:
Route::get('create_offer/{lang?}',[OfferController::class,'create']);
The value of the variable lang
is sent to the function store
via form
.
Like this:
<form method="post" action="{{ route('store_offer',App::getLocale()) }}">
I hope everyone benefits from this and bye
I want to add some modifications just for those who want to use the Request Validation
files:
Inside the roles
function we will place the same verification code as before, but with a slight difference:
public function rules(): array
{
$lang = request()->input('lang');
if(in_array($lang,['en','ar'])){
App::setLocale($lang);
Session::put('lang',$lang);
}else{
App::setLocale(Session::get('lang'));
Session::put('lang',App::getLocale());
}
//.............
Here the value of lang
is taken from the sent request
The value of lang
is sent with request
through input hidden
<input type="hidden" name="lang" value="{{ App::getLocale() }}">