Hugoifier

hugoifier / hugoifier / utils / translate.py
Blame History Raw 26 lines
1
"""
2
Translates web content using the configured AI backend.
3
"""
4
5
import logging
6
7
from ..config import call_ai
8
9
10
def translate(path: str, target_language: str = "Spanish") -> str:
11
logging.info(f"Translating content in {path} ...")
12
try:
13
with open(path, 'r', errors='replace') as f:
14
content = f.read()
15
16
prompt = f"""Translate the following web content to {target_language}.
17
Preserve all HTML tags and formatting. Only translate visible text.
18
19
Content:
20
{content[:20000]}"""
21
22
return call_ai(prompt, "You are a professional translator.")
23
except Exception as e:
24
logging.error(f"Translation failed: {e}")
25
return f"Translation failed: {e}"
26

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button