How to preserve newlines with "to_lang" Google Translate
I use to_lang
to translate text in Rails 3 application.
I noticed that it converts newlines to spaces.
What is the most appro开发者_如何学Pythonpriate approach to preserve the newlines in the original text?
I don't want to split the original text by "\n"
, translate the parts, and then combine them again, since it will increase the number of requests.
Any ideas?
I've tested it with :debug => :request
:
{:key=>"--", :q=>"To prevent abuse, Google places limits on API requests.\nUsing a valid OAuth 2.0 token or API key allows you to exceed \nanonymous limits by connecting requests back to your project.", :target=>"ru"}
and as you can see it's not the gem fault. So, to_lang
can translate arrays too. And if you'll write something like ["one", "two", "three"].to_russian
it will be one request to the Google API.
UPDATE:
irb(main):001:0> str = "test1\ntest2\ntest3"
=> "test1\ntest2\ntest3"
irb(main):002:0> arr = str.split("\n")
=> ["test1", "test2", "test3"]
irb(main):003:0> str = arr.join("\n")
=> "test1\ntest2\ntest3"
精彩评论