-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi18n.py
More file actions
133 lines (105 loc) · 3.33 KB
/
i18n.py
File metadata and controls
133 lines (105 loc) · 3.33 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"""
Internationalization (i18n) module for Memo Tori.
Supports English (en) and French (fr) locales.
Language is determined by:
1. MEMO_TORI_LANG environment variable (e.g., 'en' or 'fr')
2. System locale (e.g., fr_FR -> 'fr', en_US -> 'en')
3. Default fallback to English
"""
import os
import locale
# Available languages
LANGUAGES = ["en", "fr"]
DEFAULT_LANGUAGE = "en"
# Translation dictionaries
TRANSLATIONS = {
"en": {
# Window title
"window_title": "Memo Tori",
# Form view
"textarea_placeholder": "Your idea:",
"counter_format": "{count} / {max}",
"submit_button": "Save this idea",
"show_list_button": "Ideas list",
"form_error": "Unable to save this idea.",
# List view
"list_title": "Ideas",
"new_idea_button": "I have a new idea",
"empty_state": "No ideas yet.",
"delete_label": "delete",
"delete_confirm": "Delete this idea?",
# HTML meta
"html_lang": "en",
},
"fr": {
# Window title
"window_title": "Memo Tori",
# Form view
"textarea_placeholder": "Ton idée :",
"counter_format": "{count} / {max}",
"submit_button": "Sauvegarder cette idée",
"show_list_button": "Liste des idées",
"form_error": "Impossible d'enregistrer cette idée.",
# List view
"list_title": "Idées",
"new_idea_button": "J'ai une nouvelle idée",
"empty_state": "Aucune idée pour le moment.",
"delete_label": "effacer",
"delete_confirm": "Effacer cette idée ?",
# HTML meta
"html_lang": "fr",
}
}
def detect_language():
"""
Detect the user's preferred language.
Returns:
str: Language code ('en' or 'fr')
"""
# Check environment variable first
env_lang = os.environ.get("MEMO_TORI_LANG", "").lower().strip()
if env_lang in LANGUAGES:
return env_lang
# Try to detect from system locale
try:
system_locale, _ = locale.getdefaultlocale()
if system_locale:
# Extract language code (e.g., 'fr_FR' -> 'fr')
lang_code = system_locale.split('_')[0].lower()
if lang_code in LANGUAGES:
return lang_code
except (ValueError, AttributeError):
pass
# Fallback to default
return DEFAULT_LANGUAGE
def get_translations(lang=None):
"""
Get translations for the specified language.
Args:
lang (str, optional): Language code. If None, auto-detect.
Returns:
dict: Translation dictionary
"""
if lang is None:
lang = detect_language()
return TRANSLATIONS.get(lang, TRANSLATIONS[DEFAULT_LANGUAGE])
def translate(key, lang=None, **kwargs):
"""
Translate a single key.
Args:
key (str): Translation key
lang (str, optional): Language code. If None, auto-detect.
**kwargs: Format parameters for the translation string
Returns:
str: Translated string
"""
translations = get_translations(lang)
text = translations.get(key, key)
if kwargs:
try:
return text.format(**kwargs)
except (KeyError, ValueError):
return text
return text
# Convenience alias
t = translate