使用语言键在codeigniter的控制器中

时间:2012-10-17 07:06:24

标签: php codeigniter

我在Codeigniter框架中有一个应用程序构建。它具有多语言的功能。它工作正常为了转换每一行。但是对于从控制器设置的警报消息我尝试使用语言密钥但是未能以多语言在视图上显示消息。

以下是来自消息的控制器代码已设置为在视图上显示: -

控制器设置消息的功能:

function serial_coupon()
    {
     $admin_session_data= $this->session->userdata('user_logged_in');
     $key=$this->input->post('serial');
     $res=explode("000",$key);
     $key=$res[1];
     $result['coupon']=$this->provider_model->get_coupon($key);
          if(empty($result['coupon']))
           {
                       $msg=$this->lang->line('language_srch_msg');
               $this -> session -> set_flashdata('message',$msg);//if no search found than set the message.
               redirect('/provider/');//provider is controller
           }
           else
           {

               $this->load->view('header',$admin_session_data);
               $this->load->view('show_coupon',$result);
               $this->load->view('footer',$admin_session_data);
           }
    }

因此,提供者控制器的索引函数在视图上发送消息: -

function index()
        {
            $msg=$this->session->flashdata('message');//get the message 
            $result['msg']=$msg;and set to the array to send in view

                $result['rows']=$this->session->userdata('provider_detail');
        $user_id=$result['rows'][0]['id'];
            $result['coupons']=$this->provider_model->show_coupons($user_id);
            $this->load->view('header');
            $this->load->view('provider_interface',$result);
        $this->load->view('footer');

    }

所以消息应该在视图中显示:

<p><?php echo $msg; ?></p>

我使用语言键的其他行如下: 名称:

<?php echo $this->lang->line('language_name');?>

Now please Let me know how can i use the above language key for message in controller??

先谢谢你,但仍有任何疑问可以随意提问。

简短说明: 问题是不要显示falshdata message.i希望消息应该是用户选择的特定语言。多语言中的应用程序,用户可以从下拉框中选择语言。转换中的全部内容选择语言。但警报消息来自控制器,那么如何用选定的语言转换它们?

我在函数search_coupon中更改了我的代码,但它只适用于英语而不适用于葡萄牙语。这是我的提供者控制器的构造函数代码:

public function __construct()
        {
            parent::__construct();
            $this->load->helper('url');
            $this->load->helper('form');
            $this->load->library('session');
            $this->load->model('provider_model');

            $lng=$this->session->userdata('about_language');

                 if($lng=='' ) 
                 {

                $this->load->language('language','english');

                 }
                 else
                 {

                $this->load->language('language',$lng);

                 }



            if($this->session->userdata('provider')=="")
            {
              redirect('/login/', 'refresh');   
            }


        }

选择代码使用ajax的语言:

<script type="text/javascript">
         jQuery(document).ready(function() {
          jQuery('#port').click(function(){
                                    var dummy = 2;
                                    $.ajax({
                                            type: "POST",
                                            url: "<?php echo BASE_PATH; ?>/session/sessions",
                                        data: "&ID="+dummy+ "&lang="+'portuguese',
                                            success: function(response)
                                                {

                                                    if (response == false)
                                                        {   
                                                            location.reload();

                                                        }  
                                                }
                                           });

                                   });

            jQuery('#eng').click(function(){
                                        var dummy = 1;
                                        $.ajax({
                                                    type: "POST",
                                                    url: "<?php echo BASE_PATH; ?>/session/sessions",
                                                 data: "&ID="+dummy+ "&lang="+'english',
                                                    success: function(response)
                                                        {

                                                            if (response == false)
                                                                {
                                                                    location.reload();
                                                                }
                                                        }
                                                });

                                         });
         });
          </script> 

这是会话控制器:

function sessions(){

         $value=  $this->input->post('ID');
         $lang=  $this->input->post('lang');

         $lang=$this->session->set_userdata('about_language',$lang);
         return $lang;
        }

2 个答案:

答案 0 :(得分:1)

一种解决方案,也许不是最好的解决方案,是在flash var中存储消息密钥而不是消息。此时你正在存储:

$this -> session -> set_flashdata('message','No Search Found Of This Serial Number');//if no search found than set the message.

然后你正在加载消息:

$msg=$this->session->flashdata('message');//get the message 
$result['msg']=$msg;

并在视图中显示:

<p><?php echo $msg; ?></p>

我向你提出了这个解决方案:

$this -> session -> set_flashdata('message','message_key');//if no search found than set the message.

$msg_key = $this->session->flashdata('message');//get the message key
$result['msg_key'] = $msg_key;

在视图中:

<p><?php echo $this->lang->line('msg_key'); ?></p>

答案 1 :(得分:0)

好的,然后这样做:每次更改语言时,将其设置为会话。根据语言会话值加载语言文件。然后,您将把所有消息都换成您更改的语言。

$this->lang->load('filename', $this->session->userdata('user_lang'));