服务报价 | 域名主机 | 网络营销 | 软件工具| [加入收藏]
 热线电话: #
当前位置: 主页 > php教程 > magento教程 >

在Magento单个店铺用所有语言和本地日期打印信息

时间:2017-01-08 11:55来源:未知 作者:最模板 点击:
你是否尝试过在Magento单个页面用所有语言打印一条信息? 首先,让我来告诉你何时我需要这么做。最近我需要开发一个发送些定制邮件的功能。你已经知道我们可以在网站/店铺设置交

你是否尝试过在Magento单个页面用所有语言打印一条信息?

首先,让我来告诉你何时我需要这么做。最近我需要开发一个发送些定制邮件的功能。你已经知道我们可以在网站/店铺设置交易邮件并将这些邮件在administration/db翻译。我所要面对的挑战就是需要翻译一些交易邮件中的随机句子,所以使用预定义的模版是不可能的。在email模版我们是可以用and/or连接变量,但对我来说没什么帮助。

如果你安装的Magento社区版在1.4.2.0以上,那么我可以开始我们的翻译模块了。

  1. 告诉Magento我们模块的信息;
  2. 创建模块结构;
  3. 编写config.xml;
  4. 编写Translate.php模型;
  5. 编写Locale.php模型;
  6. 编写Data.phph助手;
  7. 编写index.php。

1.用下面的代码创建app/etc/modules/Alwayly_Translation.xml:

<?xml version="1.0"?>
<config>
    <modules>
       <Alwayly_Translate>
            <active>true</active>
            <codePool>local</codePool>
        </Alwayly_Translate>
    </modules>
</config>

2.创建app/code/local/Alwayly/Translate下的文件夹: etc, controllers, Model, Helper

3.用下面的代码创建app/code/local/Alwayly/Translate/config.xml:

<?xml version="1.0"?>
<config>
 
	<modules>
		<Alwayly_Translate>
			<version>0.1.0</version>
		</Alwayly_Translate>
	</modules>
 
	<frontend>
		<routers>
			<Alwayly_Translate>
				<use>standard</use>
				<args>
					<module>Alwayly_Translate</module>
					<frontName>Alwayly</frontName>
				</args>
			</Alwayly_Translate>
		</routers>
	</frontend>
 
	<global>
		<models>
			<Alwayly_translate>
				<class>Alwayly_Translate_Model</class>
			</Alwayly_translate>
 
			<core>
				<rewrite>
					<locale>Alwayly_Translate_Model_Mage_Core_Model_Locale</locale>
					<translate>Alwayly_Translate_Model_Mage_Core_Model_Translate</translate>
				</rewrite>
			</core>
		</models>
 
		<helpers>
			<Alwayly_translate>
				<class>Alwayly_Translate_Helper</class>
			</Alwayly_translate>
		</helpers>
	</global>
 
</config>

如你所见,我们需要重写两个Magento模型。我们需要删除一个Translate模型中的if语句,在Locale模型中添加几个“*ByLocaleCode”方法,这样就可以在我们所有的Action里使用商店代码。

在助手文件中我们将添加方法处理日期(关于本地化)。因为当Magento初始化,一些请求会为特定的本地读取内存中的各种缓存。当你尝试想要做类似的事情:Mage::helper(‘core’)->__(“translate me”)Magento将使用cache/memory。你可以看到:

/**
 * Translate model
 *
 * @author      Magento Core Team <core@magentocommerce.com>
 */
class Mage_Core_Model_Translate
//...
 
/**
     * Return translated string from text.
     *
     * @param string $text
     * @param string $code
     * @return string
     */
    protected function _getTranslatedString($text, $code)
    {
        $translated = '';
        if (array_key_exists($code, $this->getData())) {
            $translated = $this->_data[$code];
        }
        elseif (array_key_exists($text, $this->getData())) {
            $translated = $this->_data[$text];
        }
        else {
            $translated = $text;
        }
        return $translated;
    }

4.用下面的代码创建app/code/local/app/code/local/Alwayly/Translate/Model/Mage/Core/
Model/Locale.php/Translate/Model/Mage/Core/Model/Translate.php

<?php
 
/**
 * Translate model
 *
 */
class Alwayly_Translate_Model_Mage_Core_Model_Translate extends Mage_Core_Model_Translate
{
     /**
     * Retrieve locale
     *
     * @return string
     */
    public function getLocale()
    {
		$this->_locale = Mage::app()->getLocale()->getLocaleCode();
		return $this->_locale;
	}
}

我们只是移除了getLocale()方法中的if语句。

5.创建app/code/local/Alwayly/Translate/Model/Mage/Core/Model/Locale.php

<?php
/**
 * Locale model
 * 
 */
 
class Alwayly_Translate_Model_Mage_Core_Model_Locale extends Mage_Core_Model_Locale
{
    /**
     * Create Zend_Date object with date converted to store timezone and store Locale
     *
     * @param   mixed $store Information about store
     * @param   string|integer|Zend_Date|array|null $date date in UTC
     * @param   boolean $includeTime flag for including time to date
     * @return  Zend_Date
     */
    public function getStoreDate($store=null, $date=null, $includeTime=false)
    {
        $timezone = Mage::app()->getStore($store)->getConfig(self::XML_PATH_DEFAULT_TIMEZONE);
        $locale = Mage::app()->getStore($store)->getConfig(self::XML_PATH_DEFAULT_LOCALE);
        $date = new Zend_Date($date, null, $locale);
        $date->setTimezone($timezone);
        if (!$includeTime) {
            $date->setHour(0)
                ->setMinute(0)
                ->setSecond(0);
        }
        return $date;
    }
 
    /**
	 * Returns a localized information string, supported are several types of informations.
	 * For detailed information about the types look into the documentation
	 *
	 * @param  string             $value  Name to get detailed information about
	 * @param  string             $path   (Optional) Type of information to return
	 * @return string|false The wished information in the given language
	 */
	public function getTranslationByLocaleCode($value = null, $path = null, $localeCode = null)
	{
		return $this->getLocale()->getTranslation($value, $path, $localeCode/*$this->getLocale()*/);
	}
	/**
	 * Retrieve ISO date format
	 *
	 * @param   string $type
	 * @return  string
	 */
	public function getDateFormatByLocaleCode($type=null, $localeCode)
	{
		return $this->getTranslationByLocaleCode($type, 'date', $localeCode);
	}
 
	/**
	 * Retrieve ISO time format
	 *
	 * @param   string $type
	 * @return  string
	 */
	public function getTimeFormatByLocaleCode($type=null, $localeCode)
	{
		return $this->getTranslationByLocaleCode($type, 'time', $localeCode);
	}
 
	/**
	 * Retrieve ISO datetime format
	 *
	 * @param   string $type
	 * @return  string
	 */
	public function getDateTimeFormatByLocaleCode($type, $localeCode)
	{
		return $this->getDateFormatByLocaleCode($type, $localeCode) . ' ' . $this->getTimeFormatByLocaleCode($type, $localeCode);
	}
}

注意,我添加了几个“*ByLocaleCode” 方法,这样能使得Magento核心文件不被修改。

6、创建助手文件在app/code/local/Alwayly/Translate/Helper/Data.php

<?php
 
/**
 * Translate helper
 *
 */
class Alwayly_Translate_Helper_Data extends Mage_Core_Helper_Abstract
{
 
	/**
	 * Format date using specifed locale
	 *
	 * @param   date|Zend_Date|null $date in GMT timezone
	 * @param   string $format
	 * @param   bool $showTime
	 * @return  string
	 */
	public function formatDateByLocaleCode($date=null, $format='short', $showTime=false, $localeCode=null)
	{
		if ($localeCode === null || !is_string($localeCode)) {
			$localeCode = Mage::getStoreConfig('general/locale/code');
		}
		if (Mage_Core_Model_Locale::FORMAT_TYPE_FULL    	!==$format &&
				Mage_Core_Model_Locale::FORMAT_TYPE_LONG    !==$format &&
				Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM  !==$format &&
				Mage_Core_Model_Locale::FORMAT_TYPE_SHORT   !==$format) {
			return $date;
		}
		if (!($date instanceof Zend_Date) && $date && !strtotime($date)) {
			return '';
		}
		if (is_null($date)) {
			$date = Mage::app()->getLocale()->date(Mage::getSingleton('core/date')->gmtTimestamp(), null, null);
		}
		elseif (!$date instanceof Zend_Date) {
			$date = Mage::app()->getLocale()->date(strtotime($date), null, null, $showTime);
		}
 
		if ($showTime) {
			$format = Mage::app()->getLocale()->getDateTimeFormatByLocaleCode($format, $localeCode);
		}
		else {
			$format = Mage::app()->getLocale()->getDateFormatByLocaleCode($format, $localeCode);
		}
		return $date->toString($format);
	}
}

注意,我们最后在if/else判断里调用->getDate(Time/Format)ByLocaleCode方法来替换我们的Locale模型。

7、创建控制器文件app/code/local/Alwayly/Translate/controllers/IndexController.php

<?php
 
/**
 * http://films.local/index.php/ExpiringFilmsNotifier/index/notify
 */
class Alwayly_Translate_IndexController extends Mage_Core_Controller_Front_Action
{
    public function translateAction()
    {
    	echo "
";
 
    	$defaultStore = Mage::app()->getStore()->getCode();
    	$defaultLocale = Mage::app()->getLocale()->getLocaleCode();
    	$stores = array(1,2,3);
 
    	foreach ($stores as $storeId) {
    		Mage::app()->setCurrentStore($storeId);
    		$currentStore 	= Mage::app()->getStore()->getCode();
    		$currentLocale 	= Mage::getModel('core/locale')->getLocaleCode();
    		Mage::app()->getLocale()->setLocale($currentLocale);
    		// reinitialize translation cache
    		Mage::app()->getTranslator()->init('frontend', true);
 
    		echo Mage::helper('core')->__("Welcome, %s!", Mage::helper('alwayly_translate')
    			->formatDateByLocaleCode('2012-05-05 00:00:00', 'medium', false, $currentLocale))  . PHP_EOL;
    	}
    	//restore app to default values
    	Mage::app()->setCurrentStore($defaultStore);
    	Mage::app()->getLocale()->setLocale($defaultLocale);
    	Mage::app()->getTranslator()->init('frontend', true);
 
    	echo 'Did system restore default initialization?' . PHP_EOL;
    	echo Mage::helper('core')->__("Welcome, %s!", Mage::helper('alwayly_translate')
    			->formatDateByLocaleCode('2012-05-05 00:00:00', 'medium', false, $currentLocale))  . PHP_EOL;
    	exit;
    }
}

注意,我们先创建了两个变量$defaultStore和$defaultLocale,这样我妈就可以在customer页面回复Magento自定义。如果你有三个店铺确认为它们设置了本地化。

我在in app/etc/locale/de_DE/和app/etc/locale/fr_FR/ 创建了Mage_Page.csv ,代码如下:

For German:
“Welcome, %s!”,”de_DE Welcome, %s!”
For France :
“Welcome, %s!”,”fr_FR Welcome, %s!”
(责任编辑:最模板)
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
栏目列表
热点内容