DEV Community

Discussion on: Adding multi-language functionality in a website developed in laravel.

Collapse
 
fadilxcoder profile image
fadilxcoder

One way to do so is for example if you have a "posts" table with column : "id, title_en, title_fr, content_en, content_fr *" you can use if/else statement together with "app()->getLocale()*".

if(app()->getLocale() == 'en'):
    echo $var->title_en
else:
    echo $var->title_fr
endif;
Collapse
 
rezaodb profile image
Reza • Edited

Or:

@if (app()->isLocale('en'))
  //
@endif
Enter fullscreen mode Exit fullscreen mode

But best method is to set a custom If Statement!

1: Add in your AppServiceProvider:

use Illuminate\Support\ServiceProvider;

public function boot()
{   
    Blade::if('lang', function ($language) {
        return app()->isLocale($language);
    });
}
Enter fullscreen mode Exit fullscreen mode

2: In your views:

@lang('en')
//

@elselang('fr')
//

@else
//

@endlang