Ruby convert IDN domain from Punycode to Unicode
Asked Answered
F

4

10

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.

Furr answered 1/6, 2011 at 4:28 Comment(0)
B
17

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"
Brothers answered 10/3, 2012 at 17:21 Comment(0)
F
3

Whoops - looks like I found a capable answer shortly after posting (sorry). There is a subtly placed patch from 09/2010 in the bug reports section of the project's RubyForge page. Adding this to my Gemfile now allows me to use the idn library:

gem 'idn', '~> 0.0.2', :git => 'git://github.com/mihu/idn'

Too bad that the gem is apparently abandoned :/

Furr answered 1/6, 2011 at 4:37 Comment(2)
Maybe you want to maintain the gem, would be nice.Paisano
@Paisano I totally would, but I'm still a Ruby noob. Maybe someday :)Furr
G
2

The Dnsruby::Name class with the method .punicode of the Dnsruby lib allows you to convert an IDN domain from Unicode UTF-8 to ASCII punycode:

Dnsruby::Name.punycode('🏳.cf')
# => "xn--en8h.cf"
Gigantean answered 11/11, 2019 at 14:45 Comment(0)
S
0

https://github.com/knu/ruby-domain_name seem to have exactly the same functionality:

irb(main):018:0> SimpleIDN::Punycode.encode('axa.test')
=> "axa.test-"

irb(main):017:0> DomainName::Punycode.encode('axa.test')
=> "axa.test-"
Shutin answered 9/9, 2019 at 11:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.