Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translates: title, header and breadcrumbs #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Translate CMS Pages in Magento.

Use: In any CMS Page or Block use {{translate text="Text here"}} and the text will be translatable with the Inline translator.

Besides the inline translator the module will also translate title in head and header block as well as enable translation for page breadcrumbs.

Source: http://www.magentocommerce.com/boards/viewthread/179598/#

If you want this to be a part of Magento2, add a +1 <a href="https://github.com/magento/magento2/pull/875" target="_blank">here</a>.
Expand Down
14 changes: 14 additions & 0 deletions app/code/local/MB/Translate/Block/Core/Template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

class MB_Translate_Block_Core_Template extends Mage_Core_Block_Template
{
public function __construct()
{
parent::__construct();
}

public function getContentHeading()
{
return Mage::helper('mb_translate')->getTranslateModule()->__(parent::getContentHeading());
}
}
17 changes: 17 additions & 0 deletions app/code/local/MB/Translate/Block/Page/Html/Breadcrumbs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

class MB_Translate_Block_Page_Html_Breadcrumbs extends Mage_Page_Block_Html_Breadcrumbs
{
public function __construct()
{
parent::__construct();
}

public function addCrumb($crumbName, $crumbInfo, $after = false)
{
$module = Mage::helper('mb_translate')->getTranslateModule();
if (isset($crumbInfo['label'])) $crumbInfo['label'] = $module->__($crumbInfo['label']);
if (isset($crumbInfo['title'])) $crumbInfo['title'] = $module->__($crumbInfo['title']);
return parent::addCrumb($crumbName, $crumbInfo, $after);
}
}
38 changes: 38 additions & 0 deletions app/code/local/MB/Translate/Block/Page/Html/Head.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

class MB_Translate_Block_Page_Html_Head extends Mage_Page_Block_Html_Head
{
protected $shouldTranslate;

public function __construct()
{
parent::__construct();
}

public function setTranslate(bool $shouldTranslate = true)
{
$this->shouldTranslate = $shouldTranslate;
return $this;
}

public function getTitle()
{
$title = parent::getTitle();
if ($this->shouldTranslate) return Mage::helper('mb_translate')->getTranslateModule()->__($title);
return $title;
}

public function getDescription()
{
$description = parent::getDescription();
if ($this->shouldTranslate) return Mage::helper('mb_translate')->getTranslateModule()->__($description);
return $description;
}

public function getKeywords()
{
$keywords = parent::getKeywords();
if ($this->shouldTranslate) return Mage::helper('mb_translate')->getTranslateModule()->__($keywords);
return $keywords;
}
}
63 changes: 57 additions & 6 deletions app/code/local/MB/Translate/Helper/Data.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,57 @@
<?php

class MB_Translate_Helper_Data extends Mage_Core_Helper_Abstract
{

}
<?php

class MB_Translate_Helper_Data extends Mage_Core_Helper_Abstract
{
const XML_DEFAULT_MODULE_PATH = 'cms/translate/default_module';
const DEFAULT_MODULE = 'page';
protected $_defaultModule = null;

public function getTranslateModule($module = null)
{
return Mage::helper($this->getTranslateModuleName($module));
}

public function getTranslateModuleName($module = null)
{
if (empty($module)) return $this->_getDefaultModule();
return $this->_getTranslateModuleName($module);
}

protected function _getTranslateModuleName($module)
{
try
{
$helper = Mage::helper($module);
return $module;
}
catch (Exception $e)
{
return $this->_getDefaultModule();
}
}

protected function _getDefaultModule()
{
if (is_null($this->_defaultModule))
{
$moduleName = Mage::getStoreConfig(self::XML_DEFAULT_MODULE_PATH);
if (empty($moduleName))
{
$this->_defaultModule = self::DEFAULT_MODULE;
}
else
{
try
{
$helper = Mage::helper($moduleName);
$this->_defaultModule = $moduleName;
}
catch (Exception $e)
{
$this->_defaultModule = self::DEFAULT_MODULE;
}
}
}
return $this->_defaultModule;
}
}
46 changes: 4 additions & 42 deletions app/code/local/MB/Translate/Model/Template/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,12 @@

class MB_Translate_Model_Template_Filter extends Mage_Widget_Model_Template_Filter
{
const XML_DEFAULT_MODULE_PATH = 'cms/translate/default_module';
const DEFAULT_MODULE = 'page';
protected $_defaultModule = null;
public function translateDirective($construction)
{
$params = $this->_getIncludeParameters($construction[2]);
$text = $params['text'];
$module = (!empty($params['module'])) ? $this->_getTranslateModuleName($params['module']) : $this->_getDefaultModule();
return Mage::helper($module)->__($text);
}
protected function _getTranslateModuleName($module)
{
try
{
$helper = Mage::helper($module);
return $module;
}
catch (Exception $e)
{
return $this->_getDefaultModule();
}
}
protected function _getDefaultModule()
{
if (is_null($this->_defaultModule))
{
$moduleName = Mage::getStoreConfig(self::XML_DEFAULT_MODULE_PATH);
if (empty($moduleName))
{
$this->_defaultModule = self::DEFAULT_MODULE;
}
else
{
try
{
$helper = Mage::helper($moduleName);
$this->_defaultModule = $moduleName;
}
catch (Exception $e)
{
$this->_defaultModule = self::DEFAULT_MODULE;
}
}
}
return $this->_defaultModule;
return Mage::helper('mb_translate')
->getTranslateModule(isset($params['module']) ? $params['module'] : null)
->__($params['text'])
;
}
}
100 changes: 60 additions & 40 deletions app/code/local/MB/Translate/etc/config.xml
Original file line number Diff line number Diff line change
@@ -1,40 +1,60 @@
<?xml version="1.0"?>
<config>
<modules>
<MB_Translate>
<version>0.1.0</version>
</MB_Translate>
</modules>
<global>
<models>
<widget>
<rewrite>
<template_filter>MB_Translate_Model_Template_Filter</template_filter>
</rewrite>
</widget>
</models>
<helpers>
<mb_translate>
<class>MB_Translate_Helper</class>
</mb_translate>
</helpers>
</global>
<adminhtml>
<translate>
<modules>
<MB_Translate>
<files>
<default>MB_Translate.csv</default>
</files>
</MB_Translate>
</modules>
</translate>
</adminhtml>
<default>
<cms>
<translate>
<default_module>page</default_module>
</translate>
</cms>
</default>
</config>
<?xml version="1.0"?>
<config>
<modules>
<MB_Translate>
<version>0.1.0</version>
</MB_Translate>
</modules>
<global>
<models>
<widget>
<rewrite>
<template_filter>MB_Translate_Model_Template_Filter</template_filter>
</rewrite>
</widget>
</models>
<helpers>
<mb_translate>
<class>MB_Translate_Helper</class>
</mb_translate>
</helpers>
<blocks>
<mb_translate>
<class>MB_Translate_Block</class>
</mb_translate>
<page>
<rewrite>
<html_breadcrumbs>MB_Translate_Block_Page_Html_Breadcrumbs</html_breadcrumbs>
<html_head>MB_Translate_Block_Page_Html_Head</html_head>
</rewrite>
</page>
</blocks>
</global>
<frontend>
<layout>
<updates>
<mb_translate>
<file>mb_translate.xml</file>
</mb_translate>
</updates>
</layout>
</frontend>
<adminhtml>
<translate>
<modules>
<MB_Translate>
<files>
<default>MB_Translate.csv</default>
</files>
</MB_Translate>
</modules>
</translate>
</adminhtml>
<default>
<cms>
<translate>
<default_module>page</default_module>
</translate>
</cms>
</default>
</config>
21 changes: 21 additions & 0 deletions app/design/frontend/base/default/layout/mb_translate.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" ?>
<layout version="0.1.0">
<default>
<!-- Enable translation in head on all pages -->
<reference name="head">
<action method="setTranslate">true</action>
</reference>
</default>
<print>
<!-- Enable translation in head on all pages -->
<reference name="head">
<action method="setTranslate">true</action>
</reference>
</print>
<!-- Replace header block -->
<cms_page translate="label">
<reference name="content">
<block type="mb_translate/core_template" name="page_content_heading" template="cms/content_heading.phtml"/>
</reference>
</cms_page>
</layout>
7 changes: 4 additions & 3 deletions modman
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
app/code/local/MB/Translate app/code/local/MB/Translate
app/etc/modules/MB_Translate.xml app/etc/modules/MB_Translate.xml
app/locale/en_US/*.csv app/locale/en_US/
app/code/local/MB/Translate app/code/local/MB/Translate
app/etc/modules/MB_Translate.xml app/etc/modules/MB_Translate.xml
app/locale/en_US/*.csv app/locale/en_US/
app/design/frontend/base/default/layout/mb_translate.xml app/design/frontend/base/default/layout/mb_translate.xml