webtoaudio / test_load_messages.py
Brunwo
WIP
276796f
import gettext
import os
import locale
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
def load_translation(lang_code):
locale_path = os.path.join(os.path.dirname(__file__), 'locales')
try:
translation = gettext().translation('messages', localedir=locale_path, languages=[lang_code])
translation.install()
return translation.gettext # Return the translation function '_'
except FileNotFoundError:
print(f"Translation file for language '{lang_code}' not found.")
return lambda s: s # Fallback to no translation
except UnicodeDecodeError as e:
print(f"UnicodeDecodeError: {e}")
return lambda s: s # Fallback to no translation
def test_load_messages():
print("Testing English Translations:")
_ = load_translation('en')
print(_("podcast.intro"))
print(_("podcast.text_instructions"))
print("\nTesting French Translations:")
_ = load_translation('fr')
print(_("podcast.intro"))
print(_("podcast.text_instructions"))
print(_("podcast.scratch_pad"))
if __name__ == "__main__":
test_load_messages()