Tag Archives: C#

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

Advertisements
Tagged , , ,