If you have a list of supported_languages and a desired_language from a user, you can use closest_supported_match to find the best available option.
closest_supported_match(desired_language, supported_languages, max_distance=25)
Returns the language tag of the best-supported language. If no language in the list is within the max_distance threshold, it returns None.
closest_match(desired_language, supported_languages, max_distance=None)
Returns a tuple containing (best_matching_tag, distance). If no match is found within the max_distance (or if no distance is provided and no match exists), it returns ('und', 1000).
# Returns the best tag or None if too far
closest_supported_match('af', ['en', 'nl', 'zu'], max_distance=10) # returns None
# Returns (tag, distance)
closest_match('id', ['zsm', 'mhp']) # returns ('zsm', 14)
# Returns (tag, distance) with a custom cutoff
closest_match('ja', ['ja-Latn-hepburn', 'en'], max_distance=60) # returns ('ja-Latn-hepburn', 50)