how to preserve newlines?

1,038 views
Skip to first unread message

egasimus

unread,
Sep 9, 2010, 4:17:59 AM9/9/10
to Google AJAX APIs
Hi everyone. As a part of some automation stuff I'm working on in
order to make my life easier, I'm trying to translate a block of text
using, well, a lot of stuff. AutoIt3, Firefox, MozRepl, JavaScript,
and, ultimately, Google Translate API. What it all boils down to is a
page containing a single textarea and button, which POSTs my source
text to, for example, http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&langpair=en%7Cru.

So the button gets clicked, the text get sent, and the browser goes to
that URL and shows the text representation of a responseData object.
This text gets read through JS, stripped, and made use of. However, in
the translatedText field, all the newlines from the source text are
gone without a trace, and this bothers me greatly, as I'd rather keep
them. Any ideas how to preserve newlines when using the API?

Jeremy Geerdes

unread,
Sep 9, 2010, 7:30:16 AM9/9/10
to google-ajax...@googlegroups.com
The failure to preserve newlines is a result of the API assuming it's dealing with HTML, which does not require newlines. If you want to preserve them, you can specify format=text, but that will result in the API attempting to translate any HTML markup that you have in the string. Alternatively, you could replace any newlines with some HTML tag that you will then replace with newlines after the translation is returned.

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

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

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

> --
> You received this message because you are subscribed to the Google Groups "Google AJAX APIs" group.
> To post to this group, send email to google-ajax...@googlegroups.com.
> To unsubscribe from this group, send email to google-ajax-searc...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/google-ajax-search-api?hl=en.
>

egasimus

unread,
Sep 17, 2010, 3:11:52 AM9/17/10
to Google AJAX APIs
That'll do. HTML markup is rarely, if ever, encountered in the texts
I'm translating. Thanks a lot (:

On Sep 9, 2:30 pm, Jeremy Geerdes <jrgeer...@gmail.com> wrote:
> The failure to preserve newlines is a result of the API assuming it's dealing with HTML, which does not require newlines. If you want to preserve them, you can specify format=text, but that will result in the API attempting to translate any HTML markup that you have in the string. Alternatively, you could replace any newlines with some HTML tag that you will then replace with newlines after the translation is returned.
>
> Jeremy R. Geerdes
> Effective website design & development
> Des Moines, IA
>
> For more information or a project quote:http://jgeerdes.home.mchsi.com
> jrgeer...@gmail.com
Reply all
Reply to author
Forward
0 new messages