CodeIgniter 2的国际语言包(i18n)库
引言我们在CodeIgniter开发中经常会碰到多国语言的站点,这里我们就来介绍一种简单有效的多国语言的操作方法。做什么语言在地址中是这样的:cit.cn/en/aboutcit.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.
如何工作的
http://www.cit.cn/uploads/allimg/120807/1-120PF92Q0R4.png
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.php1. 删除从$special数组删除“”;2. 设置$default_uri,例如home3. 如果你的默认语言是english的话,现在连接到/的请求,被重定向到en/home4. 默认语言是$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
mark mark mark mark@ 狂人CI群:118176067 great {:soso_e142:} 继续mark 要在CodeIgniter中创建多语言网站,最好使用CodeIgniter自己的语言库而不是i18n库。它包含语言类,如语言加载器和语言切换器。资源: https://www.cloudways.com/blog/multi-language-codeigniter/
页:
[1]