I have developed many sites using Laravel 3 (L3) and have now begun migrating to Laravel 4 (L4). I am by no means an expert but I would like to share what I have learned about localization that has helped me to more effectively implement sites that use several languages. I am going to assume that you are familiar with the Laravel's localization system.
The Laravel documentation explains that you create a PHP file in the app/lang/{langage tag} directory. For example, for the English language it may be app/lang/en and for Spanish it may be app/lang/es.
I typically try to match the language file name to the controller name. Therefore, if I have a controller called app/controller/AuthController then I will have a matching language file called auth.php in all the language directories. English would be app/lang/en/auth.php and Spanish would be app/lang/es/auth.php.
So, for this example, lets say we have a sign in page that implements the AuthController. We need to support multiple languages. BTW, we are using twitter bootstrap which explains the extra markups.
here is the app/view/auth/signin.blade.php file (very basic):
....
<legend>{{ Lang::get( 'auth.signin.page.heading' ) }}</legend>
<p>{{ Lang::get( 'auth.signin.page.summary' ) }}</p>
<div class="center">
<table>
<tr>
<th class="pull-right">{{ Lang::get( 'auth.signin.username.label }}</th>
<td class="pull-left">
{{ Form::text(
'username',
Input::old( 'username', '' ),
array(
'class' => '',
'data-content' => Lang::get( 'auth.signin.username.help' ),
'maxlength' => usermodel::$field_username_size,
'placeholder' => Lang::get( 'signin.username.placeholder' ),
'rel' => 'popover',
'required' => 'required' )
) }}
</td>
</tr>
<tr>
<th class="pull-right">{{ Lang::get( 'auth.signin.password.label }}</th>
<td class="pull-left">
{{ Form::password(
'password',
array(
'class' => '',
'data-content' => Lang::get( 'auth.signin.password.help' ),
'maxlength' => usermodel::$field_password_size,
'placeholder' => Lang::get( 'signin.password.placeholder' ),
'rel' => 'popover',
'required' => 'required' )
) }}
</td>
</tr>
</table>
</div>
....
Here is the language file app/lang/en/auth.php structure:
<?php return array( 'signin' => array( 'page' => array( 'heading' => 'Sign In', 'summary' => 'Please sign into this site using your credentials.', 'meta' => array( 'keywords' => 'signin login', ), // meta ), // page 'password' => array( 'help' => 'Please enter your password.', 'hint' => 'Enter your password', 'label' => 'Password', 'placeholder' => 'Password', 'validation' => array( 'required' => 'The password field is required.', 'minlen' => 'The password must be at least eight characters.', ), // validation ), // password 'username' => array( 'help' => 'Please enter your user name.', 'hint' => 'Enter your user name', 'label' => 'User name', 'placeholder' => 'User Name', 'validation' => array( 'required' => 'The user name field is required.', 'minlen' => 'The user name must be at least eight characters.', ), // validation ), // username ), // signin ); ?>
In our app/controller/AuthController.php signin() method, we can then use the validation messages like so:
....
// Validation Messages
$mMessages = array(
'password.min' => Lang::get( 'auth.signin.password.validate.minlen' ),
'password.required' => Lang::get( 'auth.signin.password.validate.required' ),
'username.min' => Lang::get( 'auth.signin.password.validate.minlen' ),
'username.required' => Lang::get( 'auth.signin.username.validate.required' ),
);
// Validate the form data.
$mValidation = Validator::make( $mInput, $mRules, $mMessages );
if ( $mValidation -> fails() )
{
// Validation failed.
return Redirect::back()
-> withErrors( $mValidation )
-> withInput();
}
....
As you can see, the depth at which you can go is very nice and you can properly customize each languages labels and messages.
No comments:
Post a Comment