在codeigniter中定位,在不同语言上的不同视图

时间:2013-05-21 10:48:22

标签: codeigniter

我正在使用this approach来本地化我的codeigniter网络应用。 现在一切正常,除了我想删除这个重复的代码,本地化我必须把每个控制器动作放在里面

public function history() 
    {            
        $selectedLang = $this->lang->lang();
        switch ($selectedLang)
        {
            case "en":
                $data['switchLang'] = "fr";  
                $langView = "company/historyEN";
                break;
            case "sr":
                $data['switchLang'] = "en";                                
                $langView = "company/history";
                break;
            default:
               $data['switchLang'] = "en";                       
                $langView = "company/history";
        }          

        $this->lang->load("content");
        $this->load->view($langView, $data);
    }

是否可以在CI_Controller类中移动它,这对所有控制器都可用?如果是这样的话?

或其他什么,请告诉我正确的方向

2 个答案:

答案 0 :(得分:1)

正确的方法是制作自己的核心控制器并将此功能放在该类中。你让所有的控制器都从那个类扩展出来。并且您将能够在控制器中使用此功能。

但我认为最好的方法是制作自己的模板库并自动加载,而不是使用CI视图使用自己的模板库。

像这样的东西

<?php

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

class Template {

    var $CI;

    public function __construct() {
        $this->CI = &get_instance();
    }

    function view($view = '', $data = array()) {

        $selectedLang = $this->CI->lang->lang();

        switch ($selectedLang) {
            case "en":
                $data['switchLang'] = "fr";
                $langView = $view . "EN";
                break;
            case "sr":
                $data['switchLang'] = "en";
                $langView = $view;
                break;
            default:
                $data['switchLang'] = "en";
                $langView = $view;
        }

        $this->CI->lang->load("content");

        return $this->CI->load->view($langView, $data);
    }

}

/* End of file template.php */
/* Location: ./system/application/libraries/template.php */

在控制器中,您只需使用此行来获取视图

$this->template->view('company/history', $data)

答案 1 :(得分:1)

或者你可以做的是把这个功能放在模型和自动加载模型中...所以它可以在你的所有控制器中使用。你只需要在所有控制器中调用只有一行的mehhod。 / p>

好的,你去......从config / autoload.php中创建一个模型并自动加载它 然后将您的此代码放入其中....说您的型号名称是语言

<?php
class Language extends CI_Model {
public function history() 
    {            
        $selectedLang = $this->lang->lang();
        switch ($selectedLang)
        {
            case "en":
                $data['switchLang'] = "fr";  
                $langView = "company/historyEN";
                break;
            case "sr":
                $data['switchLang'] = "en";                                
                $langView = "company/history";
                break;
            default:
               $data['switchLang'] = "en";                       
                $langView = "company/history";
        }          

        $this->lang->load("content");
        $this->load->view($langView, $data);
    }
}

<?php class Language extends CI_Model { public function history() { $selectedLang = $this->lang->lang(); switch ($selectedLang) { case "en": $data['switchLang'] = "fr"; $langView = "company/historyEN"; break; case "sr": $data['switchLang'] = "en"; $langView = "company/history"; break; default: $data['switchLang'] = "en"; $langView = "company/history"; } $this->lang->load("content"); $this->load->view($langView, $data); } } 现在来自你的每个控制器

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

class controllername extends CI_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->language->history();
    }