getDescriptionForNumber method

String getDescriptionForNumber(
  1. PhoneNumber number,
  2. String language,
  3. String script,
  4. String region,
)

Returns a text description in the given language for the given phone number.

number the phone number for which we want to get a text description language two or three-letter lowercase ISO language codes as defined by ISO 639. Note that where two different language codes exist (e.g. 'he' and 'iw' for Hebrew) we use the one that Java/Android canonicalized on ('iw' in this case). script four-letter titlecase (the first letter is uppercase and the rest of the letters are lowercase) ISO script code as defined in ISO 15924 region two-letter uppercase ISO country code as defined by ISO 3166-1

returns a text description in the given language for the given phone number, or an empty string if a description is not available

Implementation

String getDescriptionForNumber(
    PhoneNumber number, String language, String script, String region) {
  int countryCallingCode = number.countryCode;
  // As the NANPA data is split into multiple files covering 3-digit areas, use a
  // phone number prefix of 4 digits for NANPA instead, e.g. 1650.
  int phonePrefix = (countryCallingCode != 1)
      ? countryCallingCode
      : (1000 + (number.nationalNumber.toInt() ~/ 10000000));
  PhonePrefixMap? phonePrefixDescriptions =
      _getPhonePrefixDescriptions(phonePrefix, language, script, region);

  String? description = (phonePrefixDescriptions != null)
      ? phonePrefixDescriptions.lookup(phoneNumber: number)
      : null;

  // When a location is not available in the requested language, fall back to English.
  if ((description == null || description.isEmpty) &&
      _mayFallBackToEnglish(language)) {
    PhonePrefixMap? defaultMap =
        _getPhonePrefixDescriptions(phonePrefix, "en", "", "");
    if (defaultMap == null) {
      return '';
    }
    description = defaultMap.lookup(phoneNumber: number);
  }

  return (description != null) ? description : '';
}