On Wed, May 15, 2013 at 6:30 AM, UA <
ritv...@gmail.com> 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