php codeigniter 语言,php – Codeigniter中的语言

CodeIgniter网站的工作原理是如何添加链接以获得这样的htpp:// example / en / news / 15或http:// example / ru / news / 15

解决方法:

完成课程添加多语言

按照这个例子

在application / config中

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

然后在application / language目录中为每种语言分别创建一个目录

$lang["msg_first_name"] = "First Name";

$lang["msg_last_name"] = "Last Name";

$lang["msg_dob"] = "Date of Birth";

$lang["msg_address"] = "Address";

然后在控制器中

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

}

}

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.

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.

可以对视图内的这些文件使用与内部控制器相同的访问方法.

$this->lang->line("msg_hello_english");

我们还可以在语言助手的支持下使用以下代码在视图中加载语言条目,从而为我们提供更清晰的代码.

lang("msg_view_english");

分配语言加载对钩子的责任

在配置中

$config['enable_hooks'] = TRUE;

然后在hooks.php中

$hook['post_controller_constructor'] = array(

'class' => 'LanguageLoader',

'function' => 'initialize',

'filename' => 'LanguageLoader.php',

'filepath' => 'hooks'

);

然后在application / hooks中

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');

}

}

}

在不同语言之间切换

在控制器中

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());

}

}

Then we need to define links to switch each of the available languages.

English

French

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

有用的链接

标签:html,php,codeigniter

来源: https://codeday.me/bug/20191003/1850393.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值