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.
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