Codeigniter中的语言 [英] Language in Codeigniter

查看:83
本文介绍了Codeigniter中的语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CodeIgniter网站致力于如何添加链接以具有这样的htpp://example/en/news/15http://example/ru/news/15

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

推荐答案

添加多语言的完整课程

Complete Lesson to Adding Multi-Language

按照此示例

application/config

In application/config

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

然后在application/language 目录中的,其中每种语言都有s 单独的目录

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

然后在控制器中

<?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);
    }
}

lang->load()方法的第一个参数将是不带后缀_lang的语言文件名.第二个参数(可选)是语言目录.如果此处未提供,它将指向您配置中的默认语言.

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.

我们可以使用lang-line()方法直接引用语言文件的条目,并将其返回值分配给传递给视图模板的数据.在视图内部,我们可以将上述语言消息用作$language_msg.

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;

然后在hooks.php中

<?php
$hook['post_controller_constructor'] = array(
    'class' => 'LanguageLoader',
    'function' => 'initialize',
    'filename' => 'LanguageLoader.php',
    'filepath' => '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());
    }
}

然后,我们需要定义链接以切换每种可用语言.

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

每当用户选择一种特定的语言时,LangSwitch类的switchLanguage()方法都会将选定的语言分配给会话,并将用户重定向到首页

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

有用的链接

  1. codeigniter.com语言类Ver< 3.0
  2. codeigniter.com语言课程3.0版+
  3. codeigniter -sitepoint中的多语言支持

这篇关于Codeigniter中的语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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