Thanks to this snippet , I finally found a solution that did not require libidn. It is built on punicode4r along with a unicode gem (a pre-generated binary can be found here ) or using ActiveSupport. I will use ActiveSupport, since I use Rails anyway, but for reference, I include both methods.
When using unicode gem:
require 'unicode'
require 'punycode'
def idn_encode(domain)
parts = domain.split(".").map do |label|
encoded = Punycode.encode(Unicode::normalize_KC(Unicode::downcase(label)))
if encoded =~ /-$/
encoded.chop!
else
"xn--" + encoded
end
end
parts.join(".")
end
With ActiveSupport :
require "punycode"
require "active_support"
$KCODE = "UTF-8"
def idn_encode(domain)
parts = domain.split(".").map do |label|
encoded = Punycode.encode(label.mb_chars.downcase.normalize(:kc))
if encoded =~ /-$/
encoded.chop!
else
"xn--" + encoded
end
end
parts.join(".")
end
ActiveSupport this StackOverflow.