Xonox

Using Google Translate API for automatic translation

1 post in this topic

I've been using PW for quite a while, now, so I would like to give back to the community that helped me so many times.

I had this customer that wanted to use the Google Translate API to automatically translate the site, so I built this function that takes advantage of it without the need to use the API on every page cycle. I put it on a file that gets called on the _init.php, so that I can use it on every template. Here it is:

<?php

	function googleAutoTranslate($page, $field) {

		// Turn off outputFormatting
		$page->of(false);

		// Get current language and field content for this language
		$current_language = wire('user')->language->name;
		$field_content = $page->getLanguageValue($current_language, $field);

		// Is there any content for this language?
		if($field_content != '') {

			// Do nothing!

		}
		else {

			// No content, lets translate...

			// Get default language text
			$text = $page->getLanguageValue('default', $field);

			// Translate only if there's content in the default language
			if($text != '') {

				// Translate it
				$apiKey = 'YOUR API KEY HERE';
				$default_language = 'pt'; // Your default language here!
				$url = 'https://www.googleapis.com/language/translate/v2?key=' . $apiKey .'&q=' . rawurlencode($text) . '&source=' . $default_language . '&target=' . $current_language;
				$json = json_decode(file_get_contents($url));

				$field_content = $json->data->translations[0]->translatedText;

				// Save translated text
				$page->$field->setLanguageValue($current_language, $field_content);
				$page->save();

			}

		}

		// Turn on outputFormatting
		$page->of(true);

		// Return result
		return $field_content;

	}

?>

Whenever you use a field on a template that should be auto translated, just call the function with a page object and the field name, like so:

echo googleAutoTranslate($page, 'body');

Features:

  • Translation occurs only once per field, so you don't need to keep paying translations (it stores the translation into the field language);
  • You can correct the translation in the admin area and it won't be overwritten;
  • If you need the translation to be made again, just delete the field content in the needed language.
  • For the translation to occur, content must exist in the default language.

I had to fight a little to get this working, so I hope this helps anyone, who comes across this particular need.

Nice Things To Have

If someone wants to give it a shot to make this into a module, please do. It would be nice to have a checkbox "Enable Google auto translate for this field", when you edit a field input features.

Don't Spend Too Much

Mind you that the Google translate is a payed service! ;) and needs a Credit Card to get it going (even with $300 free credit);

With a relatively small site (and the tests made to get this to work) I already spent about 80.000 translated characters = $3, :P

Hope this helps someone!

 

8 people like this

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.