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. I did a print_r($return_lang)
in my view, return_lang
being $data['return_lang'] = $this->session->all_userdata();
and the result is:
Array ( [__ci_last_regenerate] => 1458691563 )
It looks like the site_lang
is never present there. 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) :
RewriteEngine On RewriteRule ^(.*)$ index.php/$1 [PT,L]
RewriteCond $1 !^(index.php)
This is my 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
:
function switchLang($language = "") {
Why do we set language to ""
? I tried to remove = ""
and it didn't help anyway...
UPDATE 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.
I then changed to:
$config['base_url'] = 'http://localhost/mySite/';
but it only works when using localhost in the URL.
Using $config['base_url'] = 'http://127.0.0.1/mySite/';
makes it work fine only when using the ip address in the URL bar.
I would like to get it to work for anything (localhost and ip address). When I say it doesn't work as expected, the [site_lang]
session is not populated with english/french/german.
I'm using LangSwitcher.php
as well in my CI and this is my working code:
your_controller.php
function __construct(){
parent::__construct();
$lang = $this->session->userdata('site_lang');
$this->lang->load("message",$lang);
}
LanguageSwitch.php (controller)
<?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
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'); ?>
And to switch then lang you should go to: <?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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.