0

My site is trilingual ,I have a problem now, Normally, the site is in Persian language and users first see the Persian site and can change the language of the site from the language change option. How can the language be changed according to the IP of the visitor? For example :Persian language users, by opening the site, view the site in Persian , Arabic-speaking users, by opening the site, see the site in Arabic .this is the method that used for change language.

controller:

 public function index($locale) //en | fa
{

    App::setLocale($locale);
    Session()->put('local', $locale);

    return redirect()->back();

}

app/config:

'timezone' => 'Asia/Tehran',



'locale' => 'fa',

'fallback_locale' => 'fa',

dropdown menu:

    <div class="dropdown show nav-link">
    @php $local =Session()->get('local'); @endphp
    <a class="btn dropdown-toggle " href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown"
       aria-haspopup="true" aria-expanded="false">
        <img src="{{asset('images/translate/translate.png')}}" width="40px" height="40px">

        @switch($local)
            @case('fa')
            فارسی
            @break
            @case('en')
            English
            @break
            @case('ar')
            العربی
            @break
        @endswitch

    </a>

    <div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
        <a class="dropdown-item text-right nav-link " href="{{route('language','fa')}}"><img
                    src="{{asset('images/translate/iran.png')}}"
                    width="30" height="30"
            >
            فارسی</a>
        <a class="dropdown-item text-right nav-link" href="{{route('language','en')}}"> <img
                    src="{{asset('images/translate/english.png')}}" width="30" height="30"
            >English</a>
        <a class="dropdown-item text-right nav-link" href="{{route('language','ar')}}"> <img
                    src="{{asset('images/translate/emarat.png')}}" width="30" height="30"
            >العربی</a>
    </div>
</div>

Rout

Route::get('lang/{locale}', 'LocalizationController@index')->name('language');

1 Answers1

1

salaam.

You can get the user IP from request() method that is available in every request (every route). For example in your index method:

 public function index($locale) //en | fa
{
    $userIp = request()->ip();
    App::setLocale($locale);
    Session()->put('local', $locale);

    return redirect()->back();

}
Rouhollah Mazarei
  • 3,969
  • 1
  • 14
  • 20