Ruby 将 IDN 域从 Punycode 转换为 Unicode [英] Ruby convert IDN domain from Punycode to Unicode

查看:42
本文介绍了Ruby 将 IDN 域从 Punycode 转换为 Unicode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 Rails 应用程序,需要将 IDN 域名从 Punycode 转换为其等效的 Unicode.我尝试安装与 GNU LibIDN 绑定的 idn gem,但它不会编译本机代码.显然其他人在使用 Ruby 1.9.x 时也有同样的问题.

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.

我也尝试过纯 Ruby SimpleIDN gem,但我更喜欢原生的东西.

I also tried the pure Ruby SimpleIDN gem, but I would prefer something native.

推荐答案

尝试 simpleidn gem.它适用于 Ruby 1.8.7 和 1.9.2.

Try the simpleidn gem. It works with Ruby 1.8.7 and 1.9.2.

编辑您的 Gemfile:

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"

这篇关于Ruby 将 IDN 域从 Punycode 转换为 Unicode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆