Utf-8 encoding foreign charectors

hi,
i am trying to build and application that accepts different languages
and characters. There is two yaml files: english and frensh. i first
load the content of the French file as a hash array the dump it back
using the following command
File.open(french.yaml, “w”) {|f| YAML.dump(join_hash, f)}
Everything is working fine; however ruby encodes a string with foreign
characters when it converts back to yaml.
e.g
“français”.to_yaml => becomes “fran\xC3\xA7ais”

is there any way i can go around this; i have already wasted two days
researching.
Your help well be greatly appreciated
regards
MO

Mouhannad Oweism wrote:

hi,
i am trying to build and application that accepts different languages
and characters. There is two yaml files: english and frensh. i first
load the content of the French file as a hash array the dump it back
using the following command
File.open(french.yaml, “w”) {|f| YAML.dump(join_hash, f)}
Everything is working fine; however ruby encodes a string with foreign
characters when it converts back to yaml.
e.g
“français”.to_yaml => becomes “fran\xC3\xA7ais”

Well, \xC3\xA7 is the code point for the UTF8 representation of ç, so it
seems to be working at some level.

Ruby does not natively support UTF8. I believe that to enable UTF8
support in Rails you need to add this to your environment.rb file:

Set these for unicode support

$KCODE = ‘u’
require ‘jcode’

However, I cannot say whether or not this will help you in your specific
instance. In ruby script/console running in a DOS box on my Windows XP
your string above renders as “franaceais”

James B. wrote:
Take a look at iconv.
http://wiki.rubyonrails.com/rails/pages/iconv

Mouhannad Oweism wrote:

However what I am interested in is

conv = “français”.to_yaml

puts Iconv.new(‘UTF-8’, ‘UTF-8’).iconv(conv).inspect => NOTHING HAPPENS

Any suggestions?

This is a bug in Ruby’s YAML parser

James B. wrote:

James B. wrote:
Take a look at iconv.
http://wiki.rubyonrails.com/rails/pages/iconv

Thanks for the link James. This way can help in translating this:

puts Iconv.new(‘UTF-8’, ‘UTF-8’).iconv(“fran\xC3\xA7ais”).inspect =>
français

However what I am interested in is

conv = “français”.to_yaml

puts Iconv.new(‘UTF-8’, ‘UTF-8’).iconv(conv).inspect => NOTHING HAPPENS

Any suggestions?

MO

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs