ruby-on-railsrubyruby-on-rails-3unicodeidn

Ruby convert IDN domain from Punycode to Unicode


I'm writing a Rails app that needs to convert an IDN domain name from Punycode into its Unicode equivalent. I tried installing the idn gem that has bindings to GNU LibIDN, but it won't compile the native code. Apparently others have the same issue with Ruby 1.9.x.

I also tried the pure Ruby SimpleIDN gem, but I would prefer something native.


Solution

  • Try the simpleidn gem. It works with Ruby 1.8.7 and 1.9.2.

    Edit your Gemfile:

    gem 'simpleidn'
    

    then you can enter the command as follows:

    SimpleIDN.to_unicode("xn--mllerriis-l8a.com")
    => "møllerriis.com"
    
    SimpleIDN.to_ascii("møllerriis.com")
    => "xn--mllerriis-l8a.com"