From 68150cca9e343c65ca63ce033e3482de76d4c339 Mon Sep 17 00:00:00 2001 From: Fabrizio Balliano Date: Tue, 17 Feb 2015 20:19:37 +0000 Subject: [PATCH] 0.5.0 --- CHANGELOG.md | 4 ++ .../FullCatalogTranslate/Helper/Data.php | 16 +++++++ .../Model/TranslationSystems.php | 39 +++++++++++++++ .../FullCatalogTranslate/etc/config.xml | 1 + .../FullCatalogTranslate/etc/system.xml | 24 ++++++++++ shell/fballiano_full_catalog_translate.php | 47 ++++++++++++++----- 6 files changed, 120 insertions(+), 11 deletions(-) create mode 100644 app/code/community/Fballiano/FullCatalogTranslate/Model/TranslationSystems.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 723349c..5c4417b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/app/code/community/Fballiano/FullCatalogTranslate/Helper/Data.php b/app/code/community/Fballiano/FullCatalogTranslate/Helper/Data.php index 76793fe..af373cc 100644 --- a/app/code/community/Fballiano/FullCatalogTranslate/Helper/Data.php +++ b/app/code/community/Fballiano/FullCatalogTranslate/Helper/Data.php @@ -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"); + } } \ No newline at end of file diff --git a/app/code/community/Fballiano/FullCatalogTranslate/Model/TranslationSystems.php b/app/code/community/Fballiano/FullCatalogTranslate/Model/TranslationSystems.php new file mode 100644 index 0000000..557e94e --- /dev/null +++ b/app/code/community/Fballiano/FullCatalogTranslate/Model/TranslationSystems.php @@ -0,0 +1,39 @@ + "googletranslate", + "label" => "Google Translate" + ); + $toreturn[] = array( + "value" => "custom", + "label" => "Custom" + ); + + return $toreturn; + } +} \ No newline at end of file diff --git a/app/code/community/Fballiano/FullCatalogTranslate/etc/config.xml b/app/code/community/Fballiano/FullCatalogTranslate/etc/config.xml index c6e0867..70bc5be 100644 --- a/app/code/community/Fballiano/FullCatalogTranslate/etc/config.xml +++ b/app/code/community/Fballiano/FullCatalogTranslate/etc/config.xml @@ -28,6 +28,7 @@ + googletranslate name,short_description,description,meta_title,meta_keyword,meta_description diff --git a/app/code/community/Fballiano/FullCatalogTranslate/etc/system.xml b/app/code/community/Fballiano/FullCatalogTranslate/etc/system.xml index 0998d7f..47c95fb 100644 --- a/app/code/community/Fballiano/FullCatalogTranslate/etc/system.xml +++ b/app/code/community/Fballiano/FullCatalogTranslate/etc/system.xml @@ -15,6 +15,14 @@ 0 10 + + + 1 + 0 + 0 + select + fballiano_fullcatalogtranslate/translationSystems + 1 @@ -41,6 +49,22 @@ + + + 1 + 0 + 0 + 30 + + + + 1 + 0 + 0 + --source="%SOURCELANGUAGE%" --to="%TARGETLANGUAGE%" --string="%STRING%")]]> + + + diff --git a/shell/fballiano_full_catalog_translate.php b/shell/fballiano_full_catalog_translate.php index 23e8e15..d57e95b 100644 --- a/shell/fballiano_full_catalog_translate.php +++ b/shell/fballiano_full_catalog_translate.php @@ -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; @@ -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]; @@ -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); @@ -118,10 +129,7 @@ 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]); } } @@ -129,6 +137,23 @@ public function productCollectionWalkCallback($args) 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 <<