用户
 找回密码
 入住 CI 中国社区
搜索
查看: 5601|回复: 5
收起左侧

[版本 2.x] CodeIgniter 2的国际语言包(i18n)库

[复制链接]
发表于 2012-8-7 09:36:05 | 显示全部楼层 |阅读模式
引言

我们在CodeIgniter开发中经常会碰到多国语言的站点,这里我们就来介绍一种简单有效的多国语言的操作方法。

做什么

语言在地址中是这样的:

cit.cn/en/about

cit.cn/zh/about

保持使用库:Language Class


例子

视图中


<p><?=lang('about.gender')?></p>

英文语言文件


$lang['about.gender' = "I'm a man";

中文语言文件


$lang['about.gender' = "我是男人";

cit.cn/en/about显示的结果


<p>I'm a man</p>

cit.cn/zh/about显示的结果


<p>我是男人</p>
安装

下载ci_i18n_library.zip(http://www.cit.cn/uploads/allimg/120807/1-120PF91AR96.zip)

将MY_Lang.php 和 MY_Config.php 放到 application/core

配置

在 application/config/routes.php 增加


// example: '/en/about' -> use controller 'about'
$route['^fr/(.+)$' = "$1";
$route['^zh/(.+)$' = "$1";  
// '/en' and '/zh' -> use default controller
$route['^fr$' = $route['default_controller'];
$route['^zh$' = $route['default_controller'];

使用

让我们创建一个中英双语的页面

语言文件

application/language/english/about_lang.php



<?php  
$lang['about.gender' = "I'm a man";  
/* End of file */

application/language/chinese/about_lang.php



<?php  
$lang['about.gender' = "我是男人";  
/* End of file */
控制器

application/controllers/about.php



<?php
class About extends CI_Controller {

        function index()
        {
                // you might want to just autoload these two helpers
                $this->load->helper('language');
                $this->load->helper('url');

                // load language file
                $this->lang->load('about');


                $this->load->view('about');
        }
}

/* End of file */

视图

application/views/about.php



<p><?=lang('about.gender')?></p>
<p><?=anchor('music','Shania Twain')?></p>

测试

http://your_base_url/en/about



<p>I'm a man</p>
<p><a href="http://mywebsite.com/en/music">Shania Twain</a></p>

http://your_base_url/zh/about


<p>我是男人 </p>
<p><a href="http://mywebsite.com/fr/music">Shania Twain</a></p>

技巧贴示

你需要去翻译CodeIgniter里面system/language语言文件,例子:如果你需要使用“Form Validation”库,你就需要翻译:

system/language/form_validation_lang.php 到

application/language/chinese/form_validation_lang.php.


页面链接将会添加上当前语言的目录,但是文件链接不会


site_url('about/my_work');
// http://mywebsite.com/en/about/my_work   
site_url('css/styles.css');
// http://mywebsite.com/css/styles.css

获取当前语言


$this->lang->lang();
// en

切换到另一个语言

anchor($this->lang->switch_uri('zh'),'Display current page in chinese');


the root page (/) is supposed to be some kind of splash page, without any specific language. This can be changed: see “No splash page” below.


如何工作的


MY_Config.php保函一个重写的site_url():当生成语言地址目录的时候增加语言段,同样适用于anchor(), form_open()...


选项特殊地址

一个特殊地址不需要保函语言文件,默认的根目录地址(/)就是一特殊的URI.

你需要其他的特殊URIs,例如管理后台目录admin只需要一个语言文件。

在application/core/MY_Lang.php增加admin到数组$special中,现在链接到admin的链接就不会加入当前语言包路径了。

site_url('admin');

// http://mywebsite.com/admin


No splash page

在application/core/MY_Lang.php

1.        删除从$special数组删除“”;

2.        设置$default_uri,例如home

3.        如果你的默认语言是english的话,现在连接到/的请求,被重定向到en/home

4.        默认语言是$languages数组的第一个项目;


增加一个语言

1.        在application/core/MY_Lang.php文件中的$languages数组增加新的语言:


// example: German (de)
'de' => 'german',

2.        application/config/routes.php:增加新的路由


// example: German (de)
$route['^de/(.+)$' = "$1";
$route['^de$' = $route['default_controller'];

3.        在application/language目录中增加语言文件夹,这里的例子是“German”,需要命名为german。


CIT信息网翻译
文章地址:http://www.cit.cn/tech/develop/2012/0807/398.html


发表于 2012-8-7 10:34:33 | 显示全部楼层
mark mark mark
发表于 2012-8-7 14:36:37 | 显示全部楼层
mark@ 狂人CI群:118176067
发表于 2012-12-3 13:56:38 | 显示全部楼层
great {:soso_e142:}
发表于 2012-12-4 08:58:39 | 显示全部楼层
继续mark
发表于 2018-2-20 20:51:08 | 显示全部楼层
要在CodeIgniter中创建多语言网站,最好使用CodeIgniter自己的语言库而不是i18n库。它包含语言类,如语言加载器和语言切换器。资源: https://www.cloudways.com/blog/multi-language-codeigniter/

本版积分规则