Ruby convert IDN domain from Punycode to Unicode

狂风中的少年 提交于 2019-12-30 06:33: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.


回答1:


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"



回答2:


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




回答3:


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-"



回答4:


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"


来源:https://stackoverflow.com/questions/6196282/ruby-convert-idn-domain-from-punycode-to-unicode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!