Ruby convert IDN domain from Punycode to Unicode

后端 未结 4 1866
北海茫月
北海茫月 2021-01-05 06:53

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,

相关标签:
4条回答
  • 2021-01-05 07:08

    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-"
    
    0 讨论(0)
  • 2021-01-05 07:09

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

    0 讨论(0)
  • 2021-01-05 07:21

    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"
    
    0 讨论(0)
  • 2021-01-05 07:29

    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 讨论(0)
提交回复
热议问题