skip to Main Content

Im trying to figure out how i can change the flag of the selected language.

in my language file i have the translate of the language:

  'english-short' => 'gb',
  'arabic-short' => 'ar',
  'french-short' => 'fr',
  'russian-short' => 'ru',
  'spanish-short' => 'es',

and in the header where i have the language switch i have the code:

@if($locale == 'ar')
{{ trans('header.arabic-short') }}
@elseif($locale == 'ru')
{{ trans('header.russian-short') }}
@elseif($locale == 'fr')
{{ trans('header.french-short') }}                              
@elseif($locale == 'sp')
{{ trans('header.spanish-short') }}
@else                          
{{ trans('header.english-short') }}
@endif

Now i have this one to show : <span class="flag-icon-1 flag-icon-{{$locale}}"></span>

but how can i add the span into the if code.
Now i have dubble GB (English) on the page.

I have tryed this:

<span class="flag-icon-1 flag-icon-@if($locale == 'ar')
    {{ trans('header.arabic-short') }}
    @elseif($locale == 'ru')
    {{ trans('header.russian-short') }}
    @elseif($locale == 'fr')
    {{ trans('header.french-short') }}                              
    @elseif($locale == 'sp')
    {{ trans('header.spanish-short') }}
    @else                          
    {{ trans('header.english-short') }}
@endif
</span>

With no luck.

UPPDATE:
I have in commoncontroller.php

public function lang(){
         $locale = session()->get('locale');
        if($locale){
             return $locale;
           }else{
             return $locale = 'en';
        }
    }

And when i select the specific language i whant i have:

  <a href="{{url('lang/en')}}">
                <span class="flag-icon-1 flag-icon-en"></span>
                <span class="name">English</span>
            </a>

as I said, I see 2 languages now, i only want to see the one with the flags.enter image description here

And it´s the @if($locale == ‘ar’) that show the GB text.

2

Answers


  1. Are you passing in locale from controller or somewhere?
    I have worked with Laravel 8 and 9 mostly not sure if $locale is accessible that way.
    But, you can get the current locale using:

    app()->getLocale()
    

    Secondly, you need to have a middleware that’ll update the app locale when changed. Otherwise, whatever you do locale isn’t changing.

    Login or Signup to reply.
  2. You can use Conditional Classes for if you want:

    <span @class([
        'flag-icon-1',
        'flag-icon-' . trans('header.arabic-short') => $locale == 'ar',
        'flag-icon-' . trans('header.russian-short') => $locale == 'ru',
        'flag-icon-' . trans('header.french-short') => $locale == 'fr',
        'flag-icon-' . trans('header.spanish-short') => $locale == 'es',
        'flag-icon-' . trans('header.english-short') => $locale == 'gb',
    ])>
    </span>
    

    But I suggest you implement a more efficient way. (You can have it checked with an array or create a custom blade directive)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search