FYI, the Translation API now accepts 5,000 characters instead of 500 per
request.
-Ben

On Wed, Sep 3, 2008 at 10:03 AM, Balazs Endresz <[email protected]>wrote:

>
> Hi! I made one too: http://code.google.com/p/jquery-translate/ :)
> There's a weird regex in it that splits the text, maybe it could be
> simpler but works pretty well.
> You'll also need jQuery for it www.jquery.com but that would make your
> life much easier anyway.
>
>
> On szept. 3, 14:51, Norgepaul <[email protected]> wrote:
> > Hi,
> >
> > I've written a bit of JavaScript that partly overcomes the 500
> > character limit. It's not 100% perfect as it only has some simple tag
> > detection functionality. I'm pretty new to JavaScript and I'm sure
> > that there are a lot of people out there that can improve on the code.
> > At least it's a start :o)
> >
> > To use the code, simply stick it on your page and alter the
> > "translatecontents" function to suit your needs. You can also add code
> > to "beginTranslations" and "endTranslations" to enable/disable page
> > elements during the translation process.
> >
> > Here you go:
> >
> > =================================
> >
> > <script type="text/javascript" src="http://www.google.com/jsapi";></
> > script>
> > <script type="text/javascript">google.load("language", "1");</script>
> > <script type="text/javascript">
> >
> > var preTransValues = [];
> >
> > function inTag(chunk)
> > {
> >         var openPos = chunk.lastIndexOf('<');
> >         var closePos = chunk.lastIndexOf('>');
> >
> >         // Return true if an open tag appears before a close tag
> >         return openPos > closePos;
> >
> > }
> >
> > function beginTranslations()
> > {
> >    // Add code that should be executed when the translating starts
> >
> > }
> >
> > function endTranslations()
> > {
> >    // Add code that should be executed when the translating finished
> >
> > }
> >
> > function translateElement(tElement, translateText, fromLang, toLang,
> > minChunkSize, maxChunkSize, splitChars, lastElement)
> > {
> >         var translatedText = "";
> >         var chunkStart = 0;
> >         var chunkEnd = minChunkSize;
> >         var count = 0;
> >         var totalCount = 0;
> >         var splitChar;
> >         var tempChunk;
> >
> >         // Stop when we reach the end of the text
> >         while (chunkStart <= translateText.length)
> >         {
> >                 totalCount++;
> >
> >                 // Scan from the current position the to maximum chunk
> size to try
> >                 // and find the best point to split the text. We do this
> to try to
> > split
> >                 // between words and avoid splitting inside tags. Needs
> some work to
> > be
> >                 // completely accurate. If we reach the end of the
> maximum chunk
> > size before
> >                 // we've found a suitable position we have to split
> anyway and hope
> > for the
> >                 // best.
> >             while ((chunkEnd <= translateText.length) &&
> >                    (chunkStart + chunkEnd <= maxChunkSize))
> >             {
> >                 splitChar = translateText.substring(chunkEnd, chunkEnd +
> 1);
> >
> >                 // We've found a nice position if the current character
> matches
> > one of the
> >                 // split characters and we are not inside a tag.
> >                 if  (((!inTag(translateText.substring(chunkStart,
> chunkEnd +
> > 2))) &&
> >                      (splitChars.indexOf(splitChar))))
> >                         break;
> >                 else
> >                         chunkEnd++;
> >             }
> >
> >             // If we split inside a tag try to find the start of the tag
> >             if (inTag(translateText.substring(chunkStart, chunkEnd + 1)))
> >             {
> >                 tempChunk = translateText.substring(chunkStart, chunkEnd
> + 1)
> >                 chunkEnd = tempChunk.lastIndexOf('<') + chunkStart;
> >             }
> >
> >             // Extract the chunk of text to be translated
> >             chunk = translateText.substring(chunkStart, chunkEnd);
> >
> >             // Translate the text
> >             google.language.translate(chunk, fromLang, toLang,
> > function(result)
> >                 {
> >                                 if (!result.error)
> >                                 {
> >                                         // Add the translated text chunk
> to the completed translation
> >                                     translatedText = translatedText +
> result.translation;
> >                                     count ++;
> >
> >                                     // We only want to set the translated
> text if this is it's
> > last translation chunk
> >                                     if (count >= totalCount)
> >                                     {
> >                                         tElement.innerHTML =
> translatedText;
> >
> >                                         // If this is the last
> translation, call the end translations
> > function
> >                                         if (lastElement)
> >                                                 endTranslations();
> >                                     }
> >                                 }
> >                                 else
> >                                 {
> >                                         if (lastElement)
> >                                         endTranslations();
> >
> >                                         return result.error.message;
> >                                 }
> >                 });
> >
> >                 chunkStart = chunkEnd;
> >                 chunkEnd = chunkEnd + minChunkSize;
> >         }
> >
> > }
> >
> > function translateElements(elementIdArray, fromLang, toLang,
> > minChunkSize, maxChunkSize, splitChars)
> > {
> >         if (splitChars = "")
> >                  splitChars = "(),!.";
> >
> >         if (toLang != "")
> >                 beginTranslations();
> >
> >         for (i = 0; i < elementIdArray.length; i++)
> >         {
> >                 // Save the original text if this is the first time we
> are
> > translating it
> >                 if (preTransValues[elementIdArray[i]] == undefined)
> >                         preTransValues[elementIdArray[i]] =
> > document.getElementById(elementIdArray[i]).innerHTML;
> >
> >                 if (toLang == "")
> >
> document.getElementById(elementIdArray[i]).innerHTML =
> > preTransValues[elementIdArray[i]];
> >                 else
> >
> translateElement(document.getElementById(elementIdArray[i]),
> > preTransValues[elementIdArray[i]], fromLang, toLang, minChunkSize,
> > maxChunkSize, splitChars, i == elementIdArray.length - 1);
> >         }
> >
> > }
> >
> > function translatecontents(fromLang, toLang)
> > {
> >         translateElements(["businessTypeString", "businessDiscountText",
> > "businessDescription"], // An array of the IDs of the elements you
> > want to translate
> >                                       fromLang, // The "from" language
> e.g. en <an empty string
> > will automatically detect the from language>
> >                                       toLang,   // The "to" language e.g.
> no
> >                                       300,      // The minimum length of
> a block that will be
> > translated
> >                                       500,      // The maximum length of
> a block that will be
> > translated (500 is the current Google max)
> >                                       "");      // The characters that
> will be used to split
> > words. Default = "(),!."}
> >
> > </script>
> >
> > On Jul 30, 11:51 pm, Fitzchev <[email protected]> wrote:
> >
> > > To expand the max chars vote herehttp://
> code.google.com/p/google-ajax-apis/issues/detail?id=18
> >
> > > On Jul 30, 11:07 pm, Denis1900 <[email protected]> wrote:
> >
> > > > I'm connecting from C# by "GET" request to Google AJAX Language
> > > > Service. I use JavaScript encodeURIComponent for converting input
> > > > string, which is the value of q parameter.
> > > > But iflengthof my text is bigger than 400 symbols, i get error 400 -
> > > > Bad Request. What is themaximumlengthof the value of q paraneter? I
> > > > need totranslatelarge amount of text, so it very slow to make a new
> > > > query for each 400 symbols..
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google AJAX APIs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/Google-AJAX-Search-API?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to