How to do multi language on Laravel 4.2


You might need to add multi language system on your application, and with laravel, multi language is something easy to do, that's one of the benefit of using this framework, in this tutorial i will show you how to create a multi language system on laravel 4.2.

In laravel 4.2, you can create the language translation inside app/lang/ folder, inside this folder you can create another folder for each language that your application support. Note that, what we are going to do is to create multi language for the application language not the content of the application itself.

If you want multi language for content of your application, let's say you are creating a news app, then the multi language for content should come from the database. But for, let's say error message, you can do it with laravel multi language support.

In laravel 4.2, by default, there is only one language supported, which is 'en' for english, in this short tutorial i will create support for japanese (jp) language as an example.

Let's create the language file (testing.php)

app/lang/en/testing.php

<?php

return array(    
   "greeting" => "good morning",

);

app/lang/jp/testing.php

<?php

return array(    
   "greeting" => "おはようございます",

);

Now let's try it directly on route, just to make it easy and for show purpose only

app/routes.php

Route::get('/example', function()
{

  $lang = Input::get('lang');
 
  if (!empty($lang)) {
  
     App::setLocale($lang);
 
  }
 
  $message = Lang::get('testing.greeting');
   
  return $message;

});

Now you can test by running the url /example?lang=en and then run /example?lang=jp, see if the message changed. This is just an example on how to create multi language on laravel 4.2, i hope you get the idea of multi language in laravel.


EmoticonEmoticon