On Wed, May 15, 2013 at 6:30 AM, UA <[email protected]> wrote:
> I have a Rails app. One of my clients is importing French Text which
> is appearing weirdly. Check below example:
>
>     1. str = "--- \nFrench: \"3. Combien de r\\xC3\\xA9gions y a-t-il
> au Cameroon?\"\nEnglish: 3. How many regions are there in Cameroon?\n"
>
> Can someone assist please?

Wow, this took a while to suss out. I really hate character encodings
and translations, but here we are.

So, the problem basically lies in the fact that the encoded character
is doubly escaped:

irb(main):159:0> '\xC3\xA9'
=> "\\xC3\\xA9"

whereas the other characters are escaped just once:

irb(main):160:0> "\n"
=> "\n"
irb(main):161:0> "\""
=> "\""

what I came up with seems sort of kludgy:

1. Double escape the singly-escaped characters:

irb(main):166:0> new_str = str.gsub(/\"/,'\\"').gsub(/\n/,'\\n')
=> "--- \\nFrench: \\\"3. Combien de r\\xC3\\xA9gions y a-t-il au
Cameroon?\\\"\\nEnglish: 3. How many regions are there in
Cameroon?\\n"

2. Run it through an eval:

irb(main):167:0> eval "new_str = \"#{new_str}\""
=> "--- \nFrench: \"3. Combien de régions y a-t-il au
Cameroon?\"\nEnglish: 3. How many regions are there in Cameroon?\n"

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to