Crinsane
Crinsane

Reputation: 818

CodeIgniter form validation multilanguage labels

I'm currently working on a website that can be viewed in 3 different languages. I've put all text into languages files and almost everything is working as expected. Things like configuration for pagination I've put into a configuration file and into application/config, like this:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$config['num_links']      = 2;
$config['full_tag_open']  = '<p class="pagination">';
$config['full_tag_close'] = '</p>';
$config['first_link']     = '&laquo;&nbsp;' . lang('first');
$config['last_link']      = lang('last') . '&nbsp;&raquo;';

And it works great, but I've tried the same for my form validation configuration file, like this:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$config = array(

    'login' => array(
        array(
            'field' => 'login_email',
            'label' => lang('emailaddress'),
            'rules' => 'trim|required|valid_email|min_length[6]'
        ),
        array(
            'field' => 'login_password',
            'label' => lang('password'),
            'rules' => 'trim|required'
        ),
    ),

But this doesn't seem to work. It looks like this configuration file gets loaded before the language files/library. And to be honest, at the moment I don't really have an idea how to fix this other than taking everything out of the configuration file again, and put it into the controller, but I'd rather not do this.

Any ideas how to fix this?

Upvotes: 0

Views: 3149

Answers (2)

Plumpboy
Plumpboy

Reputation: 45

@Krishna Raj K i use a trans function like you. And i have fixed it. one more thing that i use wiredesignz hmvc. MY_Form_validation.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

    /** application/libraries/MY_Form_validation **/ 
    class MY_Form_validation extends CI_Form_validation 
    {
        public $CI;

        protected function _translate_fieldname($fieldname)
        {
            // Do we need to translate the field name? We look for the prefix 'trans:' to determine this
            if (sscanf($fieldname, 'trans:%s', $line) === 1 )
            {
                return trans($line);
            }

        return $fieldname;
    }
}

rules in model

'parent_id' => array(
                        'field'=>'parent_id',
                        'label'=>'trans:main.taxonomy.column.parent_id.name',
                        'rules'=>'is_natural_no_zero',

                    ), 

and in controller, you must load according to this order

//helper
$this->load->helper(array('array','form','anhtocon','trans'));
//library
$this->load->library(array('Nested_set','form_validation'));

$this->form_validation->CI =& $this;
//model
$this->load->model('taxonomy_model');

Upvotes: 0

emk
emk

Reputation: 51

if you check how field translation is done when defining form validation rules (see example below and consider the second argument):

$this->form_validation->set_rules('first_name', 'lang:first_name', 'required');

you can see where you are doing wrong in your actual code. instead of:

array(
    'field' => 'login_password',
    'label' => lang('password'),
    'rules' => 'trim|required'
),

the way to go is:

 array(
    'field' => 'login_password',
    'label' => 'lang:password',
    'rules' => 'trim|required'
),

Upvotes: 5

Related Questions