开发者

i18n and Error messages in Kohana 3

I am developping an administration application with Kohana 3 and I'm obviously working with a lot of forms.

The application needs to be multilangual and I'm very confused about how to manage my messages files and especially how to access them.

Does i18n support different folders and files inside the language folder?

E.g:

  • i18n
    • en
      • form
    • fr
      • form

Or does it support arrays in the language file?

i18n/fr.php

<?php defined('SYSPATH') or die('No direct script access.');

return array
(
   'common_form' => array(
                     'error_type' => 'Error 开发者_StackOverflow社区message in French.',
                     'error_type_2' => 'Other error message.',
                    )


)

And if you can do that, how would you access these fields/files when you are translating a message?

Another thing I do not understand is how can I somehow link my translations to the error messages that live in the messages folder.

This is really confusing me. Also, how do you handle errors that do not have fields (errors that are not checked by Validate class)?

Thank you.


messages/validate.php:

return array
(
'upload::not_empty' => __('validate.upload_empty'),
);

i18n/en.php:

return array
(
'validate.upload_empty' => 'Upload must not be empty',
);

i18n/ba.php:

return array
(
'validate.upload_empty' => 'Upload ne moze biti prazan',
);

etc. ( you define as many rules as you want ).

At least this is my practice, first time using i18n and it works well.


Start only with message files written in the language the application is written in.

  • The developer can write the application using only message files and worry about translations later.
  • I18n files have the text to be translated on the left and the translations on the right in a single file.
  • I18n files can be parsed and edited by modules such as backends for translators.

messages/forms.php

<?php

return array(
    'user' => array(
        'label' => 'Email',
        'title' => 'Enter your email address.',
    ),
    'pass' => array(
        'label' => 'Password',
        'title' => '8 characters or more',
    ),
    'login' => array(
        'label' => 'Log On',
),
    'submit' => array(
        'label' => 'Submit',
    ),
);

After all the messages are set add the translations:

i18n/es.php

<?php

return array(
    'Email' => 'Email',
    'Password' => 'Contraseña',
    'Log On' => 'Acceder',
 );

When calling Kohana::message() wrap it in __() so that if a language is set it will attempt to use the translated message:

APPPATH/views/Auth/login.php

<label for="user_field"><?php echo __(Kohana::message('forms', 'user.label')); ?></label>
<input type="text" id="user_field" name="user" value="" title="<?php echo __(Kohana::message('forms', 'user.title'); ?>">
<br />
<label for="pass_field"><?php echo __(Kohana::message('forms', 'pass.label')); ?></label>
<input type="password" id="pass_field" name="user" value="" title="<?php echo __(Kohana::message('forms', 'pass.title'); ?>">

To set the language use I18n::lang():

I18n::lang('es');

Of course setting the language manually isn't very useful. It can be set using:

  • a dropdown box
  • browser language


There is not a good documentation on how to translate messages. The Message class looks for the complete string in the I18n class.

Here is an example of how to translate the system validations to german messages. Inside any /i18n/de.php file add:

':field must contain only letters' => ':field darf nur Buchstaben verwenden',
':field must contain only numbers, letters and dashes' => ':field darf nur Zahlen, Buchstaben und Schrägstriche verwenden',
':field must contain only letters and numbers' => ':field darf nur Zahlen und Buchstaben verwenden',
':field must be a color' => ':field muss eine Farbe sein',
':field must be a credit card number' => ':field muss eine Kreditkartennummer sein',
':field must be a date' => ':field muss ein Datum sein',
':field must be a decimal with :param2 places' => ':field muss eine Dezimalzahl mit :param2 Nachkommastellen',
':field must be a digit' => ':field muss eine Zahl sein',
':field must be an email address' => ':field muss eine Email-Adresse sein',
':field must contain a valid email domain' => ':field muss eine korrekte Email Domain beinhalten',
':field must equal :param2' => ':field muss :param2 sein',
':field must be exactly :param2 characters long' => ':field muss genau :param2 Zeichen lang sein',
':field must be one of the available options' => ':field muss eine der verfügbaren Optionen sein',
':field must be an ip address' => ':field muss eine IP-Addresse sein',
':field must be the same as :param3' => ':field muss das gleiche wie :param3 sein',
':field must be at least :param2 characters long' => ':field muss mindestens :param2 Zeichen lang sein',
':field must not exceed :param2 characters long' => ':field darf nicht länger als :param2 Zeichen lang sein',
':field must not be empty' => ':field darf nicht leer sein',
':field must be numeric' => ':field muss eine Zahl sein',
':field must be a phone number' => ':field muss eine Telefonnummer sein',
':field must be within the range of :param2 to :param3' => ':field muss zwischen :param2 und :param3 sein',
':field does not match the required format' => ':field passt nicht zum geforderten Format',
':field must be a url' => ':field muss eine URL sein',

This prevents you from using the __() function inside the message files because it is not allowed for caching.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜