我的网页上有三种不同的语言链接
我想要的是,当用户点击任何语言链接时,该页面上的文字会更改为相应的语言。
我是否有可能从我的控制器调用其中一个方法让我们说'changeLanguage'它会改变语言,然后用不同语言的文本重新加载同一页面
知道如何有效地做到这一点
我的链接在视图中,我正在使用codeigniter
由于
答案 0 :(得分:1)
您可以在活动点击刷新您的网站,获取[HTTP] www.mywebsite.com?lg=eng
答案 1 :(得分:1)
您可以通过多种方式执行此操作,但有效地归结为您的网站配置方式。最好的起点是使用PHP来检索$ _GET请求并根据请求显示内容。
您的链接将引用http://yoursite.com/index.php?lang=en ... 你可以转换为(使用htaccess或web.config)http://yoursite.com/en
类似的东西:
// Set the default language to english if the language request is not set
$language = isset($_GET['lang']) ? $_GET['lang'] : 'en';
// Show the content base on the language
switch($_GET['lang']){
case 'en':
$content = "This is English";
break;
case 'pt':
$content = "Isto é Português";
break;
}
答案 2 :(得分:1)
查看强>
<a href=<?php echo site_url('home?language=indonesian');?>>Indonesian language</a>
<强> CONTROLLER 强>
class Home extends CI_Controller {
public function index()
{
$language = $this->input->get('language');
if($language){
// Put your code
$this->session->set_userdata('language', $language);
redirect('home');
}
if($this->session->userdata('language'))
{
var_dump($this->session->userdata('language'));
}
echo 'Hello World!';
}
}
答案 3 :(得分:0)
//first here are your switching links
<?php $base_url = site_url()."/"; ?>
<li>
<a href='<?php echo $base_url; ?>langswitch/switchLanguage/arabic?redirect_to=<?php echo urlencode(current_url())?>'>العربية</a>
</li>
<li>
<a href='<?php echo $base_url; ?>langswitch/switchLanguage/english?redirect_to=<?php echo urlencode(current_url())?>'>English</a>
</li>
//----------------------then you need to write your controller like this
class LangSwitch extends CI_Controller {
private $redirect_to = '';
public function __construct() {
parent::__construct();
$this->redirect_to = base_url();
$redirect_to = isset($_GET["redirect_to"])?$_GET["redirect_to"]:"";
if(!empty($redirect_to)){
$this->redirect_to = urldecode($redirect_to);
}
$this->load->helper('url');
}
function switchLanguage($language = "") {
$language = ($language != "") ? $language : "english";
$this->session->set_userdata('site_lang', $language);
redirect($this->redirect_to);
}
}