Language in Codeigniter [closed]

2019-01-29 14:11发布

问题:

CodeIgniter site works on how to add links to have such htpp://example/en/news/15 or http://example/ru/news/15

回答1:

Complete Lesson to Adding Multi-Language

Follow this example

In application/config

$config['language'] = 'english'; //default Language

Then in application/language directory with a separate directory for each language

<?php
$lang["msg_first_name"] = "First Name";
$lang["msg_last_name"] = "Last Name";
$lang["msg_dob"] = "Date of Birth";
$lang["msg_address"] = "Address";

then in controller

<?php
class TestLanguage extends CI_Controller
{
    public function __construct() {
        parent::__construct();       
        $this->lang->load("message","english");
    }

    function index() {
        $data["language_msg"] = $this->lang->line("msg_hello_english");
        $this->load->view('language_view', $data);
    }
}

The first parameter to the lang->load() method will be the language’s filename without the _lang suffix. The second paramter, which is optional, is the language directory. It will point to default language in your config if it’s not provided here.

We can directly reference the entries of a language file using the lang-line() method and assign it’s return to the data passed into the view templates. Inside the view, we can then use the above language message as $language_msg.

It’s possible to use the same access method for these files inside views as inside controllers.

<?php
$this->lang->line("msg_hello_english");

We can also use the following code with the support of the language helper to load language entries inside views, which gives us cleaner code.

<?php
lang("msg_view_english");

Assigning Language Loading Responsibilities to Hooks

In Config

$config['enable_hooks'] = TRUE;

Then in hooks.php

<?php
$hook['post_controller_constructor'] = array(
    'class' => 'LanguageLoader',
    'function' => 'initialize',
    'filename' => 'LanguageLoader.php',
    'filepath' => 'hooks'
);

Then in application/hooks

<?php
class LanguageLoader
{
    function initialize() {
        $ci =& get_instance();
        $ci->load->helper('language');

        $site_lang = $ci->session->userdata('site_lang');
        if ($site_lang) {
            $ci->lang->load('message',$ci->session->userdata('site_lang'));
        } else {
            $ci->lang->load('message','english');
        }
    }
}

Switching Between Different Languages

In Controller

<?php
class LangSwitch extends CI_Controller
{
    public function __construct() {
        parent::__construct();
        $this->load->helper('url');
    }

    function switchLanguage($language = "") {
        $language = ($language != "") ? $language : "english";
        $this->session->set_userdata('site_lang', $language);
        redirect(base_url());
    }
}

Then we need to define links to switch each of the available languages.

<a href='<?php echo $base_url; ?>langswitch/switchLanguage/english'>English</a>
<a href='<?php echo $base_url; ?>langswitch/switchLanguage/french'>French</a>

Whenever the user chooses a specific language, the switchLanguage() method of the LangSwitch class will assign the selected languages to the session and redirect the user to the home page

Useful Links

  1. codeigniter.com Language Class Ver < 3.0
  2. codeigniter.com Language Class Ver 3.0 +
  3. multi language support in codeigniter -sitepoint