@@ -97,6 +97,25 @@ def translate(self, content: str, output_type: str) -> str:
9797 Returns:
9898 Translated markdown content
9999 """
100+ # Validate input content
101+ if content is None :
102+ raise ValueError (
103+ f"Input content is None. "
104+ f"Cannot translate empty content."
105+ )
106+
107+ if not isinstance (content , str ):
108+ raise TypeError (
109+ f"Input content must be a string, got { type (content ).__name__ } ."
110+ )
111+
112+ content_str = content .strip ()
113+ if not content_str or len (content_str ) == 0 :
114+ raise ValueError (
115+ f"Input content is empty or contains only whitespace. "
116+ f"Content length: { len (content )} chars (after strip: { len (content_str )} chars)."
117+ )
118+
100119 # Load appropriate prompt based on target language
101120 if self .target_language == "zh-TW" :
102121 prompt_template = self ._load_translation_prompt (ZH_TW_PROMPT_FILE )
@@ -108,28 +127,35 @@ def translate(self, content: str, output_type: str) -> str:
108127 )
109128
110129 # Validate prompt template
111- if not prompt_template or len (prompt_template .strip ()) == 0 :
130+ prompt_template_str = prompt_template .strip () if prompt_template else ""
131+ if not prompt_template or len (prompt_template_str ) == 0 :
132+ prompt_file = ZH_TW_PROMPT_FILE if self .target_language == "zh-TW" else GENERIC_PROMPT_FILE
112133 raise ValueError (
113- f"Translation prompt template is empty. "
114- f"Target language: { self .target_language } , "
115- f"Prompt file: { ZH_TW_PROMPT_FILE if self .target_language == 'zh-TW' else GENERIC_PROMPT_FILE } "
134+ f"Translation prompt template is empty or contains only whitespace.\n "
135+ f" Target language: { self .target_language } \n "
136+ f" Prompt file: { prompt_file } \n "
137+ f" Template length: { len (prompt_template ) if prompt_template else 0 } chars "
138+ f"(after strip: { len (prompt_template_str )} chars)\n "
139+ f" Check if prompt file exists and contains valid content."
116140 )
117141
118142 # Build full prompt with content
119- prompt = f"""{ prompt_template }
143+ prompt = f"""{ prompt_template_str }
120144
121145---
122146
123147## Content to Translate
124148
125- { content } """
149+ { content_str } """
126150
127151 # Validate full prompt
128- if not prompt or len (prompt .strip ()) == 0 :
152+ prompt_str = prompt .strip ()
153+ if not prompt or len (prompt_str ) == 0 :
129154 raise ValueError (
130- f"Built prompt is empty. "
131- f"Template length: { len (prompt_template )} , "
132- f"Content length: { len (content )} "
155+ f"Built prompt is empty or contains only whitespace.\n "
156+ f" Template length: { len (prompt_template_str )} chars\n "
157+ f" Content length: { len (content_str )} chars\n "
158+ f" Combined prompt length: { len (prompt )} chars (after strip: { len (prompt_str )} chars)"
133159 )
134160
135161 messages = self ._build_messages (prompt )
@@ -140,7 +166,7 @@ def translate(self, content: str, output_type: str) -> str:
140166 # Show progress info
141167 model_name = self .model_config .get ("model" , "unknown" )
142168 prompt_size = len (prompt )
143- content_size = len (content )
169+ content_size = len (content_str )
144170 print (f" 📤 Sending request to { model_name } ..." )
145171 print (f" Prompt: { prompt_size :,} chars, Content: { content_size :,} chars" )
146172 print (f" This may take 30-120 seconds depending on content size..." )
0 commit comments