PHP多国语言开发:CodeIgniter 2PHP框架中的多国语言,语言包(i18n)库

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yw8886484/article/details/85076581

PHP多国语言开发:CodeIgniter 2PHP框架中的多国语言语言包(i18n)多国语言库

引言

我们在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'] = "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

将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/en/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.

页面链接将会添加上当前语言的目录,但是文件链接不会。可以参考:www.cnmeizhuang.com

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.

如何工作的

php多语言
PHP多国语言开发

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

选项:

特殊地址

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

你需要其他的特殊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。

以上就是CI框架 利用语言包(i18n)库,php多国语言实现的一些思路。

猜你喜欢

转载自blog.csdn.net/yw8886484/article/details/85076581