Source code for bot_api.util.country_code_util

import pycountry
import locale


[docs] class CountryCodeUtil: """Country code utility class."""
[docs] @staticmethod def local_country_code(): """ Get the local country code based on the current locale. Returns: str or None: The two-letter country code in uppercase if found, None otherwise. """ try: # Get the current locale's language and territory current_locale = locale.getlocale()[0] if current_locale and '_' in current_locale: country_code = current_locale.split('_')[1] # Verify it's a valid country code if pycountry.countries.get(alpha_2=country_code): return country_code except (IndexError, AttributeError): pass return None
[docs] @staticmethod def to_country_code(code): if not code or not isinstance(code, str) or len(code.strip()) != 2: return None try: return pycountry.countries.get(alpha_2=code.strip().upper()) except (KeyError, AttributeError): return None