Magento: Translate using Google API

The meta data of my products were in English and the keywords on my website were not translated in French : I have decided to perform the translation in French using the Google Translate API. The Google Translate API is a paid service but the price is not expensive (20$ for 1000000 characters). I paid 1,50$ for 77562 bytes.

The Google API is available here:
https://console.developers.google.com/

You will find here how to proceed.

  1. Register your Google Project on the Google Developers Console
  2. Create your API key
  3. Download the PHP Google API
  4. Use my PHP script (must be modified).

You will find below an extract of my PHP script :

I will send the complete script to those who will donate (only 5€)

<?php

require_once 'Google/autoload.php';
error_reporting(1);

$proxy = new SoapClient('https://......../api/v2_soap/?wsdl=1', array('trace' => 1, "user_agent" => "PHP Soap Client"));
$proxy->startSession();
$sessionId = $proxy->login(array('username' => 'your_user','apiKey' => 'apiKey'));

....

// Search Filter
$complexFilter = array(
		'complex_filter' => array(
				array(
						'key' => 'type',
						'value' => array(
								'key' => 'in',
								'value' => 'simple')
				)
		)
);

$ids=array();

// Product List
$result = $proxy->catalogProductList((object)array('sessionId' => $sessionId->result, 'filters' => $complexFilter));

...

function getProductMeta(&$myproxy,&$mysessionid,$mystoreid,&$myproductid)
{
	$attributes = new stdclass();
	$attributes->attributes = array('meta_keyword','meta_title','meta_description');
	return $myproxy->catalogProductInfo((object)array('sessionId' => $mysessionid->result, 'productId' => &$myproductid, identifierType => 'id', 'attributes' => $attributes));
}

function GoogleTranslate($string)
{
	//echo "translate string=$stringn";
	$client = new Google_Client();
	$client->setApplicationName("google-project-name");
	$client->setDeveloperKey("api_key");
	$translateService = new Google_Service_Translate($client);
	$response=$translateService->translations->listTranslations($string, 'fr', array('source' => 'en'));;
	$texte=$response['data']['translations'][0]['translatedText'];
	return $texte;
}

$ids=ParseProductsList($proxy,$sessionId,$result,$mystore, $ids);
foreach ($ids as $id)
{
	$meta=getProductMeta($proxy, $sessionId, $mystore, $id);
	$meta_title=$meta->result->meta_title;
	$meta_description=$meta->result->meta_description;
	$meta_keyword=$meta->result->meta_keyword;
	// Translate
	// Title
	$meta_title_fr=GoogleTranslate($meta_title);
	$meta_description_fr=GoogleTranslate($meta_description);
	$meta_keyword_fr=GoogleTranslate(str_replace(',',' ',$meta_keyword));
	// Update store
	$result=updateDataStoreView($proxy, $sessionId,$id,array('meta_title' => $meta_title_fr,'meta_description' => $meta_description_fr,'meta_keyword' => str_replace(' ',',',}

// End Session
$proxy->endSession(array('sessionId' => $sessionId->result));

 

Et voila,

Nicolas Portais
Author Photographer
http://www.mystockphoto.fr/
http://photos-art.pro/

.

 

Ce contenu a été publié dans Anglais, Magento, avec comme mot(s)-clé(s) , , , , , , , , . Vous pouvez le mettre en favoris avec ce permalien.

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Captcha (solve the arithmetic equation) * Time limit is exhausted. Please reload CAPTCHA.