Codeigniter will not load language

2019-05-30 13:56发布

问题:

Hello i have problem with Codeigniter language class.

I follow this guide :

https://www.codeigniter.com/user_guide/libraries/language.html

I create test controller

I create test_lang.php inside app/language/english

I set english language in config file

My controller :

<?php

    class Test extends MX_Controller
    {
        public $data;

        function __construct() 
        {
            $this->load->helper('language');
            $this->load->lang('test');
        }

        function index() {

            $this->data['title'] = $this->lang->line("test");

            $this->load->view('test',  $this->data);
        }
    }
    ?>

Application/language/english/test_lang.php

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

$lang['test'] = 'This form post did not pass our security checks.';


?>

Views :

<?php

echo $title;
?>

And i have blanko page. No result, nothing is heppenging.

I try directly in views to put echo $this->lang->line("test"); and again nothing.

What i do wrong? Any1 can tell me how to fix this? Thanks

回答1:

You should load the language file by using $this->lang->load('test'); instead of $this->load->lang('test');.

Update:

There are some mistakes in your controller, first __constructor() must be changed to __construct(). Second, you should call the parent __construct() method after overriding:

public function __construct()
{
    parent::__construct();
}