doujiu4643
doujiu4643
2014-12-19 06:20
浏览 550
已采纳

使用自定义代码更改WordPress中的语言

I have site in WP that have multiple languages like (English,German and French).there url look like this :http://example.com(english). http://example.com/?lang=fr(french) and http://example.com/?lang=de(german) I am trying to change language before page loads.so i try some code on index.php but it creates issue. My code is :

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

It generate url like this with empty page.

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
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doujieju0397
    doujieju0397 2014-12-19 09:38
    已采纳

    I solved it using condition before the switch statment.It works

    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;
    }
    }
    

    While on Magento side i set the session before the layout load event like this.

    Mage::getSingleton('core/session')->setData("foo","magento");
    
    点赞 评论
  • dongzhanyan3667
    dongzhanyan3667 2014-12-19 06:35

    It is generating output like this because you have taken only french in switch.So every time it is getting that case only. What you can do is make an array of that language and after that use it in switch like this:

        $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;
            }
        }
    

    Please let me know the output here.

    点赞 评论

相关推荐