translate required fields
public $required = array('title', 'content', 'author_id');
and i get error like: Please fill out the required field 'author id'
how can i translate this fields?. I know that i can use
'author_id' => Please fill out the required field 'Avtor'
but i would like to add this in language file. Can i somehow translate just title to 'Naslov', content to ' Vsebina' and author_id to 'Avtor' that message will be like Please fill out the required field 'Naslov' or 'Vsebina' or 'Avtor',... and not translating whole text?
Comments
public $default_required_message = "Please fill out the required field '%1s'"
The second way is to use a key/value in the required array like so:
public $required = array('title' => 'Please fill out the title field.', 'content' => 'Please fill out the content field', 'author_id' => 'Please fill out the author field');
public $required = array('title' => lang('translate'));
becouse I get error. Can i somehow use lang() function here?
With first option i also don't know how to translate title, content,... to our language without using static Names. I want to use names in lang files.
You need to do it in the __contruct() not the class member definitions.
So at the top:
public $required = array();
__construct():
function __construct() { $this->required = array( 'title' => lang('string_1'), 'content' => lang('string_2'), 'author_id' => lang('string_3') ); parent::__construct('your_table_name'); }
Just tested that and it works for me.
// convert required fields to rules foreach($required as $key => $val) { if (is_int($key)) { $field = $val; $lang = lang('form_label_'.$val); if ($lang == '') { $lang = str_replace('_', ' ', $val); } //$msg = sprintf($this->default_required_message, str_replace('_', ' ', $lang)); $msg = sprintf($this->default_required_message, $lang); } else { $field = $key; $msg = $val; } $this->rules[] = array($field, 'required', $msg); }
so fields name are now selected from language files