Spaces:
Sleeping
Sleeping
File size: 1,113 Bytes
276796f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
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()
|