File size: 1,140 Bytes
f3dd5d6
 
 
 
 
 
 
 
 
 
 
01dc73d
f3dd5d6
 
 
 
 
 
01dc73d
 
 
 
f3dd5d6
 
 
 
 
 
 
 
 
 
 
 
 
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
from google import genai
from google.genai import types


SYS_PROMPT_TRANSLATION = """
You are an expert translator.
Your sole purpose is to accurately and faithfully translate the provided text into the [TARGET_LANGUAGE].
Do not add any extra information, explanations, or stylistic changes.
Maintain the original meaning and tone as closely as possible.
"""

def get_translaton(text: str, api_key: str, target_language: str) -> str:

	if not api_key:
		return "Error: Gemini API Key not found."
	if not text:
		return "Error: text not found."

	try:
		client = genai.Client(api_key=api_key)
	except Exception as e:
		return f"ERROR: {str(e)}"

	lang_map = {"Deutsch": "German", "English": "English", "Français": "French", "Русский язык": "Russain", "中文": "Chinese"}
	tar_lang = lang_map.get(target_language, "English")
	sys_prompt = SYS_PROMPT_TRANSLATION.replace("[TARGET_LANGUAGE]", tar_lang)
	response = client.models.generate_content(
		model="gemini-2.5-flash-preview-05-20",
		config=types.GenerateContentConfig(
			system_instruction=sys_prompt,
			temperature=0.1,
			),
		contents=[text]
	)
	return response.text