@@ -932,48 +932,52 @@ def format_number_for_mobile_dialing(numobj, region_calling_from, with_formattin
932932 numobj_no_ext = PhoneNumber ()
933933 numobj_no_ext .merge_from (numobj )
934934 numobj_no_ext .extension = None
935- numobj_type = number_type (numobj_no_ext )
936935 region_code = region_code_for_country_code (country_calling_code )
937- if region_code == "CO" and region_calling_from == "CO" :
938- if numobj_type == PhoneNumberType .FIXED_LINE :
936+
937+ formatted_number = ""
938+ if region_calling_from == region_code :
939+ numobj_type = number_type (numobj_no_ext )
940+ is_fixed_line_or_mobile = ((numobj_type == PhoneNumberType .FIXED_LINE ) or
941+ (numobj_type == PhoneNumberType .MOBILE ) or
942+ (numobj_type == PhoneNumberType .FIXED_LINE_OR_MOBILE ))
943+ # Carrier codes may be needed in some countries. We handle this here.
944+ if region_code == "CO" and numobj_type == PhoneNumberType .FIXED_LINE :
939945 formatted_number = format_national_number_with_carrier_code (numobj_no_ext ,
940946 _COLOMBIA_MOBILE_TO_FIXED_LINE_PREFIX )
947+ elif region_code == "BR" and is_fixed_line_or_mobile :
948+ if numobj_no_ext .preferred_domestic_carrier_code is not None :
949+ formatted_number = format_national_number_with_preferred_carrier_code (numobj_no_ext , "" )
950+ else :
951+ # Brazilian fixed line and mobile numbers need to be dialed with a
952+ # carrier code when called within Brazil. Without that, most of
953+ # the carriers won't connect the call. Because of that, we return
954+ # an empty string here.
955+ formatted_number = ""
941956 else :
942- # E164 doesn't work at all when dialling within Colombia
943- formatted_number = format_number (numobj_no_ext , PhoneNumberFormat .NATIONAL )
944- elif region_code == "PE" and region_calling_from == "PE" :
945- # In Peru, numbers cannot be dialled using E164 format from a mobile
946- # phone for Movistar. Instead they must be dialled in national
947- # format.
948- formatted_number = format_number (numobj_no_ext , PhoneNumberFormat .NATIONAL )
949- elif (region_code == "AE" and region_calling_from == "AE" and
950- numobj_type == PhoneNumberType .UAN ):
951- # In the United Arab Emirates, numbers with the prefix 600 (UAN
952- # numbers) cannot be dialled using E164 format. Instead they must be
953- # dialled in national format.
954- formatted_number = format_number (numobj_no_ext , PhoneNumberFormat .NATIONAL )
955- elif (region_code == "BR" and region_calling_from == "BR" and
956- ((numobj_type == PhoneNumberType .FIXED_LINE ) or
957- (numobj_type == PhoneNumberType .MOBILE ) or
958- (numobj_type == PhoneNumberType .FIXED_LINE_OR_MOBILE ))):
959- if numobj_no_ext .preferred_domestic_carrier_code is not None :
960- formatted_number = format_national_number_with_preferred_carrier_code (numobj_no_ext , "" )
961- else :
962- # Brazilian fixed line and mobile numbers need to be dialed with a
963- # carrier code when called within Brazil. Without that, most of
964- # the carriers won't connect the call. Because of that, we return
965- # an empty string here.
966- formatted_number = ""
957+ # For NANPA countries, non-geographical countries, and Mexican
958+ # fixed line and mobile numbers, we output international format
959+ # for numbers that can be dialed internationally as that always
960+ # works.
961+ if ((country_calling_code == _NANPA_COUNTRY_CODE or
962+ region_code == REGION_CODE_FOR_NON_GEO_ENTITY or
963+ (region_code == "MX" and is_fixed_line_or_mobile )) and
964+ _can_be_internationally_dialled (numobj_no_ext )):
965+ # MX fixed line and mobile numbers should always be formatted
966+ # in international format, even when dialed within MX. For
967+ # national format to work, a carrier code needs to be used,
968+ # and the correct carrier code depends on if the caller and
969+ # callee are from the same local area. It is trickier to get
970+ # that to work correctly than using international format,
971+ # which is tested to work fine on all carriers.
972+ formatted_number = format_number (numobj_no_ext , PhoneNumberFormat .INTERNATIONAL )
973+ else :
974+ formatted_number = format_number (numobj_no_ext , PhoneNumberFormat .NATIONAL )
967975 elif _can_be_internationally_dialled (numobj_no_ext ):
968976 if with_formatting :
969977 return format_number (numobj_no_ext , PhoneNumberFormat .INTERNATIONAL )
970978 else :
971979 return format_number (numobj_no_ext , PhoneNumberFormat .E164 )
972- else :
973- if region_calling_from == region_code :
974- formatted_number = format_number (numobj_no_ext , PhoneNumberFormat .NATIONAL )
975- else :
976- formatted_number = ""
980+
977981 if with_formatting :
978982 return formatted_number
979983 else :
0 commit comments