Skip to content

Commit

Permalink
Always use preferred intl prefix if present
Browse files Browse the repository at this point in the history
  • Loading branch information
twcclegg committed Jun 1, 2021
1 parent a50dbe6 commit 3fac2c5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
4 changes: 2 additions & 2 deletions csharp/PhoneNumbers.Test/TestBuildMetadataFromXml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void TestLoadTerritoryTagMetadata()
{
var xmlInput =
"<territory countryCode='33' leadingDigits='2' internationalPrefix='00'" +
" preferredInternationalPrefix='0011' nationalPrefixForParsing='0'" +
" preferredInternationalPrefix='00~11' nationalPrefixForParsing='0'" +
" nationalPrefixTransformRule='9$1'" + // nationalPrefix manually injected.
" preferredExtnPrefix=' x' mainCountryForCode='true'" +
" leadingZeroPossible='true' mobileNumberPortableRegion='true'>" +
Expand All @@ -103,7 +103,7 @@ public void TestLoadTerritoryTagMetadata()
Assert.Equal(33, phoneMetadata.CountryCode);
Assert.Equal("2", phoneMetadata.LeadingDigits);
Assert.Equal("00", phoneMetadata.InternationalPrefix);
Assert.Equal("0011", phoneMetadata.PreferredInternationalPrefix);
Assert.Equal("00~11", phoneMetadata.PreferredInternationalPrefix);
Assert.Equal("0", phoneMetadata.NationalPrefixForParsing);
Assert.Equal("9$1", phoneMetadata.NationalPrefixTransformRule);
Assert.Equal("0", phoneMetadata.NationalPrefix);
Expand Down
3 changes: 3 additions & 0 deletions csharp/PhoneNumbers.Test/TestPhoneNumberUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,9 @@ public void TestFormatOutOfCountryWithPreferredIntlPrefix()
// are accepted as possible international prefixes in our test metadta.)
Assert.Equal("0011 39 02 3661 8300",
phoneUtil.FormatOutOfCountryCallingNumber(ITNumber, RegionCode.AU));

// Testing preferred international prefixes with ~ are supported (designates waiting).
Assert.Equal("8~10 39 02 3661 8300", phoneUtil.FormatOutOfCountryCallingNumber(ITNumber, RegionCode.UZ));
}

[Fact]
Expand Down
15 changes: 8 additions & 7 deletions csharp/PhoneNumbers/PhoneNumberUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1462,18 +1462,19 @@ public string FormatOutOfCountryCallingNumber(PhoneNumber number, string regionC
var metadataForRegionCallingFrom = GetMetadataForRegion(regionCallingFrom);
var internationalPrefix = metadataForRegionCallingFrom.InternationalPrefix;

// For regions that have multiple international prefixes, the international format of the
// number is returned, unless there is a preferred international prefix.
// In general, if there is a preferred international prefix, use that. Otherwise, for regions
// that have multiple international prefixes, the international format of the number is
// returned since we would not know which one to use.
var internationalPrefixForFormatting = "";
if (UniqueInternationalPrefix.IsMatchAll(internationalPrefix))
{
internationalPrefixForFormatting = internationalPrefix;
}
else if (metadataForRegionCallingFrom.HasPreferredInternationalPrefix)
if (metadataForRegionCallingFrom.HasPreferredInternationalPrefix)
{
internationalPrefixForFormatting =
metadataForRegionCallingFrom.PreferredInternationalPrefix;
}
else if (UniqueInternationalPrefix.IsMatchAll(internationalPrefix))
{
internationalPrefixForFormatting = internationalPrefix;
}

var regionCode = GetRegionCodeForCountryCode(countryCallingCode);
var metadataForRegion =
Expand Down

0 comments on commit 3fac2c5

Please sign in to comment.