Skip to content

Commit 51d3422

Browse files
committed
smarter language evaluation in translate function. some credit to thecommcraft
1 parent a2b87ef commit 51d3422

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

scratchattach/other/other_apis.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,24 @@ def scratch_team_members() -> dict:
149149

150150

151151
def translate(language: str | Languages, text: str = "hello"):
152-
if language.lower() not in Languages.all_of("code", str.lower):
153-
if language.lower() in Languages.all_of("name", str.lower):
154-
language = Languages.find(language.lower(), apply_func=str.lower).code
152+
lang = language
153+
if isinstance(language, str):
154+
if language.lower() in Languages.all_of("code", str.lower):
155+
lang = Languages.find(language.lower(), "code", apply_func=str.lower)
156+
157+
elif language.lower() in Languages.all_of("name", str.lower):
158+
lang = Languages.find(language.lower(), apply_func=str.lower)
159+
160+
elif isinstance(language, Languages):
161+
lang = language.value
162+
else:
163+
# The code will work so long as the language has a 'code' attribute, however, this is bad practice.
164+
warnings.warn(f"{language} is not {str} or {Languages}, but {type(language)}.")
155165

156-
lang = Languages.find(language, "code", str.lower)
157166
if lang is None:
158167
raise ValueError(f"{language} is not a supported translate language")
159168

169+
print(lang.__dict__)
160170
response_json = requests.get(
161171
f"https://translate-service.scratch.mit.edu/translate?language={lang.code}&text={text}").json()
162172

scratchattach/utils/supportedlangs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ def apply_func(x):
116116

117117
for lang_enum in Languages:
118118
lang = lang_enum.value
119+
119120
try:
120121
if apply_func(getattr(lang, by)) == value:
121122
return lang

0 commit comments

Comments
 (0)