首页 > PHP开源 > 多语言路由–kohana3使用手册

多语言路由–kohana3使用手册

2011年2月10日 发表评论 阅读评论

有许多的方法来用 Kohana 完成国际化的功能。一种方法就是在路由里指定语言。

例如

http://example.com/en-us/products // For English

http://example.com/fr-fr/products // For French

你可以在你的路由里捕捉到请求中的语言部分。下面的示例仅仅允许请求中的英语,法语和荷兰语,默认是英语。

Route::set(‘default’, ‘(<lang>/)(<controller>)(/<action>(/<id>))’, array(‘lang’ => ‘(en-us|fr-fr|nl-nl)’, ‘id’=>’.+’))

->defaults(array(

‘lang’ => ‘en-us’,

‘controller’ => ‘welcome’,

‘action’ => ‘index’,

));

那些现在可以在控制器中设置语言。下面的示例就在定制的模板控制器中设置了语言。

class Controller_Website extends Controller_Template

{

public function before()

{

// Set the language

I18n::$lang = Request::instance()->param(‘lang’);

}

}

在 application/messages 目录里创建语言文件

/home/kerkness/kohana/application/i18n/fr.php

return array

(

‘Hello World’ => ‘Bonjour Monde’,

);

在 application/view 目录下合适的目录里创建特殊语言视图

/home/kerkness/kohana/application/views/fr-fr/index.php

<p>Merci de visiter notre site Internet.</p>

<p>Voici mon contenu de homepage…

你所有的控制器都将使用适合的语言。

class Controller_Welcome extends Controller_Website
{
    public function action_index()
    {
        // Hello World字符串将被翻译
        $this->template->title = __('Hello World');

        //加载特定语言的视图
        $this->template->content = View::factory('page/'.I18n::$lang.'/index');
    }
}
分类: PHP开源 标签:
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.