change or extend Phonemetadata

249 views
Skip to first unread message

BoCas

unread,
Mar 23, 2011, 6:15:46 AM3/23/11
to libphonenumber-discuss
Hello everybody,

i am using the libphonenumber library to change the format of phone
numbers. Unfortunately I'm facing a problem with German numbers and I
would like to change the behavior of the formatting.

The following lines are used to format a phone number:

PhoneNumber phoneNumber =
PhoneNumberUtil.getInstance().parse(unformattedValue, "DE");
value = phoneUtil.format(phoneNumber,
PhoneNumberFormat.INTERNATIONAL);

I get the following result:
unformattedValue="0172 7369456" -> value="+49 172 7369456"
unformattedValue="089 123456789" -> value="+49 89/123456789"

Now I would like to format the second number with a space instead of a
slash (+49 89 123456789).

I have tried to understand why libphonenumber is using the space for
mobil numbers and the slash for normal phone numbers. It seems that
the PhoneMetadata for Germany contains NumberFormat List that contains
7 entries. The first entry is taken because it matches the entered
number.
Looking at the PhoneMetadata regular expressions, there would be
probably more things to change.

How can I change the PhoneMetadata and use them with PhoneNumberUtil?
Why isn't there a way to access and to manipulate these Data?

Is there any documentation about the PhoneMetadata?

Thanks in advance.
Cheers,
Boris

Shaopeng

unread,
Mar 23, 2011, 7:20:12 AM3/23/11
to libphonenum...@googlegroups.com, BoCas
On Wed, Mar 23, 2011 at 11:15 AM, BoCas <cassar...@googlemail.com> wrote:
Hello everybody,

i am using the libphonenumber library to change the format of phone
numbers. Unfortunately I'm facing a problem with German numbers and I
would like to change the behavior of the formatting.

The following lines are used to format a phone number:

               PhoneNumber phoneNumber =
PhoneNumberUtil.getInstance().parse(unformattedValue, "DE");
               value = phoneUtil.format(phoneNumber,
PhoneNumberFormat.INTERNATIONAL);

I get the following result:
unformattedValue="0172 7369456" -> value="+49 172 7369456"
unformattedValue="089 123456789" -> value="+49 89/123456789"

Now I would like to format the second number with a space instead of a
slash (+49 89 123456789).

I have tried to understand why libphonenumber is using the space for
mobil numbers and the slash for normal phone numbers. It seems that
the PhoneMetadata for Germany contains NumberFormat List that contains
7 entries. The first entry is taken because it matches the entered
number.
Looking at the PhoneMetadata regular expressions, there would be
probably more things to change.

You could use formatByPattern to override the default behavior, which is controlled by the metadata.
 

How can I change the PhoneMetadata and use them with PhoneNumberUtil?
Why isn't there a way to access and to manipulate these Data?

Is there any documentation about the PhoneMetadata?

Lara Rennie

unread,
Mar 23, 2011, 7:33:09 AM3/23/11
to libphonenum...@googlegroups.com, Shaopeng, BoCas
2011/3/23 Shaopeng <jia.sh...@gmail.com>:

>
>
> On Wed, Mar 23, 2011 at 11:15 AM, BoCas <cassar...@googlemail.com> wrote:
>>
>> Hello everybody,
>>
>> i am using the libphonenumber library to change the format of phone
>> numbers. Unfortunately I'm facing a problem with German numbers and I
>> would like to change the behavior of the formatting.
>>
>> The following lines are used to format a phone number:
>>
>>                PhoneNumber phoneNumber =
>> PhoneNumberUtil.getInstance().parse(unformattedValue, "DE");
>>                value = phoneUtil.format(phoneNumber,
>> PhoneNumberFormat.INTERNATIONAL);
>>
>> I get the following result:
>> unformattedValue="0172 7369456" -> value="+49 172 7369456"
>> unformattedValue="089 123456789" -> value="+49 89/123456789"
>>
>> Now I would like to format the second number with a space instead of a
>> slash (+49 89 123456789).
>>
>> I have tried to understand why libphonenumber is using the space for
>> mobil numbers and the slash for normal phone numbers. It seems that
>> the PhoneMetadata for Germany contains NumberFormat List that contains
>> 7 entries. The first entry is taken because it matches the entered
>> number.
>> Looking at the PhoneMetadata regular expressions, there would be
>> probably more things to change.

Often mobile and fixed-line formatting are different - the slash is
indicating that the prefix here is optional (an area code), but the
mobile numbers must be fully dialled.

A simpler way if you don't want to change the metadata or use format
by pattern is to do your own replace on the string afterwards to
change "/" and whatever other chars you don't want to a space...

Boris Cassar

unread,
Mar 23, 2011, 8:18:11 AM3/23/11
to Lara Rennie, libphonenum...@googlegroups.com, Shaopeng
Hi Lara,

thanks for the quick response. This is exactly how I did it. I Hoped for a cleaner solution.
The reason why I didn't choose the formatByPattern was that I don't know how the input is formatted. As I understood, I would have to pass a list with NumberFormat objects and each possible entry format must be matched by a pattern.

Cheers,
Boris


2011/3/23 Lara Rennie <larar...@google.com>

Shaopeng

unread,
Mar 23, 2011, 8:20:59 AM3/23/11
to Boris Cassar, Lara Rennie, libphonenum...@googlegroups.com
Also, is there any specific reason why you want to replace the slash with space in the first place? It is the commonly used way of formatting German fixed-line numbers.

Shaopeng

Lara Rennie

unread,
Mar 23, 2011, 8:22:31 AM3/23/11
to Boris Cassar, libphonenum...@googlegroups.com, Shaopeng
Yes, using formatByPattern is not trivial. I wonder if we should
provide some sort of method where people specify which formatting
symbols they want to use, and we replace ours with those instead, so
they don't have to rewrite the patterns? For the general case that
would be okay and allow the user to specify, for example, no slashes,
or no parentheses.....

We probably have to gather some use-cases to see what kind of
generalised solution would be appropriate. Is it just slashes you
don't want, or are there other symbols too?

2011/3/23 Boris Cassar <cassar...@googlemail.com>:

Shaopeng

unread,
Mar 23, 2011, 9:38:00 AM3/23/11
to Boris Cassar, libphonenum...@googlegroups.com
Hello Boris,

Thanks for your nice word! I agree that with +49 prefix, space is more commonly used then slash. If you could raise a bug in our project, we could fix this for you in our next release.

Cheers,

Shaopeng

On Wed, Mar 23, 2011 at 1:37 PM, Boris Cassar <cassar...@googlemail.com> wrote:
Hello Shaopeng,
my customer want to see the phone numbers displayed this way.
In Germany we use the slash if we write the numbers without the country prefix, i.e. 089/123456789 or a mobil number 0172/7369456. If there is the +49 prefix, it is not common to use the slash. But I don't think that this is a strict rule, there are plenty of special cases and possibilities.
In my case, I was very happy to find your library. I was able to present a quick solution to my customer (he came up with this issue a week before production delivery date). Thanks a lot. You saved me!
Boris


2011/3/23 Shaopeng <jia.sh...@gmail.com>
Reply all
Reply to author
Forward
0 new messages