in modules/util.py [0:0]
def create_localized_faker(self, country_code: str):
"""
Given a country code, creates a Faker instance with the appropriate locale.
Ensures valid Faker locale names are used.
Returns:
-------
Optional[Tuple[Faker, bool]] -> (faker_instance, is_valid_locale) or None if invalid.
"""
# Check if locale exists, otherwise return None
locale = next(filter(lambda x: country_code in x, AVAILABLE_LOCALES), None)
if not locale:
logging.error(
f"Invalid country code `{country_code}`. No faker instance created."
)
return None # No fallback
try:
# seed to get consistent data
if self.fake is None:
if locale != self.locale:
Faker.seed(locale)
self.locale = locale
self.fake = Faker(locale)
faker = self.fake
self.fake = faker
faker.add_provider(internet)
faker.add_provider(misc)
return faker, True
except AttributeError:
logging.error(
f"Invalid locale `{locale}`. Faker instance could not be created."
)
return None