pydefine 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pydefine/i18n.py ADDED
@@ -0,0 +1,344 @@
1
+ """
2
+ pydefine.i18n
3
+ ~~~~~~~~~~~~~
4
+
5
+ Internationalization (i18n) support for pyDefine.
6
+
7
+ Provides translation capabilities for error explanations.
8
+ Currently includes stub for Hinglish (Hindi + English mix) translation.
9
+
10
+ Usage:
11
+ from pydefine.i18n import set_language, translate_explanation
12
+
13
+ set_language('hi') # Hinglish
14
+ translated = translate_explanation(explanation, 'hi')
15
+ """
16
+
17
+ import re
18
+ from typing import Dict, Optional
19
+
20
+ # Current language (default: English)
21
+ _current_language = "en"
22
+
23
+ # Supported languages
24
+ SUPPORTED_LANGUAGES = {
25
+ "en": "English",
26
+ "hi": "Hinglish (Hindi + English)",
27
+ }
28
+
29
+
30
+ def set_language(language_code: str) -> bool:
31
+ """
32
+ Set the current language for translations.
33
+
34
+ Args:
35
+ language_code: Language code ('en', 'hi', etc.)
36
+
37
+ Returns:
38
+ True if language is supported, False otherwise
39
+
40
+ Example:
41
+ >>> set_language('hi')
42
+ True
43
+ >>> set_language('fr')
44
+ False
45
+ """
46
+ global _current_language
47
+
48
+ if language_code in SUPPORTED_LANGUAGES:
49
+ _current_language = language_code
50
+ return True
51
+ else:
52
+ print(f"⚠️ Warning: Language '{language_code}' not supported. Using English.")
53
+ return False
54
+
55
+
56
+ def get_language() -> str:
57
+ """
58
+ Get the current language code.
59
+
60
+ Returns:
61
+ Current language code (e.g., 'en', 'hi')
62
+ """
63
+ return _current_language
64
+
65
+
66
+ def translate_explanation(text: str, target_language: str) -> str:
67
+ """
68
+ Translate an error explanation to target language.
69
+
70
+ Args:
71
+ text: English explanation text
72
+ target_language: Target language code
73
+
74
+ Returns:
75
+ Translated text (or original if translation not available)
76
+
77
+ Example:
78
+ >>> text = "You tried to divide by zero"
79
+ >>> translate_explanation(text, 'hi')
80
+ "Aapne zero se divide karne ki koshish ki"
81
+ """
82
+ if target_language == "en":
83
+ return text
84
+
85
+ if target_language == "hi":
86
+ return _translate_to_hinglish(text)
87
+
88
+ # Fallback to English
89
+ return text
90
+
91
+
92
+ def _translate_to_hinglish(text: str) -> str:
93
+ """
94
+ Translate English text to Hinglish (Hindi + English mix).
95
+
96
+ This is a stub implementation with basic word replacements.
97
+ For production, integrate with a proper translation API.
98
+
99
+ Args:
100
+ text: English text
101
+
102
+ Returns:
103
+ Hinglish text
104
+ """
105
+ # Basic word-level replacements (stub implementation)
106
+ replacements = {
107
+ # Common programming terms (keep English)
108
+ "Python": "Python",
109
+ "code": "code",
110
+ "function": "function",
111
+ "variable": "variable",
112
+ "file": "file",
113
+ "error": "error",
114
+ "exception": "exception",
115
+
116
+ # Verbs and actions
117
+ "You tried": "Aapne koshish ki",
118
+ "You wrote": "Aapne likha",
119
+ "You pressed": "Aapne press kiya",
120
+ "tried to": "koshish ki",
121
+ "doesn't exist": "exist nahi karta",
122
+ "doesn't follow": "follow nahi karta",
123
+ "couldn't find": "nahi mil saka",
124
+ "can't find": "nahi mil sakta",
125
+ "doesn't have": "ke paas nahi hai",
126
+
127
+ # Nouns
128
+ "the language rules": "language ke rules",
129
+ "the wrong type": "galat type",
130
+ "a dictionary": "ek dictionary",
131
+ "a list": "ek list",
132
+ "a file": "ek file",
133
+ "a folder": "ek folder",
134
+ "the program": "program",
135
+ "the computer": "computer",
136
+
137
+ # Descriptors
138
+ "wrong": "galat",
139
+ "correct": "sahi",
140
+ "invalid": "invalid",
141
+ "missing": "missing",
142
+ "too big": "bahut bada",
143
+ "too long": "bahut lamba",
144
+
145
+ # Common phrases
146
+ "It's like": "Yeh aisa hai jaise",
147
+ "This is": "Yeh hai",
148
+ "Something went wrong": "Kuch galat ho gaya",
149
+ "Check": "Check karein",
150
+ "Make sure": "Pakka karein",
151
+ "Use": "Use karein",
152
+ "Install": "Install karein",
153
+ }
154
+
155
+ translated = text
156
+
157
+ # Apply replacements (longer phrases first to avoid partial matches)
158
+ for english, hinglish in sorted(replacements.items(), key=lambda x: -len(x[0])):
159
+ translated = translated.replace(english, hinglish)
160
+
161
+ return translated
162
+
163
+
164
+ def get_supported_languages() -> Dict[str, str]:
165
+ """
166
+ Get dictionary of supported language codes and names.
167
+
168
+ Returns:
169
+ Dictionary mapping language codes to full names
170
+
171
+ Example:
172
+ >>> langs = get_supported_languages()
173
+ >>> print(langs['hi'])
174
+ 'Hinglish (Hindi + English)'
175
+ """
176
+ return SUPPORTED_LANGUAGES.copy()
177
+
178
+
179
+ def is_language_supported(language_code: str) -> bool:
180
+ """
181
+ Check if a language is supported.
182
+
183
+ Args:
184
+ language_code: Language code to check
185
+
186
+ Returns:
187
+ True if supported, False otherwise
188
+ """
189
+ return language_code in SUPPORTED_LANGUAGES
190
+
191
+
192
+ # Translation templates for common error messages
193
+ # This can be expanded with full translations in future versions
194
+ TRANSLATION_TEMPLATES = {
195
+ "hi": {
196
+ "SyntaxError": {
197
+ "simple_explanation": (
198
+ "Aapne Python code likha jo language ke rules follow nahi karta. "
199
+ "Python samajh nahi saka ki aapne kya type kiya. "
200
+ "Yeh aisa hai jaise galat grammar ke saath sentence likhna 📝"
201
+ ),
202
+ "fix_suggestion": (
203
+ "Missing colons (:), unmatched parentheses/brackets check karein, "
204
+ "ya keywords jaise 'if', 'for', 'def' mein typos dekhe"
205
+ )
206
+ },
207
+
208
+ "NameError": {
209
+ "simple_explanation": (
210
+ "Aapne ek variable ya function name use karne ki koshish ki jo abhi tak exist nahi karta. "
211
+ "Python ko samajh nahi aa raha aap kya bol rahe ho. "
212
+ "Yeh aisa hai jaise kisi ko bulana jo kamre mein nahi hai 🤷"
213
+ ),
214
+ "fix_suggestion": (
215
+ "Spelling check karein, pakka karein ki aapne pehle variable define kiya hai, "
216
+ "ya module import karein jisme yeh hai"
217
+ )
218
+ },
219
+
220
+ "ZeroDivisionError": {
221
+ "simple_explanation": (
222
+ "Aapne ek number ko zero se divide karne ki koshish ki, jo math mein impossible hai. "
223
+ "Zero se division universe ke math rules ko tod deta hai. "
224
+ "Python infinity calculate nahi kar sakta ➗"
225
+ ),
226
+ "fix_suggestion": (
227
+ "Divide karne se pehle check karein ki divisor zero toh nahi hai, "
228
+ "ya zero values handle karne ke liye condition add karein"
229
+ )
230
+ },
231
+
232
+ "FileNotFoundError": {
233
+ "simple_explanation": (
234
+ "Python ko woh file nahi mili jise aap open ya use karne ki koshish kar rahe ho. "
235
+ "File aapke diye gaye path par exist nahi karti. "
236
+ "Yeh aisa hai jaise ek book dhundhna jo shelf par nahi hai 📁"
237
+ ),
238
+ "fix_suggestion": (
239
+ "File path ki spelling check karein, verify karein ki file exist karti hai, "
240
+ "ya file create karein agar honi chahiye"
241
+ )
242
+ },
243
+
244
+ "TypeError": {
245
+ "simple_explanation": (
246
+ "Aapne galat type ke data ke saath kuch karne ki koshish ki. "
247
+ "Jaise ek number ko ek word ke saath add karna - yeh mix nahi hote. "
248
+ "Python ko compatible types chahiye saath kaam karne ke liye 🔢➕📝"
249
+ ),
250
+ "fix_suggestion": (
251
+ "Data ko sahi type mein convert karein (e.g., int(), str(), list()), "
252
+ "ya check karein ki aap sahi operation use kar rahe ho"
253
+ )
254
+ },
255
+
256
+ "ValueError": {
257
+ "simple_explanation": (
258
+ "Data type toh sahi hai par actual value sense nahi bana rahi. "
259
+ "Jaise 'hello' word ko number mein convert karne ki koshish karna. "
260
+ "Format ya content galat hai 🎯"
261
+ ),
262
+ "fix_suggestion": (
263
+ "Check karein ki input value valid hai jo aap karne ki koshish kar rahe ho. "
264
+ "Bad values handle karne ke liye validation ya try/except add karein"
265
+ )
266
+ },
267
+
268
+ "KeyError": {
269
+ "simple_explanation": (
270
+ "Aapne ek dictionary se ek key use karke value lene ki koshish ki jo exist nahi karti. "
271
+ "Yeh aisa hai jaise dictionary mein ek word dhundhna jo hai hi nahi. "
272
+ "Aapne jo key maangi woh missing hai 🔑"
273
+ ),
274
+ "fix_suggestion": (
275
+ "dict[key] ke bajaye dict.get(key, default) use karein, "
276
+ "ya 'key in dict' se pehle check karein ki key exist karti hai"
277
+ )
278
+ },
279
+
280
+ "IndexError": {
281
+ "simple_explanation": (
282
+ "Aapne list mein ek position access karne ki koshish ki jo exist nahi karta. "
283
+ "Jaise 10th item lena jab sirf 5 items hain. "
284
+ "Index range se bahar hai 📊"
285
+ ),
286
+ "fix_suggestion": (
287
+ "Access karne se pehle len() se length check karein, "
288
+ "ya end se count karne ke liye negative indices use karein"
289
+ )
290
+ },
291
+
292
+ "ImportError": {
293
+ "simple_explanation": (
294
+ "Python aapke request kiye gaye module ko import nahi kar saka. "
295
+ "Package shayad installed nahi hai ya naam galat hai. "
296
+ "Import fail ho gaya 📦"
297
+ ),
298
+ "fix_suggestion": (
299
+ "Package ko pip install se install karein, spelling check karein, "
300
+ "ya verify karein ki module aapke environment mein exist karta hai"
301
+ )
302
+ },
303
+
304
+ "AttributeError": {
305
+ "simple_explanation": (
306
+ "Aapne ek property ya method access karne ki koshish ki jo object par exist nahi karta. "
307
+ "Jis cheez ke saath aap kaam kar rahe ho uske paas woh feature nahi hai. "
308
+ "Yeh aisa hai jaise car ka door kholna jo hai hi nahi 🚗"
309
+ ),
310
+ "fix_suggestion": (
311
+ "dir(object) use karke sahi attribute name check karein, "
312
+ "ya verify karein ki object type wahi hai jo aap expect kar rahe ho"
313
+ )
314
+ },
315
+ }
316
+ }
317
+
318
+
319
+ def get_translated_template(error_type: str, language_code: str) -> Optional[Dict[str, str]]:
320
+ """
321
+ Get translated template for an error type.
322
+
323
+ Args:
324
+ error_type: Exception class name
325
+ language_code: Target language code
326
+
327
+ Returns:
328
+ Dictionary with translated explanation and fix, or None
329
+ """
330
+ if language_code in TRANSLATION_TEMPLATES:
331
+ return TRANSLATION_TEMPLATES[language_code].get(error_type)
332
+ return None
333
+
334
+
335
+ # Export public API
336
+ __all__ = [
337
+ 'set_language',
338
+ 'get_language',
339
+ 'translate_explanation',
340
+ 'get_supported_languages',
341
+ 'is_language_supported',
342
+ 'get_translated_template',
343
+ 'SUPPORTED_LANGUAGES',
344
+ ]