Using Google’s LibPhoneNumber in Microsoft.NET with C#

LibPhoneNumber from Google is an awesome library. LibPhoneNumber is a Java, C++ and Javascript library for parsing, formatting, storing and validating international phone numbers. You can input any phone number from over the world and the lib is telling you if the phone number might be valid, from which country the phone number is and the number format which is common for the number type and region.

 

libphonenrresultsmsmsmssm

 

Well, I was looking for such functionality, because our CRM users have to enter phone numbers from all over the world and we want to format and validate the phone number. Unfortunately there is no C# or VB.NET version available from this great Google library so I ended in doing some tests to integrate them into Microsoft.NET environment.

 

The first try

I’ve build my own JavaScript function the get the needed functionality running (region code, number formatting) in my own html site. Then I merged all related Google JS files from the closure-libaray into one single file with help of the Google closurebuilder.py. This task is very good documented by Google.

So, after that I was looking for a JavaScript engine running in .NET, so that I can execute JS-code in managed .NET runtime. I thought the DLR support of .NET would be the best environment to do this. Unfortunately there is no official JS engine from Microsoft so far. So I ended in trying some engines found on codeplex.com. javascriptdotnet.codeplex.com did the job. This engine can interpret the Google JS file (which has a size of ~500kb and is quite big) and returns the correct result. I should mention here that other engines did also work but sometimes with a wrong result. But stop, there is still a problem with this JS engine: it does not run under 64bit system. This is because the engine used the v8 engine from Google behind the scenes and the v8 only supports 32bit (x86) architectures. There are some hacks to use v8 under 64bit but these seems to be too much experimental.

 

Second try

I’ve tried to convert the Java code into C# manually and by a converter tool. Result: no success, because there are too much of specialties in the code and my java knowledge is somewhat old.

 

Third try

There is a C++ version available and I thought it should be possible to use it via p/invoke within .NET. But there seems to be a similar issue with 32/64bit systems. So I did not further investigate into this possibility.

 

Last and successful try

So finally I’ve tried to use the phone metadata directly within C#. Google’s metadata behind the Java project is a very well structured XML document written by Google which contains a lot of regex patterns. These patterns build a structure to find out to which country a number belongs and which number format is common for a specific region.

 

phonemetadataxmlofromgooglednmdmdmdm

 

I was very surprised how easy it was to use this metadata XML in my own code. With just a few lines and within two hours the prototype was working…. well, to finalize the library has cost me another day…

 

Source

At the end I thought it is worth to share my code with other C#/VB.NET folks. You can grab the code here. It does not everything the original Google library can do; I just implemented the cases that we need in our project (resolve the region and common format for any phone number). Here is a quick example of the C# code (from the Unit Test project…)

 

phonenumberresultincsharp

 

Let me see if someone other is interested to use this C# version. If so i can put the code onto codeplex.com or so. Any feedback is highly appreciated.

 

Happy coding

About these ads
Tagged , , ,

13 thoughts on “Using Google’s LibPhoneNumber in Microsoft.NET with C#

  1. DB says:

    Dude you rock. This is awesome. Thanks.

  2. Patrick Mézard says:

    Interested people may also look at https://bitbucket.org/pmezard/libphonenumber-csharp/wiki/Home. It should be on par with the Java version except for country level geocoding (.NET has no internationalized RegionCodes by default). That said, it is surely not as C#-ish as yours since I tried to stick to the Java API for maintenance reasons.

  3. Andrew Whiddett says:

    Oh – THANK YOU, this is very useful.

  4. [...] Using Google’s LibPhoneNumber in Microsoft.NET with C# « Kieners Blog. [...]

  5. ooswald says:

    Nice one. How do you handle the case when someone enters a national number like 031 311 11 11? Currently your code returns null. I am porting your code to Objective-C.

    • beatkiener says:

      Thank you for your feedback. It is interestingly that the source once more gets translated into another programming language :)
      Well, I use another converter before the phone-lib. This converter will convert any phone number into an international phone number like +41313221100.
      If you want I can provide you the source. Please let me know.

      • ooswald says:

        Any phone number? Yes, I would be interested [oli ät appswithlove.com]. In the meantime I ended up porting most of libPhoneNumber to ObjC. Will publish on github once its unit tested.

  6. forrestl says:

    Could you post your code to convert any phone number into international format when number is not preceded by a plus sign.

    • beatkiener says:

      Source is written in VB.NET and commented in german only.
      Please find source code here: http://files.thekieners.com/blogcontent/2012/ClassPhoneNumberFormatter.txt

      Usage: you must create an instance of formatter with your region settings. for example in Switzerland it is as following: New PhoneNumberFormatter(“0″, “41″, “00″).
      For other countries the counter-code will differ, network-carrier-code and international-carrier-code might be the same in your country.
      Please note that the formatter can do vanity number resolving too.

      have fun.

  7. techahmed says:

    You rock man :) Made my day.

    I am now using your library to verify my sms to send sms using http://www.experttexting.com gateway to send global SMS.

    Thanks. Do you intend to keep on updating this library ? specially Number type(LANDLINE,MOBILE E.T.C) would be great help ?

  8. RobIIII says:

    What I did was: use IKVM.net to convert the Java jar file into a .Net dll. Reference the DLL, reference IKVM.Net’s core DLL and you’re done. Every time a new version is released I just throw the .jar at IKVM.net, build a new .Net DLL and be done with it. Yes, there’s the overhead of IKVM but on the other hand I have 100% of the functionality provided by Google and it’s easy to stay up-to-date.

  9. Kishore says:

    Hi , wanna talk to you about the license and other terms to use. Please leave your contact email ID.

  10. neelam ojha says:

    thanks.this is very helpful to me. keep it up

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: