将本地号码转换为国际格式(用于 Rails 和 Nexmo/Twilo 等服务)

Converting local numbers into international format (for use with Rails and services such as Nexmo/Twilo)

我想使用 Nexmo 或 Twilo 等服务验证我的用户 phone 号码。

要通过这些服务发送消息,我需要提供国际号码,但是,我不想要求我的用户提供完整的国际号码 - 只是他们会提供给任何人的正常号码他们自己的国家(我不希望他们知道他们的国际拨号代码等)。

那么将本地国内号码转换为国际号码的最佳方法是什么?除了他们的号码,每个用户都将被要求提供他们的国家。 (我的用户群将是国际化的。)

Google 的开源 libphonenumber project is probably the best tool for any sort of work involving phone numbers. Given a country and phone number, libphonenumber can be used to produce the E.164 format which Twilio prefers. It can also produce example phone numbers to improve the accuracy of the data you collect in the first place. A ruby port is available. An example Twilio app using libphonenumber in php can be found in Twilio Skills training.

phony_rails gem provides international formatting and validation. This is a rails wrapper for phony giving you view helpers and model validators. To see all the countries it supports see the phony 存储库。