Skip to content

Commit cdb8936

Browse files
Prompt improvements.
Fixed translating to IT and many more improvements
1 parent 54ff579 commit cdb8936

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

AndroidResourceTranslator.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
# ------------------------------------------------------------------------------
2222

2323
TRANSLATION_GUIDELINES = """\
24-
Translate the following resource for an Android app provided after the dashed line to {target_language}.
24+
Follow the guidelines below closely.
2525
2626
Guidelines:
2727
1. **Purpose & Context:**
28-
The translation is for an Android application's UI. Use terminology and phrasing consistent with software interfaces.
28+
The translation is for an Android application's UI. Use terminology and phrasing consistent with android applications. Do not change the meaning of the text.
2929
3030
2. **Formatting & Structure:**
3131
- Preserve all placeholders (e.g., %d, %s, %1$s) exactly as they appear.
@@ -51,7 +51,7 @@
5151
5252
6. **Output Requirements:**
5353
Return ONLY the final translated text as a single plain line, preserving any required formatting from the source.
54-
----------
54+
5555
"""
5656

5757
PLURAL_GUIDELINES_ADDITION = """\
@@ -62,13 +62,16 @@
6262
6363
7. **Output Requirements:**
6464
Return ONLY a JSON object containing the translated plural mapping as a single plain line. Do not include any markdown formatting, code blocks, or additional commentary, except if any such formatting is already present in the source string provided.
65-
----------
6665
"""
6766

6867
SYSTEM_MESSAGE_TEMPLATE = """\
6968
You are a software engineer translating textual UI elements within a software application from English into {target_language} while keeping technical terms in English.
7069
"""
7170

71+
TRANSLATE_FINAL_TEXT = """\
72+
Translate the following resource for an Android app provided after the dashed line to the following values-{target_language}/string.xml language: {target_language}
73+
----------
74+
"""
7275
# ------------------------------------------------------------------------------
7376
# Logger Setup
7477
# ------------------------------------------------------------------------------
@@ -381,7 +384,11 @@ def translate_text(text: str, target_language: str, api_key: str, model: str, pr
381384
if text.strip() == "":
382385
return ""
383386

384-
prompt = TRANSLATION_GUIDELINES.format(target_language=target_language) + text
387+
prompt = (
388+
TRANSLATION_GUIDELINES +
389+
TRANSLATE_FINAL_TEXT.format(target_language=target_language) +
390+
text
391+
)
385392
system_message = SYSTEM_MESSAGE_TEMPLATE.format(target_language=target_language)
386393
if project_context:
387394
system_message += f"\nProject context: {project_context}"
@@ -395,8 +402,9 @@ def translate_plural_text(source_plural: Dict[str, str], target_language: str, a
395402
"""
396403
source_json = json.dumps(source_plural, indent=2)
397404
prompt = (
398-
TRANSLATION_GUIDELINES.format(target_language=target_language) +
405+
TRANSLATION_GUIDELINES +
399406
PLURAL_GUIDELINES_ADDITION +
407+
TRANSLATE_FINAL_TEXT.format(target_language=target_language) +
400408
source_json
401409
)
402410
system_message = SYSTEM_MESSAGE_TEMPLATE.format(target_language=target_language)
@@ -490,7 +498,7 @@ def auto_translate_resources(
490498
model=openai_model,
491499
project_context=project_context,
492500
)
493-
logger.info(f"Translated string '{key}': '{source_text}' -> '{translated}'")
501+
logger.info(f"Translated string '{key}' to {lang}: '{source_text}' -> '{translated}'")
494502
if validate_translations:
495503
translated = validate_translation(source_text, translated, target_language=lang)
496504
logger.info(f"Validated string '{key}': now '{translated}'")

0 commit comments

Comments
 (0)