You can avoid the above challenges by simply using the Translate element, which does exactly what you're looking for right out of the box. Take a look: http://www.google.com/webelements/translate
Just specify the language of your website, and then users with their browser set to a different language will automatically see a banner at the top asking if they want to translate. Moreover, you can include the dropdown list so that people can choose to translate even if their browser language matches your site (say, they're at an Internet cafe in a non-native country). I hope this helps! Adam On Jul 28, 4:17 am, Jeremy Geerdes <[email protected]> wrote: > This can absolutely be done. There are three challenges you will face, though: > > 1. Reliably detecting the user's language. The most reliable method is to > check the browser's Accept-Language header, but that isn't necessarily > accessible from Javascript. A simple PHP or other script which would detect > this and write the appropriate destination language code would be most > effective. > > 2. Chunking the contents of the page. Since the API has a length limit on > strings it will translate, you will need to chunk the contents in some way. I > would recommend walking through the DOM and checking whether an elements' > innerHTML length is less than 1200 or so when uri encoded. If you want to go > for fewer requests, you can set up a server-side proxy and send your > translation queries through the POST method via an XMLHttpRequest object.. If > you do this, you need to make sure the uri-encoded innerHTML is less than > 5,000 characters long. That part is simple enough. The challenge you will hit > is when you run into a single text node that is too long. You'll need to > figure out how to split the string on a clause so that it will still > translate reasonably well. I would suggest a regular expression such as the > one below: > > encodeURIComponent(el.innerHTML).match(/(.{1,1200}[\.\,\n])/g); > > This will look for clauses which, when uri-encoded, are up to 1200 characters > long and end with either a comma (,), period (.), or newline character. > > 3. HTML entities. The API will return special characters as HTML entities, > which will need to be translated into plain text in the case of text nodes. > If you search the web, there are a handful of scripts out there that you can > get the necessary matrices and regexps from. > > A fourth, lesser challenge will be keeping the responses straight. To do > this, you need to use a method closure, which is essentially a function which > returns a function. The challenge will be figuring out if you want to replace > all of the innerHTML of an element with the response, or part of it (if the > contents were split). > > All of these challenges are doable. In fact, I wrote a script that does this > very thing last week. It's for a client, though, so I can't just share it. > And by the way, dealing with the third challenge took up some 250 lines of > code, while all the rest of the script was less than 200. > > Jeremy R. Geerdes > Effective website design & development > Des Moines, IA > > For more information or a project quote:http://jgeerdes.home.mchsi.com > [email protected] > > If you're in the Des Moines, IA, area, check out Debra Heights Wesleyan > Church! > > On Jul 28, 2010, at 1:13 AM, [email protected] wrote: > > > > > I want users of my site to be able to view contents in different > > languages. So I want to achieve that by creating a HTML drop down menu > > that will allow them to select language of their choice and the system > > detects the original language and then translate to the language > > selected. > > e.g English - French, french - Spanish, Spanish - English etc. > > > -- > > 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 > > athttp://groups.google.com/group/google-ajax-search-api?hl=en. -- 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.
