开发者

Ruby convert IDN domain from Punycode to Unicode

开发者 https://www.devze.com 2023-03-09 11:47 出处:网络
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 n

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开发者_JAVA技巧 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.


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"


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 :/


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('
0

精彩评论

暂无评论...
验证码 换一张
取 消