2

I'm building a website in Laravel, I'm having it get the browser language and set the website's language accordingly.

Problem is, nothing in Registrar.php seems to get translated in English or Dutch

Could anyone please help me out? Thanks, g3

JordyvD
  • 1,565
  • 1
  • 18
  • 45
  • can you show the code of what you have done so far? You need to create separate folders for all languages you want to support and translate accordingly. You can start by coping the contents of resources/lang/en to resources/lang/nl-be, then translate the content of nl-be to dutch – Emeka Mbah Jun 18 '15 at 09:33
  • `Registrar.php`, `English` and `Dutch` are links :) – JordyvD Jun 18 '15 at 09:34
  • how? don't understand, what version of Laravel are you using – Emeka Mbah Jun 18 '15 at 09:35
  • Laravel 5, I mean in the question these are links – JordyvD Jun 18 '15 at 09:36
  • Okay, let me get you right, you wish to change user local when they click dutch link or english link? – Emeka Mbah Jun 18 '15 at 09:38
  • 1
    I just saw your code in pastebin, my advice is you should always include your code in your answer to make it clearer, pastebin is good but the link might get broken later and not everyone what to visit pastebin. How are you setting your locale in laravel. If you set Locale properly the correct lang file will be used. I answered similar question here: http://stackoverflow.com/questions/29723344/how-can-i-make-the-user-switch-languages-in-laravel-5/29726072#29726072 – Emeka Mbah Jun 18 '15 at 09:42
  • No, the words I mentioned are in the original question, they link to pastebin with the file's contents. The locale is being set using the browser's configured language. The locale is set the way it should, everything else is being translated. – JordyvD Jun 18 '15 at 09:53
  • Thanks for the tip to include everything in the post itself :) Will do so next time :D @DigitLimit – JordyvD Jun 18 '15 at 09:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/80866/discussion-between-digitlimit-and-g3mini). – Emeka Mbah Jun 18 '15 at 09:58

1 Answers1

2

To make this work you need to use a middleware to set Locales:

Create a LocaleMiddleware.php inside app/http/middleware directory

//LocaleMiddleware.php

<?php namespace App\Http\Middleware;

use Closure;
use Illuminate\Session\Store as Session;
use Illuminate\Contracts\Auth\Guard as Auth;


class LocaleMiddleware {

    public function __construct(Session $session)
    {
        $this->session      = $session;
    }


  //Languages available in your resources/lang

   protected $languages = ['en','es', 'nl-be'];



   public function handle($request, Closure $next)
   {
       $langlist = $_SERVER['HTTP_ACCEPT_LANGUAGE'];

       // We just want the main language
        $lang = substr($langlist,0,2);

        if(isset($this->languages[$lang])){
           app()->setLocale($lang);
        }else{
           //You may log this here
        }

        return $next($request);
     }

 }

Then register the middleware in app\httpe.kernel.php in $middleware array

/**
 * The application's global HTTP middleware stack.
 *
 * @var array
 */
protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,

     .........

    \App\Http\Middleware\LocaleMiddleware::class,
];

Creating lang files

You need to create separate folders for all languages you want to support and translate the files in them accordingly e.g nl-be\validation.php.

You can start by coping the contents of resources/lang/en to resources/lang/nl-be, then translate the contents ofnl-be\validation.php` to dutch equivalent

enter image description here

Emeka Mbah
  • 16,745
  • 10
  • 77
  • 96