Magento指定店鋪翻譯

給指定模塊添加對應翻譯文件,並在配置文件config.php配置好

<config>
    <frontend>
        <translate>
            <modules>
                <Core>
                    <files>
                        <default>Core.csv</default>
                    </files>
                </Core>
            </modules>
        </translate>
    </frontend>
</config>

根據指定的店鋪找到對應的店鋪code

public static function getStoreConfig($path, $store = null)
{
    return self::app()->getStore($store)->getConfig($path);
}
$newLocaleCode = Mage::getStoreConfig(
    Mage_Core_Model_Locale::XML_PATH_DEFAULT_LOCALE, $storeId
);

$initialEnvironmentInfo = Mage::getSingleton('core/app_emulation')->startEnvironmentEmulation($storeId);
Mage::app()->getLocale()->setLocaleCode($newLocaleCode);
Mage::getSingleton('core/translate')->setLocale($newLocaleCode)->init(Mage_Core_Model_App_Area::AREA_FRONTEND, true);

$translatedString = Mage::helper('core')->__($string);
Mage::getSingleton('core/app_emulation')->stopEnvironmentEmulation($initialEnvironmentInfo);

$newLocaleCode是根據當前的$storeId得到對應的站點code

$initialEnvironmentInfo是根據$storeId模擬出該店鋪環境

第三句是將指定的店鋪code設定爲當前店鋪code

第四句是設置好當前店鋪,然後初始化找到對應的翻譯

第五句是停止當前環境模擬

這段代碼整體思路也算是magento內部翻譯的處理邏輯,底層方法比較亂,感興趣的可以自己研究一下,不過這段代碼總結起來就是先根據指定的店鋪模擬出一個對應店鋪的虛擬環境,然後將指定的店鋪設置成當前店鋪找到當前店鋪對應的翻譯,然後初始化將該店鋪下的所有翻譯文件裏面的翻譯語句拿出來組裝到一起,然後根據給定的語句到這個組裝好的翻譯裏面去找這句話對應的翻譯,然後再關閉當前模擬的虛擬店鋪

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章