我在WP中有多个语言的网站,例如(English,German and French)
。网址看起来像:http://example.com(english). http://example.com/?lang=fr(french) and http://example.com/?lang=de(german)
我试图在页面加载之前更改语言。所以我在index.php
上尝试了一些代码,但它会产生问题。
我的代码是:
$currentUrl = 'this is current page url';
$store_code = "french";
switch ($store_code) {
case 'french':
$_baseurl = $currentUrl."?lang=fr";
echo "<script type='text/javascript'>window.location='{$_baseurl}'</script>";
break;
case 'german':
$_baseurl = $currentUrl."?lang=de";
echo "<script type='text/javascript'>window.location='{$_baseurl}'</script>";
break;
case 'mandarin':
$_baseurl = $currentUrl."?lang=zh";
echo "<script type='text/javascript'>window.location='{$_baseurl}'</script>";
break;
default:
$_baseurl = $currentUrl;
echo "<script type='text/javascript'>window.location='{$_baseurl}'</script>";
break;
}
Output.
它使用空页生成这样的URL。
http://example.com/?lang=fr?lang=fr?lang=fr?lang=fr?lang=fr?lang=fr?lang=fr?lang=fr?lang=fr?lang=fr?lang=fr?lang=fr?lang=fr?lang=fr?lang=fr
答案 0 :(得分:0)
我在切换声明之前使用条件解决了它。它正常工作
define('WP_USE_THEMES', true);
require("../app/Mage.php");
umask(0);
Mage::app('default');
$currentUrl = Mage::helper('core/url')->getCurrentUrl();
$store_code = Mage::app()->getStore()->getCode();
if(Mage::getSingleton('core/session', array('name' => 'frontend'))->getData("foo") !='bar')
{
Mage::getSingleton('core/session')->setData("foo","bar");
switch ($store_code) {
case 'french':
$_baseurl = $currentUrl."?lang=fr";
echo "<script type='text/javascript'>window.location='{$_baseurl}'</script>";
break;
case 'german':
$_baseurl = $currentUrl."?lang=de";
echo "<script type='text/javascript'>window.location='{$_baseurl}'</script>";
break;
case 'mandarin':
$_baseurl = $currentUrl."?lang=zh";
echo "<script type='text/javascript'>window.location='{$_baseurl}'</script>";
break;
default:
$_baseurl = $currentUrl;
echo "<script type='text/javascript'>window.location='{$_baseurl}'</script>";
break;
}
}
在Magento方面,我在布局加载事件之前设置会话,如下所示。
Mage::getSingleton('core/session')->setData("foo","magento");
答案 1 :(得分:-1)
正在生成这样的output
,因为每次只获取case
时,你在switch.So中只使用了法语。您可以做的是制作一个该语言的语言,然后在switch
中使用它,如下所示:
$currentUrl = 'this is current page url';
$store_code = array('french', 'german', 'mandarin');
foreach($store_code as $mylang){
switch ($mylang) {
case 'french':
$_baseurl = $currentUrl."?lang=fr";
echo "<script type='text/javascript'>window.location='{$_baseurl}'</script>";
break;
case 'german':
$_baseurl = $currentUrl."?lang=de";
echo "<script type='text/javascript'>window.location='{$_baseurl}'</script>";
break;
case 'mandarin':
$_baseurl = $currentUrl."?lang=zh";
echo "<script type='text/javascript'>window.location='{$_baseurl}'</script>";
break;
default:
$_baseurl = $currentUrl;
echo "<script type='text/javascript'>window.location='{$_baseurl}'</script>";
break;
}
}
请告诉我这里的输出。