From ad4aeae05a5fc4b5980acf6596ee1542f49ad05c Mon Sep 17 00:00:00 2001 From: Fabrizio Balliano Date: Thu, 2 Mar 2017 13:58:22 +0000 Subject: [PATCH] * code reformat * Google Translate API URL was changed (now is "https://translation.googleapis.com/language/translate/v2") --- CHANGELOG.md | 5 + .../FullCatalogTranslate/Helper/Data.php | 55 +++--- .../Model/TranslatableAttributes.php | 2 +- .../Model/TranslatableCategoryAttributes.php | 2 +- .../Model/TranslationSystems.php | 18 +- .../FullCatalogTranslate/etc/adminhtml.xml | 38 ++-- .../FullCatalogTranslate/etc/config.xml | 64 +++--- .../FullCatalogTranslate/etc/system.xml | 88 ++++----- .../install-0.1.0.php | 2 +- .../upgrade-0.6.1-0.7.0.php | 40 ++-- .../Fballiano_FullCatalogTranslate.xml | 18 +- shell/fballiano_full_catalog_translate.php | 182 +++++++++--------- 12 files changed, 260 insertions(+), 254 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index afeaa9a..68533c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +0.7.3 +----- +* code reformat +* Google Translate API URL was changed (now is "https://translation.googleapis.com/language/translate/v2") + 0.7.2 ----- * mbstring extension is required (check added) if you use Google Translate API. diff --git a/app/code/community/Fballiano/FullCatalogTranslate/Helper/Data.php b/app/code/community/Fballiano/FullCatalogTranslate/Helper/Data.php index 85d4230..9b2b9da 100755 --- a/app/code/community/Fballiano/FullCatalogTranslate/Helper/Data.php +++ b/app/code/community/Fballiano/FullCatalogTranslate/Helper/Data.php @@ -1,4 +1,5 @@ $v) { + foreach ($attributes as $k => $v) { $attributes[$k] = trim($v); } return $attributes; } - /** - * @return array - */ - public function getCategoryAttributesToTranslate() - { - $attributes = Mage::getStoreConfig("fballiano_full_catalog_translate/general/category_attributes_to_translate"); - $attributes = explode(",", $attributes); - foreach ($attributes as $k=>$v) { - $attributes[$k] = trim($v); - } + /** + * @return array + */ + public function getCategoryAttributesToTranslate() + { + $attributes = Mage::getStoreConfig("fballiano_full_catalog_translate/general/category_attributes_to_translate"); + $attributes = explode(",", $attributes); + foreach ($attributes as $k => $v) { + $attributes[$k] = trim($v); + } - return $attributes; - } + return $attributes; + } /** * @return string @@ -57,19 +58,19 @@ 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 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"); - } + /** + * @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/TranslatableAttributes.php b/app/code/community/Fballiano/FullCatalogTranslate/Model/TranslatableAttributes.php index fdb8abe..25a5f59 100644 --- a/app/code/community/Fballiano/FullCatalogTranslate/Model/TranslatableAttributes.php +++ b/app/code/community/Fballiano/FullCatalogTranslate/Model/TranslatableAttributes.php @@ -1,4 +1,5 @@ "googletranslate", - "label" => "Google Translate" - ); - $toreturn[] = array( - "value" => "custom", - "label" => "Custom" - ); + $toreturn[] = array( + "value" => "googletranslate", + "label" => "Google Translate" + ); + $toreturn[] = array( + "value" => "custom", + "label" => "Custom" + ); return $toreturn; } diff --git a/app/code/community/Fballiano/FullCatalogTranslate/etc/adminhtml.xml b/app/code/community/Fballiano/FullCatalogTranslate/etc/adminhtml.xml index 15445a2..8df552d 100644 --- a/app/code/community/Fballiano/FullCatalogTranslate/etc/adminhtml.xml +++ b/app/code/community/Fballiano/FullCatalogTranslate/etc/adminhtml.xml @@ -1,22 +1,22 @@ - - - - - - - - - - FBalliano Full Catalog Translate - - - - - - - - - + + + + + + + + + + FBalliano Full Catalog Translate + + + + + + + + + \ 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 4a21802..e0d2911 100755 --- a/app/code/community/Fballiano/FullCatalogTranslate/etc/config.xml +++ b/app/code/community/Fballiano/FullCatalogTranslate/etc/config.xml @@ -1,37 +1,37 @@ - - - 0.7.0 - - - - - - Fballiano_FullCatalogTranslate_Helper - - - - - Fballiano_FullCatalogTranslate_Model - - - - - - Fballiano_FullCatalogTranslate - Mage_Catalog_Model_Resource_Setup - - - - - - - + + + 0.7.3 + + + + + + Fballiano_FullCatalogTranslate_Helper + + + + + Fballiano_FullCatalogTranslate_Model + + + + + + Fballiano_FullCatalogTranslate + Mage_Catalog_Model_Resource_Setup + + + + + + + googletranslate - name,short_description,description,meta_title,meta_keyword,meta_description + name,short_description,description,meta_title,meta_keyword,meta_description name,description,meta_title,meta_keyword,meta_description - - - + + + \ No newline at end of file diff --git a/app/code/community/Fballiano/FullCatalogTranslate/etc/system.xml b/app/code/community/Fballiano/FullCatalogTranslate/etc/system.xml index 37c142a..7b58cba 100755 --- a/app/code/community/Fballiano/FullCatalogTranslate/etc/system.xml +++ b/app/code/community/Fballiano/FullCatalogTranslate/etc/system.xml @@ -1,20 +1,20 @@ - - - service - - 1 - 0 - 0 - - - - 1 - 0 - 0 - 10 - + + + service + + 1 + 0 + 0 + + + + 1 + 0 + 0 + 10 + 1 @@ -24,16 +24,16 @@ fballiano_fullcatalogtranslate/translationSystems 40 - - - 1 - 0 - 0 - multiselect - fballiano_fullcatalogtranslate/translatableAttributes - Only these attributes will be translated. + + + 1 + 0 + 0 + multiselect + fballiano_fullcatalogtranslate/translatableAttributes + Only these attributes will be translated. 20 - + 1 @@ -44,23 +44,23 @@ Only these attributes will be translated. 30 - - - - - 1 - 0 - 0 - 20 - - - - 1 - 0 - 0 - - - + + + + + 1 + 0 + 0 + 20 + + + + 1 + 0 + 0 + + + 1 @@ -77,7 +77,7 @@ - - - + + + \ No newline at end of file diff --git a/app/code/community/Fballiano/FullCatalogTranslate/sql/fballiano_fullcatalogtranslate_setup/install-0.1.0.php b/app/code/community/Fballiano/FullCatalogTranslate/sql/fballiano_fullcatalogtranslate_setup/install-0.1.0.php index fcaf9a7..493cc05 100644 --- a/app/code/community/Fballiano/FullCatalogTranslate/sql/fballiano_fullcatalogtranslate_setup/install-0.1.0.php +++ b/app/code/community/Fballiano/FullCatalogTranslate/sql/fballiano_fullcatalogtranslate_setup/install-0.1.0.php @@ -30,7 +30,7 @@ "type" => "int", "default" => "0", "label" => "Translate automatically?", - "note"=> "Should this product be translated automatically when the store admin decides so?", + "note" => "Should this product be translated automatically when the store admin decides so?", "input" => "select", 'source' => 'eav/entity_attribute_source_boolean', "global" => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE, diff --git a/app/code/community/Fballiano/FullCatalogTranslate/sql/fballiano_fullcatalogtranslate_setup/upgrade-0.6.1-0.7.0.php b/app/code/community/Fballiano/FullCatalogTranslate/sql/fballiano_fullcatalogtranslate_setup/upgrade-0.6.1-0.7.0.php index 61ec9f3..c3d0676 100755 --- a/app/code/community/Fballiano/FullCatalogTranslate/sql/fballiano_fullcatalogtranslate_setup/upgrade-0.6.1-0.7.0.php +++ b/app/code/community/Fballiano/FullCatalogTranslate/sql/fballiano_fullcatalogtranslate_setup/upgrade-0.6.1-0.7.0.php @@ -26,26 +26,26 @@ $installer->removeAttribute(Mage_Catalog_Model_Category::ENTITY, "fb_translate"); $installer->addAttribute(Mage_Catalog_Model_Category::ENTITY, "fb_translate", array( - "group" => "General Information", - "type" => "int", - "default" => "0", - "label" => "Translate automatically?", - "note"=> "Should this product be translated automatically when the store admin decides so?", - "input" => "select", - 'source' => 'eav/entity_attribute_source_boolean', - "global" => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE, - "backend" => "", - "frontend" => "", - "is_configurable" => false, - "required" => false, - 'searchable' => false, - 'filterable' => false, - 'comparable' => false, - 'visible_on_front' => false, - 'visible_in_advanced_search' => false, - 'used_in_product_listing' => false, - 'unique' => false, - 'apply_to' => '' + "group" => "General Information", + "type" => "int", + "default" => "0", + "label" => "Translate automatically?", + "note" => "Should this product be translated automatically when the store admin decides so?", + "input" => "select", + 'source' => 'eav/entity_attribute_source_boolean', + "global" => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE, + "backend" => "", + "frontend" => "", + "is_configurable" => false, + "required" => false, + 'searchable' => false, + 'filterable' => false, + 'comparable' => false, + 'visible_on_front' => false, + 'visible_in_advanced_search' => false, + 'used_in_product_listing' => false, + 'unique' => false, + 'apply_to' => '' )); $installer->endSetup(); \ No newline at end of file diff --git a/app/etc/modules/Fballiano_FullCatalogTranslate.xml b/app/etc/modules/Fballiano_FullCatalogTranslate.xml index 4945af5..81bf863 100644 --- a/app/etc/modules/Fballiano_FullCatalogTranslate.xml +++ b/app/etc/modules/Fballiano_FullCatalogTranslate.xml @@ -1,12 +1,12 @@ - - - true - community - - - - - + + + true + community + + + + + \ No newline at end of file diff --git a/shell/fballiano_full_catalog_translate.php b/shell/fballiano_full_catalog_translate.php index b4b7c45..d6fd280 100755 --- a/shell/fballiano_full_catalog_translate.php +++ b/shell/fballiano_full_catalog_translate.php @@ -25,44 +25,44 @@ class Fballiano_FullCatalogTranslate_Shell extends Mage_Shell_Abstract { protected $helper = null; - protected $translation_system = null; + protected $translation_system = null; protected $api_key = null; - protected $command = null; + protected $command = null; protected $store_source = null; protected $store_dest = null; protected $language_source = null; protected $language_dest = null; - protected $ws_url = "https://www.googleapis.com/language/translate/v2"; + protected $ws_url = "https://translation.googleapis.com/language/translate/v2"; protected $attributes_to_translate = null; - protected $category_attributes_to_translate = null; + protected $category_attributes_to_translate = null; protected $datapump = null; - protected $debug_mode = false; - protected $dry_run = false; + protected $debug_mode = false; + protected $dry_run = false; public function run() { - $this->debug_mode = $this->getArg("debug"); - $this->dry_run = $this->getArg("dry"); - if ($this->dry_run) $this->debug_mode = true; + $this->debug_mode = $this->getArg("debug"); + $this->dry_run = $this->getArg("dry"); + if ($this->dry_run) $this->debug_mode = true; $this->helper = Mage::helper("fballiano_fullcatalogtranslate"); - $this->translation_system = $this->helper->getTranslationSystem(); + $this->translation_system = $this->helper->getTranslationSystem(); $this->attributes_to_translate = $this->helper->getAttributesToTranslate(); - $this->category_attributes_to_translate = $this->helper->getCategoryAttributesToTranslate(); - $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"); + $this->category_attributes_to_translate = $this->helper->getCategoryAttributesToTranslate(); + $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"); if (!function_exists("mb_strlen")) die("Please install mbstring PHP extension\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"); - } + 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]; @@ -103,7 +103,7 @@ public function run() $this->language_source = substr(Mage::app()->getLocale()->getLocaleCode(), 0, 2); $this->ws_url .= "&source={$this->language_source}"; - $this->translateCategories($store_id_dest); + $this->translateCategories($store_id_dest); $product = Mage::getModel("catalog/product"); foreach ($products as $product_id) { @@ -111,20 +111,20 @@ public function run() $row = $product->getData(); echo "Translating {$row["sku"]} from {$this->language_source} to {$this->language_dest}... "; - if ($this->debug_mode) echo "\n"; + if ($this->debug_mode) echo "\n"; $translated_row = array(); $translated_row["store"] = (string)$this->store_dest; $translated_row["sku"] = (string)$row["sku"]; $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])) { - $translated_row[$attribute] = $this->translateString($row[$attribute]); + $translated_row[$attribute] = $this->translateString($row[$attribute]); + } + if ($this->debug_mode) { + echo "\t[$attribute] [{$row[$attribute]}] -> [{$translated_row[$attribute]}]\n"; } - if ($this->debug_mode) { - echo "\t[$attribute] [{$row[$attribute]}] -> [{$translated_row[$attribute]}]\n"; - } } - if (!$this->dry_run) $this->datapump->ingest($translated_row); + if (!$this->dry_run) $this->datapump->ingest($translated_row); echo "OK\n"; } @@ -133,88 +133,88 @@ public function run() echo "Terminated\n"; } - public function translateCategories($store_id_dest) - { - $appEmulation = Mage::getSingleton("core/app_emulation"); - $attribute_id = Mage::getModel("catalog/entity_attribute")->loadByCode(Mage_Catalog_Model_Category::ENTITY, "fb_translate")->getId(); - $table_name = Mage::getSingleton("core/resource")->getTableName("catalog_category_entity_int"); - $categories = Mage::getSingleton("core/resource")->getConnection("core_read")->fetchCol("SELECT entity_id FROM {$table_name} WHERE attribute_id={$attribute_id} AND store_id={$store_id_dest} AND value=1"); - - foreach ($categories as $category_id) { - $initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($this->store_source); - $category = Mage::getModel("catalog/category"); - $category->load($category_id); - $row = $category->getData(); - $appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo); - - echo "Translating category {$row["entity_id"]} from {$this->language_source} to {$this->language_dest}... "; - if ($this->debug_mode) echo "\n"; - $translated_row = array(); - $translated_row["fb_translate"] = 0; - foreach ($this->category_attributes_to_translate as $attribute) { - if (strlen($row[$attribute])) { - $translated_row[$attribute] = $this->translateString($row[$attribute]); - } - if ($this->debug_mode) { - echo "\t[$attribute] [{$row[$attribute]}] -> [{$translated_row[$attribute]}]\n"; - } - } - if (!$this->dry_run) { - $initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($this->store_dest); - $category = Mage::getModel("catalog/category"); - $category->load($category_id); - $category->addData($translated_row); - $category->save(); - $appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo); - } - echo "OK\n"; - } - } + public function translateCategories($store_id_dest) + { + $appEmulation = Mage::getSingleton("core/app_emulation"); + $attribute_id = Mage::getModel("catalog/entity_attribute")->loadByCode(Mage_Catalog_Model_Category::ENTITY, "fb_translate")->getId(); + $table_name = Mage::getSingleton("core/resource")->getTableName("catalog_category_entity_int"); + $categories = Mage::getSingleton("core/resource")->getConnection("core_read")->fetchCol("SELECT entity_id FROM {$table_name} WHERE attribute_id={$attribute_id} AND store_id={$store_id_dest} AND value=1"); + + foreach ($categories as $category_id) { + $initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($this->store_source); + $category = Mage::getModel("catalog/category"); + $category->load($category_id); + $row = $category->getData(); + $appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo); + + echo "Translating category {$row["entity_id"]} from {$this->language_source} to {$this->language_dest}... "; + if ($this->debug_mode) echo "\n"; + $translated_row = array(); + $translated_row["fb_translate"] = 0; + foreach ($this->category_attributes_to_translate as $attribute) { + if (strlen($row[$attribute])) { + $translated_row[$attribute] = $this->translateString($row[$attribute]); + } + if ($this->debug_mode) { + echo "\t[$attribute] [{$row[$attribute]}] -> [{$translated_row[$attribute]}]\n"; + } + } + if (!$this->dry_run) { + $initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($this->store_dest); + $category = Mage::getModel("catalog/category"); + $category->load($category_id); + $category->addData($translated_row); + $category->save(); + $appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo); + } + echo "OK\n"; + } + } public function productCollectionWalkCallback($args) { $row = $args["row"]; echo "Translating {$row["sku"]} from {$this->language_source} to {$this->language_dest}... "; - if ($this->debug_mode) echo "\n"; + if ($this->debug_mode) echo "\n"; $translated_row = array(); $translated_row["store"] = $this->store_dest; $translated_row["sku"] = $row["sku"]; $translated_row["fb_translate"] = 0; foreach ($this->attributes_to_translate as $attribute) { if (strlen($row[$attribute])) { - if ($this->translation_system == "googletranslate" and mb_strlen($translated_row[$attribute]) >= 5000 ) { + if ($this->translation_system == "googletranslate" and mb_strlen($translated_row[$attribute]) >= 5000) { echo "\t[$attribute] more than 5000 chars long, unsupported by Google Translate, not translated\n"; continue; } - $translated_row[$attribute] = $this->translateString($row[$attribute]); + $translated_row[$attribute] = $this->translateString($row[$attribute]); if ($this->debug_mode) { echo "\t[$attribute] [{$row[$attribute]}] -> [{$this->language_dest} {$translated_row[$attribute]}]\n"; } } } - if (!$this->dry_run) $this->datapump->ingest($translated_row); + if (!$this->dry_run) $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); - if ($this->debug_mode) echo "\t{$ws_url}\n"; - $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 - ); - if ($this->debug_mode) echo "\t{$command}\n"; - return shell_exec($command); - } - } + public function translateString($string) + { + switch ($this->translation_system) { + case "googletranslate": + $ws_url = "{$this->ws_url}&q=" . urlencode($string); + if ($this->debug_mode) echo "\t{$ws_url}\n"; + $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 + ); + if ($this->debug_mode) echo "\t{$command}\n"; + return shell_exec($command); + } + } public function usageHelp() {