problem in changing default language (localization) in laravel 7

1.2k Views Asked by At

iam trying to make multilanguage view in laravel i set up everything but when i change the localization in config/app.php from "en" to "ar" ... it stays "en" and i tested with the function App::getLocal

my files of translation "messages.php" for "en"

'''

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Authentication Language Lines
    |--------------------------------------------------------------------------
    |
    | The following language lines are used during authentication for various
    | messages that we need to display to the user. You are free to modify
    | these language lines according to your application's requirements.
    |
    */
"offer name required" => "the name field is required",
"offer name max" => "the name cannot exceed 100 characters",
"offer name unique" => "offer name is already used",
"offer photo required" => "photo field is required also"
];

''' for "ar"

'''

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Authentication Language Lines
    |--------------------------------------------------------------------------
    |
    | The following language lines are used during authentication for various
    | messages that we need to display to the user. You are free to modify
    | these language lines according to your application's requirements.
    |
    */
"offer name required" => "يجب ادخال اسم العرض",
"offer name max" => "يجب أن لا يتعدى الاسم 100 عنصرا",
"offer name unique" => "هذا الاسم غير متوفر",
"offer photo required" => "يجب ادخال رابط الصورة"
];

''' when i print a message for test using this code : '''

<?php

namespace App\Http\Controllers;
use App;
use Illuminate\Http\Request;

class locale extends Controller
{
    function getlocal(){
 
     echo __('messages.offer name required');
     echo(' <br> ');
   
}}

'''

it always prints in english eventhough iam putting it locale="ar" in app.php

i hope you can help me solve this .. and if you need any additional infos tell me .. thank you for your time

2

There are 2 best solutions below

1
On
  1. You can try clear cache with php artisan config:clear command.

  2. Probably a problem with a spaces. Try to change

    // "offer name required" => "يجب ادخال اسم العرض",
    'offer_name_required' => 'يجب ادخال اسم العرض',
    
    // echo __('messages.offer name required');
    echo __('messages.offer_name_required');
    
0
On

I would suggest to store the language in session and use a middleware to setLocale.

Working example

SetLocale.php middleware

 public function handle($request, Closure $next)
{
    if (Session::has('language'))
    {
        App::setLocale(strtolower(Session::get('language')));
    }
    return $next($request);
}

Controller function to set a language

 public function setLanguage(string $language)
{
    if (Session::has('language'))
    {
        Session::forget('language');
    }
    Session::put('language', $language);

    return redirect()->back();
}

So now on everything that happens on the website, the middleware will check the language and set it to what's in session.

Also don't forget to specify the middleware in $middlewareGroups in app\Http\Kernel.php

You can create a form anywhere on website for the user to choose his language preference with a route to the controller function.