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? I am thinking on following lines: 2. str = str.gsub(''"'', '''') 3. **Need to add a line which replaces \\ in the str above to just \** 4. str = str.force_encoding("iso-8859-1") 5. str = str.encode(''UTF-8'') In step 3, I was thinking of something like str = str.gsub(/\\\\/, "\\") OR somehow if possible push output of puts or a similar function back to str example: > puts str --- French: 3. Combien de r\xC3\xA9gions y a-t-il au Cameroon? English: 3. How many regions are there in Cameroon? but even that works. Can someone please assist? -- 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 rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
tamouse mailing lists
2013-May-16 08:46 UTC
Re: French sentences appearing weird in Rails Website
On Wed, May 15, 2013 at 6:30 AM, UA <ritvij.j-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Wednesday, 15 May 2013 07:30:14 UTC-4, UA 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? > >Where is this text coming from? Because that string looks like YAML, complete with the opening "---". \xC3\xA9 is the UTF-8 encoding of codepoint U+00E9, "small letter e with acute", something you''d expect in French text. If you do `YAML.load(str)` in 1.9 or higher, this is what appears: irb: YAML.load(str) ===> {"French"=>"3. Combien de régions y a-t-il au Cameroon?", "English"=>"3. How many regions are there in Cameroon?"} --Matt Jones -- 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 rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/76daf6fd-a064-4317-8052-157b13925562%40googlegroups.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
tamouse mailing lists
2013-May-17 03:30 UTC
Re: Re: French sentences appearing weird in Rails Website
On May 16, 2013 8:23 AM, "Matt Jones" <al2o3cr-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > > On Wednesday, 15 May 2013 07:30:14 UTC-4, UA 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? >> > > Where is this text coming from? Because that string looks like YAML,complete with the opening "---". \xC3\xA9 is the UTF-8 encoding of codepoint U+00E9, "small letter e with acute", something you''d expect in French text.> > If you do `YAML.load(str)` in 1.9 or higher, this is what appears: > > irb: YAML.load(str) > ===> {"French"=>"3. Combien de régions y a-t-il au Cameroon?","English"=>"3. How many regions are there in Cameroon?"}> > --Matt JonesThat''s what I thought originally, too. When I copied the OP''s string as written, and fed it to YAML.load, it flubbed the translation, reversing thebyte order. As far as I can tell, I have UTF-8 set everywhere. So I''m not sure why it works for you but not for me... -- 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 rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.