在运行时更改时,表单验证忽略语言 [英] Form validation ignores language when changed during run-time

查看:126
本文介绍了在运行时更改时,表单验证忽略语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用CodeIgniter来构建多语言Web应用程序。我在 / system / languages / 文件夹下有英语和其他语言,我创建了一个负责在运行时更改工作语言的模型。



默认情况下CodeIgniter按照 /application/config/config.php

的定义使用法语工作

$ config ['language'] ='french';



URI段模型改变语言相应,简化示例如下:

 类multilang extends CI_Model {
public function __construct ){
parent :: __ construct();
if($ this-> uri-> segment(1)=='en'){
$ this-> config-> set_item('language','english');
}
}
}

列在 /application/config/autoload.php 中的自动加载设置下,并且可以通过调用以确认语言确实动态更改:



echo $ this-> config-> item('language');



但是,内置的表单验证库不会考虑更改的语言,而只显示设置文件中硬编码的错误消息 /application/config/config.php 在这种情况下是法语。



起初我认为这是因为表单验证是在 multilang 模型。为了确保首先加载模型,我修改了表单验证构造函数以加载模型,然后再加载此类:

  public function __construct($ rules = array())
{
$ this-> CI =& get_instance();
$ this-> CI-> load-> model('multilang');
//正常代码之后....
}

该模型在表单验证之前加载。不幸的是这还不够,并且表单验证仍然忽略在运行时更改时的语言。任何人都知道为什么会发生这种情况。



谢谢。

解决方案

问题是,我做的AJAX请求没有考虑到包含语言缩写的URI段,因为AJAX请求的URI不需要语言段,所以我完全忘记了。 / p>

因此,我使用会话cookie来存储语言。将multilang构造函数更改为:

 类multilang extends CI_Model {
public function __construct(){
parent ::__构造();

#在会话之间存储lang
$ data = $ this-> session-> all_userdata();

if(isset($ data ['language'])){
$ lang = $ data ['language'];
#如果lang在会话之间改变
if($ this-> uri-> segment(1)=='fr'){
$ lang ='french'
} else if($ this-> uri-> segment(1)=='en'){
$ lang ='english';
}

#如果使用lang缩写之一更改lang
#overule会话设置
if($ this-> uri-> segment )=='en'){
$ lang ='english';
} else if($ this-> uri-> segment(1)=='fr'){
$ lang ='french'
}

$ this-> config-> set_item('language',$ lang)
$ this-> session-> set_userdata('language',$ lang);
} else {
if($ this-> uri-> segment(1)=='en'){
$ this-> config-> set_item ', '英语');
$ this-> session-> set_userdata('language','english');
} else if($ this-> uri-> segment(1)=='fr'){
$ this-> config-> set_item('language','french' );
$ this-> session-> set_userdata('language','french');
}
}
}
}



:不需要改变form_validation构造函数。



回答提供给以后参考,并提醒人们我们错过的小事情。这是显而易见的权利!这可能会帮助下一个忘记的人。



结束问题。


I'm using CodeIgniter to build a multilanguage web application. I have English and other languages under /system/languages/ folder and I've created a model responsible for changing the working language at run-time.

By default CodeIgniter is working in French as defined in /application/config/config.php

$config['language'] = 'french';

Later, according to a URI segment the model changes the language accordingly, simplified example bellow:

class multilang extends CI_Model {
    public function __construct() {
        parent::__construct();
        if ($this->uri->segment(1) == 'en') {
            $this->config->set_item('language', 'english');
        }
    }
}

This model is the first model listed under the auto load settings in /application/config/autoload.php and I can confirm that the language is indeed changed dynamically by calling:

echo $this->config->item('language');

However the built in form validation library does not take into account the changed language, instead only shows error messages from the language hard coded in the settings file /application/config/config.php in this case French.

At first I assumed this was because the form validation was loaded before the multilang model. To make sure the model was loaded first, I modified the form validation constructor to load the model before anything else like this:

public function __construct($rules = array())
{
    $this->CI =& get_instance();
    $this->CI->load->model('multilang');
    // normal code after....
}

This made sure the model loaded before the form validation. Unfortunately this wasn't enough and the form validation still ignores the language when changed during run-time. Anyone knows why this happens?

Thank you.

解决方案

The problem was that I was doing AJAX requests that didn't took into account the URI segment that contained the language abbreviation, because the URI for AJAX requests didn't needed the language segment in the first place, so I totally forgot about it.

Therefore I used the session cookie to store the language. Changing the multilang constructor to:

class multilang extends CI_Model {
    public function __construct() {
        parent::__construct();

        # store lang between session
        $data = $this->session->all_userdata();

        if (isset($data['language'])) {
            $lang = $data['language'];
            # if lang was changed between sessions
            if ($this->uri->segment(1) == 'fr'){
                $lang = 'french';
            } else if ($this->uri->segment(1) == 'en'){
                $lang = 'english';
            }

            # if lang was changed using one of the lang abbreviations
            # overule session setting
            if ($this->uri->segment(1) == 'en') {
                $lang = 'english';
            } else if ($this->uri->segment(1) == 'fr') {
                $lang = 'french';
            }

            $this->config->set_item('language', $lang);
            $this->session->set_userdata('language', $lang);   
        } else {
            if ($this->uri->segment(1) == 'en') {
                $this->config->set_item('language', 'english');
                $this->session->set_userdata('language', 'english');
            } else if ($this->uri->segment(1) == 'fr') {
                $this->config->set_item('language', 'french');
                $this->session->set_userdata('language', 'french');
            }
        }
    }
}

Note: The change to the form_validation constructor wasn't required.

Answer provided for future reference, and to remind people of little things we miss. It was so obvious right! Well this might help the next one who forgets.

Closing question.

这篇关于在运行时更改时,表单验证忽略语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆