5000 Characters Length Limit Of Google Ajax Language API

Google Ajax Language API allow translating and detecting the language of blocks of text within a webpage using only JavaScript. However, it has a characters length limit of 5000 for input text (see it’ terms). Although you should even can’t translate a text longer than about 1000 characters if you don’t use a Javascript Proxy to solve Cross Domain problems and transfer POST request, because the limit of URL length.

I have written a simple Javascript Proxy in PHP, it only accepts POST method. (Most javascript APIs allow using jsonp in GET method.)

<?php
// This very Simple PHP Proxy is degigned to solve javascript browser cross domain (same origin policy) problems

if ( 'POST' != $_SERVER['REQUEST_METHOD'] ) {
	header('Allow: POST');
	header('HTTP/1.1 405 Method Not Allowed');
	die();
}

function fail($error) {
	header('HTTP/1.1 500 Internal Server Error');
	die($error);
}

if( !isset($_SERVER['HTTP_HOST']) )
	fail("Forbidden");

$allowed_domains = Array($_SERVER['HTTP_HOST']);
if( 
	!isset($_SERVER["HTTP_REFERER"]) ||
	!preg_match('#^https?://(' . implode("|", array_map('preg_quote', $allowed_domains)) . ')#i', $_SERVER["HTTP_REFERER"]) 
)
	fail("Referrer Empty or Not Allowed.");

if( !isset($_GET['url'])  )
	fail("Forbidden");

$session = curl_init($_GET['url']);

$postvars = '';
$deli = '';
while ($element = current($_POST)) {
	$postvars .= $deli . urlencode(key($_POST)).'='.urlencode($element);
	next($_POST);
	$deli = '&';
}

curl_setopt ($session, CURLOPT_POST, true);
curl_setopt ($session, CURLOPT_POSTFIELDS, $postvars);


curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_REFERER, $_SERVER["HTTP_REFERER"]);
curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);


$result = curl_exec($session);


$headers = array(
	'Expires' => 'Wed, 11 Jan 1984 05:00:00 GMT',
	'Last-Modified' => gmdate( 'D, d M Y H:i:s' ) . ' GMT',
	'Cache-Control' => 'no-cache, must-revalidate, max-age=0',
	'Pragma' => 'no-cache',
	'Content-Type' => 'text/javascript; charset=UTF-8',
	'Content-Length' => strlen($result)
);
foreach( $headers as $name => $field_value )
	header("{$name}: {$field_value}");

echo $result;
curl_close($session);

Usage:
Just send your POST request to yourproxy.php?url=POSTURL instead of directly to POSTURL.

I used this proxy to develop a feature that translate the whole webpage into another language using Google Ajax Language API. Sadly I found the Google’s Limit that if you POST a text longer then 5000 characters, Google Server will return a error “exceed string length limit”. I am exploring other open translation APIs, such as Microsoft Translator AJAX API. However, Microsoft’s Docs seems to be more complex ( and ugly = = ) than Google’s.

BTW: why google and other web APIs which can be accessed via javascript don’t support Cross-Origin Resource Sharing? It seems currently only Twitter and YQL support this standand.

0 Responses to “5000 Characters Length Limit Of Google Ajax Language API”


Comments are currently closed.