Spaces:
Sleeping
Sleeping
Brunwo (aider)
commited on
Commit
•
001156c
1
Parent(s):
1bd636b
test: add a test file for loading and debugging message translations in English and French locales
Browse files
path/to/test_load_messages.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gettext
|
2 |
+
import os
|
3 |
+
|
4 |
+
def load_translation(lang_code):
|
5 |
+
locale_path = os.path.join(os.path.dirname(__file__), 'locales')
|
6 |
+
try:
|
7 |
+
translation = gettext.translation('messages', localedir=locale_path, languages=[lang_code])
|
8 |
+
translation.install()
|
9 |
+
return translation.gettext # Return the translation function '_'
|
10 |
+
except FileNotFoundError:
|
11 |
+
print(f"Translation file for language '{lang_code}' not found.")
|
12 |
+
return lambda s: s # Fallback to no translation
|
13 |
+
except UnicodeDecodeError as e:
|
14 |
+
print(f"UnicodeDecodeError: {e}")
|
15 |
+
return lambda s: s # Fallback to no translation
|
16 |
+
|
17 |
+
def test_load_messages():
|
18 |
+
print("Testing English Translations:")
|
19 |
+
_ = load_translation('en')
|
20 |
+
print(_("podcast.intro"))
|
21 |
+
print(_("podcast.text_instructions"))
|
22 |
+
|
23 |
+
print("\nTesting French Translations:")
|
24 |
+
_ = load_translation('fr')
|
25 |
+
print(_("podcast.intro"))
|
26 |
+
print(_("podcast.text_instructions"))
|
27 |
+
|
28 |
+
if __name__ == "__main__":
|
29 |
+
test_load_messages()
|