libphonenumber README

The project's README from github.com/google/libphonenumber. As one markdown file: README.md.

What is it?

Google's common Java, C++ and JavaScript library for parsing, formatting, and validating international phone numbers. The Java version is optimized for running on smartphones, and is used by the Android framework since 4.0 (Ice Cream Sandwich).

Quick links

Highlights of functionality

Demo

Java

The Java demo is updated with a slight delay after the GitHub release.

Last demo update: v9.0.39.

Note: Even though the library (main branch/maven release) is at v8.12.57, because of some deployment issues, we were unable to update the Java demo with the new binary version. We will soon fix this. Meantime, please use JS demo.

If this number is lower than the latest release's version number, we are between releases and the demo may be at either version.

Demo App

There is a demo Android App called E.164 Formatter in this repository. The purpose of this App is to show an example of how the library can be used in a real-life situation, in this case specifically in an Android App using Java.

JavaScript

The JavaScript demo may be run at various tags; this link will take you to master.

Java code

To include the Java code in your application, either integrate with Maven (see wiki) or download the latest jars from the Maven repository.

Javadoc

Javadoc is automatically updated to reflect the latest release at https://javadoc.io/doc/com.googlecode.libphonenumber/libphonenumber/.

Versioning and Announcements

We generally choose the release number following these guidelines.

If any of the changes pushed to master since the last release are incompatible with the intent / specification of an existing libphonenumber API or may cause libphonenumber (Java, C++, or JS) clients to have to change their code to keep building, we publish a major release. For example, if the last release were 7.7.3, the new one would be 8.0.0.

If any of those changes enable clients to update their code to take advantage of new functionality, and if clients would have to roll-back these changes in the event that the release was marked as "bad", we publish a minor release. For example, we'd go from 7.7.3 to 7.8.0.

Otherwise, including when a release contains only metadata changes, we publish a sub-minor release, e.g. 7.7.3 to 7.7.4.

Sometimes we make internal changes to the code or metadata that, while not affecting compatibility for clients, could affect compatibility for porters of the library. For such changes we make announcements to libphonenumber-discuss. Such changes are not reflected in the version number, and we would publish a sub-minor release if there were no other changes.

Want to get notified of new releases? During most of the year, excepting holidays and extenuating circumstances, we release fortnightly. We update release tags and document detailed release notes. We also send an announcement to libphonenumber-discuss for every release.

Quick Examples

Let's say you have a string representing a phone number from Switzerland. This is how you parse/normalize it into a PhoneNumber object:

String swissNumberStr = "044 668 18 00";
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
try {
  PhoneNumber swissNumberProto = phoneUtil.parse(swissNumberStr, "CH");
} catch (NumberParseException e) {
  System.err.println("NumberParseException was thrown: " + e.toString());
}

At this point, swissNumberProto contains:

{
  "country_code": 41,
  "national_number": 446681800
}

PhoneNumber is a class that was originally auto-generated from phonenumber.proto with necessary modifications for efficiency. For details on the meaning of each field, refer to resources/phonenumber.proto.

Now let us validate whether the number is valid:

boolean isValid = phoneUtil.isValidNumber(swissNumberProto); // returns true

There are a few formats supported by the formatting method, as illustrated below:

// Produces "+41 44 668 18 00"
System.out.println(phoneUtil.format(swissNumberProto, PhoneNumberFormat.INTERNATIONAL));
// Produces "044 668 18 00"
System.out.println(phoneUtil.format(swissNumberProto, PhoneNumberFormat.NATIONAL));
// Produces "+41446681800"
System.out.println(phoneUtil.format(swissNumberProto, PhoneNumberFormat.E164));

You could also choose to format the number in the way it is dialed from another country:

// Produces "011 41 44 668 1800", the number when it is dialed in the United States.
System.out.println(phoneUtil.formatOutOfCountryCallingNumber(swissNumberProto, "US"));

Formatting Phone Numbers 'as you type'

PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("US");
System.out.println(formatter.inputDigit('6'));  // Outputs "6"
...  // Input more digits
System.out.println(formatter.inputDigit('3'));  // Now outputs "650 253"

Geocoding Phone Numbers

PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder.getInstance();
// Outputs "Zurich"
System.out.println(geocoder.getDescriptionForNumber(swissNumberProto, Locale.ENGLISH));
// Outputs "Zürich"
System.out.println(geocoder.getDescriptionForNumber(swissNumberProto, Locale.GERMAN));
// Outputs "Zurigo"
System.out.println(geocoder.getDescriptionForNumber(swissNumberProto, Locale.ITALIAN));

Mapping Phone Numbers to original carriers

Caveat: We do not provide data about the current carrier of a phone number, only the original carrier who is assigned the corresponding range. Read about number portability.

PhoneNumber swissMobileNumber =
    new PhoneNumber().setCountryCode(41).setNationalNumber(798765432L);
PhoneNumberToCarrierMapper carrierMapper = PhoneNumberToCarrierMapper.getInstance();
// Outputs "Swisscom"
System.out.println(carrierMapper.getNameForNumber(swissMobileNumber, Locale.ENGLISH));

More examples on how to use the library can be found in the unit tests.

Third-party Ports

Several third-party ports of the phone number library are known to us. We share them here in case they're useful for developers.

However, we emphasize that these ports are by developers outside the libphonenumber project. We do not evaluate their quality or influence their maintenance processes.

Alternatives to our own versions:

Tools based on libphonenumber metadata:

libphonenumber (google/libphonenumber)

Searching Database…

这是什么?Google 推出的库,用于解析、格式化和验证国际电话号码,提供 Java、C++ 和 JavaScript 版本。
这是我要嵌入的库,还是独立的应用?这是一个可以添加到你自己的 Java、C++ 或 JavaScript 项目中的库;它附带一个演示用的 Android 应用和一个 JS 演示供你试用,但它本身并不是面向最终用户的工具。
怎么把它加到我的项目里?对于 Java,可以通过 Maven 集成,或从 Maven 仓库下载最新的 jar 包;C++ 和 JS 版本则需从本仓库的源码目录构建。
JS 版本需要额外的东西吗?官方 JS 版本依赖 Google Closure;如果你不想要这个依赖,可以选择第三方替代方案,例如精简重写版,或与 browserify 兼容的 npm 封装版。
它能告诉我号码当前的运营商吗?不能。运营商映射只能返回该号段最初分配的运营商,无法得知当前的归属方,原因是号码携号转网。
Java 版本是专门为 Android 优化的吗?是的,Java 版本针对智能手机做了优化,自 Android 4.0(Ice Cream Sandwich)起已被 Android 框架使用;Android 应用需要重新打包元数据,并通过 AssetManager 加载,或者使用已经这样做的第三方移植版。
它会查询手机机主的个人信息吗?不会。它只根据号码本身提供地理位置、运营商和时区信息,无法识别实际的个人或当前的机主。
这个库到底能告诉我号码的什么信息?它可以解析和格式化号码,判断号码是否有效,识别号码类型(如手机、免费电话、VoIP 等),从文本中找出嵌入的号码,在用户输入时实时格式化号码,并提供离线的地理位置、运营商和时区查询。
有没有 Python 或 Go 等其他语言的移植版?有的,C#、Go、Python、Ruby、Rust、PHP、Swift、Objective-C、Elixir、Dart、R、Clojure、Erlang 等语言都有第三方移植版,但 Google 并不维护或审核这些版本。
电话号码元数据多久更新一次?该项目通常每两周发布一次版本,根据改动是否破坏兼容性、是否新增功能或仅是元数据更新,分别发布主版本、次版本或修订版本。
如果不想要 Closure 依赖,有没有更轻量的 JS 替代品?有的,社区提供了一些替代方案,例如约 110KB 的精简重写版,以及基于原版 Closure 库封装的、与 browserify 兼容的 npm 包装版(约 420KB)。
Creator
google
Size
318.3 MB
Stars
18.3K
Age
11y
Commits · 90 Days