Skip to content

Commit

Permalink
0.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabrizio Balliano committed Feb 17, 2015
1 parent 8847b85 commit 68150cc
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
0.5.0
-----
* support for custom translation command was introduced

0.4.0
-----
* a bug with Magmi and table prefixes was solved
Expand Down
16 changes: 16 additions & 0 deletions app/code/community/Fballiano/FullCatalogTranslate/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,20 @@ public function getApiKey()
{
return Mage::getStoreConfig("fballiano_full_catalog_translate/google_translate/api_key");
}

/**
* @return string
*/
public function getTranslationSystem()
{
return Mage::getStoreConfig("fballiano_full_catalog_translate/general/translation_system");
}

/**
* @return string
*/
public function getCommand()
{
return Mage::getStoreConfig("fballiano_full_catalog_translate/custom/command");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* FBalliano
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this Module to
* newer versions in the future.
*
* @category FBalliano
* @package FBalliano_FullCatalogTranslate
* @copyright Copyright (c) 2014 Fabrizio Balliano (http://fabrizioballiano.it)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

class Fballiano_FullCatalogTranslate_Model_TranslationSystems
{
public function toOptionArray()
{
$toreturn = array();
$toreturn[] = array(
"value" => "googletranslate",
"label" => "Google Translate"
);
$toreturn[] = array(
"value" => "custom",
"label" => "Custom"
);

return $toreturn;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<default>
<fballiano_full_catalog_translate>
<general>
<translation_system>googletranslate</translation_system>
<attributes_to_translate>name,short_description,description,meta_title,meta_keyword,meta_description</attributes_to_translate>
</general>
</fballiano_full_catalog_translate>
Expand Down
24 changes: 24 additions & 0 deletions app/code/community/Fballiano/FullCatalogTranslate/etc/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
<show_in_store>0</show_in_store>
<sort_order>10</sort_order>
<fields>
<translation_system>
<label>Translation system</label>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
<frontend_type>select</frontend_type>
<source_model>fballiano_fullcatalogtranslate/translationSystems</source_model>
</translation_system>
<attributes_to_translate>
<label>Attributes to translate</label>
<show_in_default>1</show_in_default>
Expand All @@ -41,6 +49,22 @@
</api_key>
</fields>
</google_translate>
<custom>
<label>Custom translation system</label>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
<sort_order>30</sort_order>
<fields>
<command>
<label>Command</label>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
<comment><![CDATA[Type the complete command here (eg: /bin/mytranslator.sh <span style="white-space:nowrap">--source="%SOURCELANGUAGE%"</span> <span style="white-space:nowrap">--to="%TARGETLANGUAGE%"</span> <span style="white-space:nowrap">--string="%STRING%"</span>)]]></comment>
</command>
</fields>
</custom>
</groups>
</fballiano_full_catalog_translate>
</sections>
Expand Down
47 changes: 36 additions & 11 deletions shell/fballiano_full_catalog_translate.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
class Fballiano_FullCatalogTranslate_Shell extends Mage_Shell_Abstract
{
protected $helper = null;
protected $translation_system = null;
protected $api_key = null;
protected $command = null;
protected $store_source = null;
protected $store_dest = null;
protected $language_source = null;
Expand All @@ -37,10 +39,22 @@ class Fballiano_FullCatalogTranslate_Shell extends Mage_Shell_Abstract
public function run()
{
$this->helper = Mage::helper("fballiano_fullcatalogtranslate");
$this->translation_system = $this->helper->getTranslationSystem();
$this->attributes_to_translate = $this->helper->getAttributesToTranslate();
$this->api_key = $this->helper->getApiKey();
if (!$this->api_key) die("Please set your API key in the Magento admin configuration.\n");
$this->ws_url .= "?key={$this->api_key}";
$this->api_key = $this->helper->getApiKey();
$this->ws_url .= "?key={$this->api_key}";
$this->command = $this->helper->getCommand();

switch ($this->translation_system) {
case "googletranslate":
if (!$this->api_key) die("Please set your API key in the Magento admin configuration.\n");
break;
case "custom":
if (!$this->command) die("Please set the translation command in the Magento admin configuration.\n");
break;
default:
die("Unrecognized translation system: {$this->translation_system}.\n");
}

$args = array_keys($this->_args);
$this->store_source = @$args[0];
Expand Down Expand Up @@ -93,10 +107,7 @@ public function run()
$translated_row["fb_translate"] = "0"; //leave it as string otherwise magmi won't save it
foreach ($this->attributes_to_translate as $attribute) {
if (strlen($row[$attribute])) {
$ws_url = "{$this->ws_url}&q=" . urlencode($row[$attribute]);
$translated = json_decode(file_get_contents($ws_url), true);
$translated = $translated["data"]["translations"][0]["translatedText"];
$translated_row[$attribute] = (string)$translated;
$translated_row[$attribute] = $this->translateString($row[$attribute]);
}
}
$this->datapump->ingest($translated_row);
Expand All @@ -118,17 +129,31 @@ public function productCollectionWalkCallback($args)
$translated_row["fb_translate"] = 0;
foreach ($this->attributes_to_translate as $attribute) {
if (strlen($row[$attribute])) {
$ws_url = "{$this->ws_url}&q=" . urlencode($row[$attribute]);
$translated = json_decode(file_get_contents($ws_url), true);
$translated = $translated["data"]["translations"][0]["translatedText"];
$translated_row[$attribute] = $translated;
$translated_row[$attribute] = $this->translateString($row[$attribute]);
}
}

$this->datapump->ingest($translated_row);
echo "OK\n";
}

public function translateString($string)
{
switch ($this->translation_system) {
case "googletranslate":
$ws_url = "{$this->ws_url}&q=" . urlencode($string);
$translated = json_decode(file_get_contents($ws_url), true);
return (string)$translated["data"]["translations"][0]["translatedText"];
case "custom":
$command = str_replace(
array("%SOURCELANGUAGE%", "%TARGETLANGUAGE%", "%STRING%"),
array($this->language_source, $this->language_dest, $string),
$this->command
);
return shell_exec($command);
}
}

public function usageHelp()
{
return <<<USAGE
Expand Down

0 comments on commit 68150cc

Please sign in to comment.