3

I need to add another language to a laravel project, I did all the translation files and changed all the text to {{trans()}}. I just can't change the URIs to seLocale so I wrote this route

   Route::get('/changelanguage', function(){
      if(Session::has('locale'))
      {
        $lang = Session::get('locale');
          if ($lang == 'en') {
              App::setLocale('ar'); 
              session(['locale' => 'ar']);
             }
          elseif ($lang == 'ar') {
             App::setLocale('en'); 
              session(['locale' => 'en']);
             }
        }else{
        session(['locale' => 'en']);
       }
     return Redirect::back();

    });

and this doesn't make any changes to the local

Beshoy Tamry
  • 75
  • 1
  • 11
  • did you use trans() correctly `{{trans('file.messageKey')}}` ?? – Maraboc Nov 01 '16 at 13:46
  • yes, the translation works when I manually change App locale to "ar" – Beshoy Tamry Nov 01 '16 at 13:54
  • Try to use `put` like this `Session::put('locale', 'ar');` instead of `session(['locale' => 'ar']);` ! – Maraboc Nov 01 '16 at 14:13
  • I tried. I also tried to var_dump(App::getLocale()) and it shows an array of two strings en & ar – Beshoy Tamry Nov 01 '16 at 14:30
  • Take a look to this [exampe](https://laracasts.com/discuss/channels/general-discussion/where-to-setlocale-in-laravel-5-on-multilingual-multidomain-app?page=1) it creates a middleware to handel that hope that helps. – Maraboc Nov 01 '16 at 14:43
  • this example doesn't show how the middleware gets the user request and also the example he attached in github is different. the point is when I redirect to a certain uri or page it works and when I leave this page the app changes back to default locale @Maraboc – Beshoy Tamry Nov 01 '16 at 15:08
  • Yes, in the Github example it's a job hwo change local not the middleware :p, i think the best is to do like `omar.makled` did in the same [Link](https://laracasts.com/discuss/channels/general-discussion/where-to-setlocale-in-laravel-5-on-multilingual-multidomain-app?page=1) it's well documented ! – Maraboc Nov 01 '16 at 16:08
  • hi @BeshoyTamry, do have any tutorial that shows step by step how to setup a multi-language feature in Laravel? I am trying to follow the docs but I can't do nothing. – IgorAlves Mar 28 '17 at 18:25

0 Answers0