Google Language Ajax API-translate whole web page in-line without 500 words limit

19 views
Skip to first unread message

tcs 241

unread,
Nov 21, 2008, 1:05:00 PM11/21/08
to Google-AJAX...@googlegroups.com


Does anybody know how to translate whole web page in-line without 500
words limit?

I'd like translate my website in different languages with Ajax API,
because Django i18n cannot be utilized yet.

Any comments?

Thanks,
Chef

Free Immigration Guide
visachoice.appspot.com

Jeremy Geerdes

unread,
Nov 21, 2008, 2:55:14 PM11/21/08
to Google-AJAX...@googlegroups.com
Well, first of all, the limit has been increased from 500 characters
to 5,000 characters. Second, you will need to use what's called a
Javascript closure, which is essentially a function that returns a
function (search for it on Google). You can use this method to give
your callback a snapshot of what it's supposed to be replacing. Then
you can make multiple, parallel calls to the translation API to do the
translation.

Jeremy R. Geerdes
Effective website design & development
Des Moines, IA

For more information or a project quote:
http://jgeerdes.home.mchsi.com
jgee...@mchsi.com

Unless otherwise noted, any price quotes contained within this
communication are given in US dollars.

If you're in the Des Moines, IA, area, check out Debra Heights
Wesleyan Church!

Harpreet Bassi

unread,
Nov 21, 2008, 4:07:09 PM11/21/08
to Google-AJAX...@googlegroups.com
Jeremy,
 
If you can elaborate it that will be great.
 
I will really be thankful to you.
 
Hary

Dan

unread,
Nov 21, 2008, 6:08:49 PM11/21/08
to Google AJAX APIs
I've found the best way is to break it down into elements, then send
the page elements to the google server. You can do them
asynchronously, so the page is actually loaded pretty quickly. I also
cache translated elements so they don't have to be re-translated and
so I can fix some bad translations.

here is one of my clients who is using it: http://bravekids.org

-Dan
i...@morrisdev.com



On Nov 21, 1:07 pm, "Harpreet Bassi" <harpreet.bass...@gmail.com>
wrote:
> Jeremy,
>
> If you can elaborate it that will be great.
>
> I will really be thankful to you.
>
> Hary
>
> On Fri, Nov 21, 2008 at 11:55 AM, Jeremy Geerdes <jrgeer...@gmail.com>wrote:
>
>
>
> > Well, first of all, the limit has been increased from 500 characters
> > to 5,000 characters.  Second, you will need to use what's called a
> > Javascript closure, which is essentially a function that returns a
> > function (search for it on Google).  You can use this method to give
> > your callback a snapshot of what it's supposed to be replacing.  Then
> > you can make multiple, parallel calls to the translation API to do the
> > translation.
>
> > Jeremy R. Geerdes
> > Effective website design & development
> > Des Moines, IA
>
> > For more information or a project quote:
> >http://jgeerdes.home.mchsi.com
> > jgeer...@mchsi.com

Harpreet Bassi

unread,
Nov 21, 2008, 6:24:46 PM11/21/08
to Google-AJAX...@googlegroups.com
Dan,
By page elements you mean sending contents from the web page along with html tags to the google server.
 
for e.g
As from web page i get
<html>
<body>
blah blah
</body>
<p>
blah blah
</p>
</html>
 
will i be sending it as it is to the google server?
 
Harry

tcs 241

unread,
Nov 22, 2008, 2:39:11 AM11/22/08
to Google AJAX APIs

I revised google sample code according to code from (
http://www.labnol.org/internet/website-translation-with-google-ajax-language-ap/4367/
).

but it only works when paragraph is short. Once text length is
increased by twice, there is no translation.

some suggestion?

Thanks a lot!

Chef




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/
>
<title>Google AJAX Language API - Hello World</title>
<script type="text/javascript" src="http://www.google.com/jsapi"></
script>
<script type="text/javascript">

google.load("language", "1");

function initialize() {
var text = document.getElementById("text").innerHTML;
var len = text.length;


var words = 50;
var text_start=-1*words;

for(i=0; i<=(len/words); i++) {

google.language.detect(text, function(result) {

if (!result.error && result.language) {

text_start=text_start+words;
google.language.translate(text.substr(text_start,words),
result.language, "es",
function(result) {
var translated = document.getElementById("translation");
if (result.translation) {
translated.innerHTML = translated.innerHTML+result.translation;
}
});

}


});

}
}
google.setOnLoadCallback(initialize);

</script>
</head>
<body>
<div id="text">

<p> WASHINGTON, Nov 14 (Reuters) - Sales at U.S. retailers
suffered a record decline in October as fears of recession sapped
spending, but part of the drop was due to slumping gasoline prices,
which helped buoy consumer confidence this month.

The Commerce Department said on Friday that retail sales slumped 2.8
percent in October to a seasonally adjusted $363.7 billion, the
largest decline since the department's current methodology was adopted
in 1992, as mounting unemployment hit shoppers' appetites.

WASHINGTON, Nov 14 (Reuters) - Sales at U.S. retailers suffered a
record decline in October as fears of recession sapped spending, but
part of the drop was due to slumping gasoline prices, which helped
buoy consumer confidence this month.

The Commerce Department said on Friday that retail sales slumped 2.8
percent in October to a seasonally adjusted $363.7 billion, the
largest decline since the department's current methodology was adopted
in 1992, as mounting unemployment hit shoppers' appetites.

A separate Reuters/University of Michigan November survey of consumers
showed that confidence unexpectedly rebounded from a record October
drop as tumbling gas prices offset worries about the economy.


</p>


</div>
<div id="translation"></div>
</body>
</html>


On Nov 21, 3:24 pm, "Harpreet Bassi" <harpreet.bass...@gmail.com>
wrote:
> > > > > visachoice.appspot.com- Hide quoted text -
>
> - Show quoted text -

Balazs Endresz

unread,
Nov 22, 2008, 6:23:11 AM11/22/08
to Google AJAX APIs
Hi all! If you want an easy to use and flexible solution there's a
plugin for jQuery for this:
http://code.google.com/p/jquery-translate/
http://jquery.com/

All you have to do is to include these above, then translating the
whole page takes only one line:
$('body').translate('es'); //You can use any css selector instead of
body

You can try it live with some other fancy features enabled:
http://code.google.com/p/jquery-translate/wiki/Extensions

If you don't want to use a js framework just for this maybe at least
you can reuse some parts of it. But without jQuery you wouldn't be
able to translate arrays, store the translation bound to html elements
and restore them without page refresh or translate from the original
text to multiple languages.

Jeremy Geerdes

unread,
Nov 22, 2008, 8:25:44 AM11/22/08
to Google-AJAX...@googlegroups.com
As I noted previously, there is a CHARACTER limit on the length of
your string to be translated. Specifically, it can be no longer than
5,000 characters. So it's not enough to simply parse by <p> or other
HTML element; you have to check the character count, too. For that
reason, I would recommend translating in chunks based on html element,
then length, and then sentences or clauses In other words, break the
original string into chunks based on elements. Then check those
strings for length. And if they are too long, break them into
individual sentences or even clauses.

Jeremy R. Geerdes
Effective website design & development
Des Moines, IA

For more information or a project quote:
http://jgeerdes.home.mchsi.com
jgee...@mchsi.com

Unless otherwise noted, any price quotes contained within this
communication are given in US dollars.

If you're in the Des Moines, IA, area, check out Debra Heights
Wesleyan Church!




Ben Lisbakken

unread,
Nov 24, 2008, 12:35:30 PM11/24/08
to Google-AJAX...@googlegroups.com

It is worth noting that some browsers GET request urls are limited to under 2,000 characters.  The Javascript API is affected by this, and the text sent in the request will be part of the URL.

-Ben

On Nov 22, 2008 5:26 AM, "Jeremy Geerdes" <jrge...@gmail.com> wrote:


As I noted previously, there is a CHARACTER limit on the length of
your string to be translated. Specifically, it can be no longer than
5,000 characters.  So it's not enough to simply parse by <p> or other
HTML element; you have to check the character count, too.  For that
reason, I would recommend translating in chunks based on html element,
then length, and then sentences or clauses In other words, break the
original string into chunks based on elements.  Then check those
strings for length.  And if they are too long, break them into
individual sentences or even clauses.

Jeremy R. Geerdes Effective website design & development Des Moines, IA For more information or a ...

jgee...@mchsi.com

Unless otherwise noted, any price quotes contained within this communication are given in US doll...

On Nov 22, 2008, at 1:39 AM, tcs 241 wrote: > > > I revised google sample code according to code fr...

Balazs Endresz

unread,
Nov 24, 2008, 1:03:12 PM11/24/08
to Google AJAX APIs
Aha! Then I think you can close this issue:
http://code.google.com/p/google-ajax-apis/issues/detail?id=150

And maybe you could have a look at this as well, please:
http://code.google.com/p/google-ajax-apis/issues/detail?id=149

On Nov 24, 6:35 pm, Ben Lisbakken <lisba...@google.com> wrote:
> It is worth noting that some browsers GET request urls are limited to under
> 2,000 characters.  The Javascript API is affected by this, and the text sent
> in the request will be part of the URL.
>
> -Ben
>
> On Nov 22, 2008 5:26 AM, "Jeremy Geerdes" <jrgeer...@gmail.com> wrote:
>
> As I noted previously, there is a CHARACTER limit on the length of
> your string to be translated. Specifically, it can be no longer than
> 5,000 characters.  So it's not enough to simply parse by <p> or other
> HTML element; you have to check the character count, too.  For that
> reason, I would recommend translating in chunks based on html element,
> then length, and then sentences or clauses In other words, break the
> original string into chunks based on elements.  Then check those
> strings for length.  And if they are too long, break them into
> individual sentences or even clauses.
>
> Jeremy R. Geerdes Effective website design & development Des Moines, IA For
> more information or a ...
> jgeer...@mchsi.com

tcs 241

unread,
Nov 24, 2008, 2:14:05 PM11/24/08
to Google AJAX APIs
In my code, I only send 50 characters each time. I thought I should
can translate text with length of 50*n characters. So, there is no
limit for the length of text. Is that true?

Thanks,
Chef
Free Immigration Guide
http://visachoice.appspot.com
> > code according to code fr...- Hide quoted text -

tcs 241

unread,
Dec 4, 2008, 12:54:45 AM12/4/08
to Google AJAX APIs

Hi,

I tried Balazs' code.It works!Although i still don't know what is
wrong with my code.

This plug-in has these advantages:
1.Less coding. (see the sample code)
2.no limit for text length.
3.To be translated text is extracted from HTML and concatenated.And,
less API requests are needed.

Looks like Balazs has done substantial work to solve this issue.

here is a minimalism sample:

( You can download sample code from
http://code.google.com/p/djangopollsgae/downloads/list )

<html>
<head>

<p> Please download

jquery[1].translate-1.2.4.min.js from
http://code.google.com/p/jquery-translate/

jquery-1[1].2.6.min.js from
http://jquery.com/ .

Maybe the *.js files have been updated. Please change *.js
file names accordinglly.
</p>
<script type="text/javascript" src="jquery-1[1].2.6.min.js"></
script>

<script type="text/javascript" src="jquery
[1].translate-1.2.4.min.js"></script>


<script type="text/javascript">
function startTranslation(){
$('p').translate( 'en',
'es')
}
</script>
</head>
<body>

<p id="textToTranslate">WASHINGTON, Nov 14 (Reuters) - Sales
at U.S. retailers suffered a record decline in October as fears of
recession sapped spending, but part of the drop was due to slumping
gasoline prices, which helped buoy consumer confidence this month.

The Commerce Department said on Friday that retail sales slumped 2.8
percent in October to a seasonally adjusted $363.7 billion, the
largest decline since the department's current methodology was adopted
in 1992, as mounting unemployment hit shoppers' appetites.



</p>


<input type="button" onclick="startTranslation();"
value="Translate "/>
</body>
</html>


Thanks,
Chef
Free Immigration Guide
http://visachoice.appspot.com


On Nov 24, 11:14 am, tcs 241 <Mr.C...@gmail.com> wrote:
> In my code, I only send 50 characters each time. I thought I should
> can translate text with length of 50*n characters. So, there is no
> limit for the length of text. Is that true?
>
> Thanks,
> Chef
> Free Immigration Guidehttp://visachoice.appspot.com
> > - Show quoted text -- Hide quoted text -

Balazs Endresz

unread,
Dec 4, 2008, 5:07:42 AM12/4/08
to Google AJAX APIs
Thanks, I'm glad you like it!
Though it's not quite clear if there's some problem with your code or
isn't, you said "It works! Although i still don't know what is wrong
with my code". Apart from putting a paragraph in the head and not
having a doctype there's nothing wrong with it, or am I missing
something? :)
Reply all
Reply to author
Forward
0 new messages