Creating localization of content in laravel

If our app is going to be used by people in different countries, or who speak different
languages, we’ll need to localize the content. Laravel provides an easy way to do this.

Getting ready

For this recipe, we just need a standard installation of Laravel.

How to do it…

For this recipe, follow these steps:
1. In the app/lang directory, add three new directories (if they aren’t already there) :
en, es, and de.
2. In the en directory, create a file named localized.php and add the following code
to it:
<?php
return array(
‘greeting’ => ‘Good morning :name’,
‘meetyou’ => ‘Nice to meet you!’,
‘goodbye’ => ‘Goodbye, see you tomorrow.’,
);

3. In the es directory, create a file named localized.php and add the following code
to it:
<?php
return array(
‘greeting’ => ‘Buenos días :name’,
‘meetyou’ => ‘Mucho gusto!’,
‘goodbye’ => ‘Adiós, hasta mañana.’,
);
4. In the de directory, create a file named localized.php and add the following code
to it:
<?php
return array(
‘greeting’ => ‘Guten morgen :name’,
‘meetyou’ => ‘Es freut mich!’,
‘goodbye’ => ‘Tag. Bis bald.’,
);
5. In our routes.php file, create four routes as follows:
Route::get(‘choose’, function()
{
return View::make(‘language.choose’);
});
Route::post(‘choose’, function()
{
Session::put(‘lang’, Input::get(‘language’));
return Redirect::to(‘localized’);
});
Route::get(‘localized’, function()
{
$lang = Session::get(‘lang’, function() { return ‘en’;
});
App::setLocale($lang);
return View::make(‘language.localized’);
});
Route::get(‘localized-german’, function()
{
App::setLocale(‘de’);
return View::make(‘language.localized-german’);
});
6. In the views directory, create a folder named language.

7. In views/language, create the file choose.php and add the following code to it:
<h2>Choose a Language:</h2>
<?= Form::open() ?>
<?= Form::select(‘language’, array(‘en’ => ‘English’, ‘es’ =>
‘Spanish’)) ?>
<?= Form::submit() ?>
<?= Form::close() ?>
8. In the views/language directory, create a file named localized.php and add
the following code to it:
<h2>
<?= Lang::get(‘localized.greeting’, array(‘name’ =>
‘Lindsay Weir’)) ?>
</h2>
<p>
<?= Lang::get(‘localized.meetyou’) ?>
</p>
<p>
<?= Lang::get(‘localized.goodbye’) ?>
</p>
<p>
<?= HTML::link(‘localized-german’, ‘Page 2’) ?>
</p>
9. In the views/language directory, create a file named localized-german.php
and add the following code to it:
<h2>
<?= Lang::get(‘localized.greeting’, array(‘name’ =>
‘Lindsay Weir’)) ?>
</h2>
<p>
<?= Lang::get(‘localized.meetyou’) ?>
</p>
<p>
<?= Lang::get(‘localized.goodbye’) ?>
</p>
10. In the browser, go to http://{your-server}/choose (where your-server is
our URL), submit the form, and test the localization.

How it works…

For this recipe, we begin by setting up our language directories in the app/lang directory.
We’ll be using en for our English files, es for our Spanish files, and de for our German files.
Inside each directory, we create a file using the exact same name, and add in an array, using
the exact same keys.

Our first route is going to be a language selector page. On this page, we can choose either
English or Spanish. When we submit, it will POST to the route, create a new session, add the
choice, and redirect to the page to display the text in the chosen language.
Our localized route takes the session and passes the choice to App::setLocale(). We also
have a default value of English if there was no session set.
In our localized view, we print out the text using Lang::get(). In the first line of our
language file, we also included the :name placeholder, so we can pass in an array with the
placeholder name as the key when we call the language file.
Our last route shows how we can statically set the language default in our route.

Leave a Reply

Your email address will not be published. Required fields are marked *