当前位置: 动力学知识库 > 问答 > 编程问答 >

Laravel - validation localization doesn't work

问题描述:

Whenever I use the trans() function to return a specific translation from the validation.php file, it works just perfectly. I have two languages in my application and the translations get returned for both of them.

However, whenever I use the Laravel validator, it returns messages in the default locale only. Is there something I need to specify in the validator? How do I make it work for both languages?

网友答案:

You need to pass as 3rd parameters your translations. Let's assume you have defined your fields, rules and validator like in the following code:

$data = Input::only('title');
$rules['title'] = 'required|min:20|max:80',
$validator = Validator::make($data, $rules,
            Lang::get('forms.validation.entry'));

Now you need to define your translations. Let's assume you need translation for fr lang so you need co create lang/fr/forms.php file and put the following content into it:

<?php

return

array (
  'validation' => array (
     'entry' => array (
         'title.required' => 'Your translation for title required',
         'title.min' => 'Your translation for title min',
         'title.max' => 'Your translation for title max',
      )
  )
);

Of course you can create file with simpler array but it's just example - instead of forms.validation.entry it could be for example just forms or validation.

网友答案:

The problem originated from my localization implementation. I've added App::setLocale(Session::get('lang')); to the App::before() method in the filters.php file and it all works now.

分享给朋友:
您可能感兴趣的文章:
随机阅读: