[英]session with multilingual website - codeigniter
I followed this tutorial but when selecting another language in the dropdown, the text doesn't switch to another language. 我遵循了本教程,但是在下拉菜单中选择其他语言时,文本不会切换为另一种语言。 The redirect works however.
重定向有效。
I am using database session and the site_lang
value in the database is changed correctly. 我正在使用数据库会话,并且数据库中的
site_lang
值已正确更改。 I did a print_r($return_lang)
in my view, return_lang
being $data['return_lang'] = $this->session->all_userdata();
在我看来,我做了一个
print_r($return_lang)
, return_lang
是$data['return_lang'] = $this->session->all_userdata();
and the result is: 结果是:
Array ( [__ci_last_regenerate] => 1458691563 )
数组([__ci_last_regenerate] => 1458691563)
It looks like the site_lang
is never present there. 似乎
site_lang
从来没有出现过。 Only in the database. 仅在数据库中。 I can't figure out what's wrong.
我不知道怎么了。
I used the Hooks method of the tutorial, the 3 language files are created under the language folder and I entered the following in my htaccess (from another tutorial) : 我使用了本教程的Hooks方法,在language文件夹下创建了3个语言文件,并在htaccess中输入了以下内容(来自另一本教程):
RewriteEngine On RewriteRule ^(.*)$ index.php/$1 [PT,L]
RewriteRule上的RewriteEngine ^(。*)$ index.php / $ 1 [PT,L]
RewriteCond $1 !^(index.php)
RewriteCond $ 1!^(index.php)
This is my homeController: 这是我的homeController:
<?php
class homeController extends CI_Controller{
public function index(){
$data['main_content'] = 'home';
$data['return_lang'] = $this->session->all_userdata();
$this->load->view('templates/default',$data);
}
function initialize() {
$ci =& get_instance();
$ci->load->helper('language');
$siteLang = $ci->session->userdata('site_lang');
if ($siteLang) {
$ci->lang->load('message',$siteLang);
} else {
$ci->lang->load('message','english');
}
}
}
The other files are exactly the same as described in the tutorial. 其他文件与本教程中描述的文件完全相同。
There is one line I don't understand in LanguageSwitcher.php
: 在
LanguageSwitcher.php
有我不懂的一行:
function switchLang($language = "") {
函数switchLang($ language =“”){
Why do we set language to ""
? 为什么我们将语言设置为
""
? I tried to remove = ""
and it didn't help anyway... 我试图删除
= ""
,但仍然没有帮助...
UPDATE 1: 更新1:
I found one of the problem: 我发现了问题之一:
$config['base_url']
was set to ' '
and I get the expected behavior only when using 127.0.0.1 instead of localhost
in the URL. $config['base_url']
设置为' '
, 只有在URL中使用127.0.0.1而不是localhost
时,我才得到预期的行为。
I then changed to: 然后,我更改为:
$config['base_url'] = 'http://localhost/mySite/';
but it only works when using localhost in the URL. 但是它仅在URL中使用localhost时有效。
Using $config['base_url'] = 'http://127.0.0.1/mySite/';
使用
$config['base_url'] = 'http://127.0.0.1/mySite/';
makes it work fine only when using the ip address in the URL bar. 使它仅在使用URL栏中的IP地址时才能正常工作。
I would like to get it to work for anything (localhost and ip address). 我想让它适用于任何东西(本地主机和IP地址)。 When I say it doesn't work as expected, the
[site_lang]
session is not populated with english/french/german. 当我说它不能按预期工作时,
[site_lang]
会话中未填充english / french / german。
I'm using LangSwitcher.php
as well in my CI and this is my working code: 我
LangSwitcher.php
CI中使用LangSwitcher.php
,这是我的工作代码:
your_controller.php your_controller.php
function __construct(){
parent::__construct();
$lang = $this->session->userdata('site_lang');
$this->lang->load("message",$lang);
}
LanguageSwitch.php (controller) LanguageSwitch.php(控制器)
<?php
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);
$url = (null !== $this->session->userdata('ciutat_name')) ? $this->session->userdata('ciutat_name') : "tria_ciutat";
redirect(base_url($url));
}
}
Then, you must have different folders for each lang inside language folder like: english , french with all files called: message_lang.php
然后,对于每个lang语言文件夹,您必须具有不同的文件夹,例如:english,French,所有文件名为:
message_lang.php
with the translations this way: 用这种方式翻译:
$lang["text"] = "Text in english";
In the view you use this every time you need to use a translation: <?php echo lang('text'); ?>
在视图中,每次需要使用翻译时都使用此方法:
<?php echo lang('text'); ?>
<?php echo lang('text'); ?>
And to switch then lang you should go to: <?php echo base_url('langswitch/switchLanguage/english'); ?>
而要切换到lang,您应该转到:
<?php echo base_url('langswitch/switchLanguage/english'); ?>
<?php echo base_url('langswitch/switchLanguage/english'); ?>
The structure is: 结构为:
/controllers:
- your_controllers.php //here in the __construct set the lang
- langswitch.php //the langswitcher
/languages
/english: message_lang.php //here the translations
/french: message_lang
/views
your_views.php //here you use lang()
Hope it helps to you or put some 'light' on your question. 希望它对您有所帮助或对您的问题有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.