You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Use additional context in LLM prompt, update OpenAI dep
- Update OpenAI package to latest version (2.29.0)
- Improve prompt formulation
- Pass string ID, comment, terminology matches when available
- Move OpenAI GPT version to settings
f"The target language '{target_language_name}' is not supported."
40
62
)
41
63
42
-
intro_text=f"Refine the {target_language} machine translation below to make it {characteristic}."
43
-
44
-
common_rules=textwrap.dedent(
45
-
"""Follow these rules IN ORDER OF PRIORITY:
46
-
1) ENDING PUNCTUATION — SEMANTICS, NOT LITERAL CHAR:
47
-
- Detect the English ending: none, ".", "?", "!", "…".
48
-
- The translation MUST express the same ending SEMANTIC:
49
-
• if English ends with "?" → translation ends with a question.
50
-
• if English ends with "!" → translation ends with an exclamation.
51
-
• if English ends with "…" → translation ends with an ellipsis.
52
-
• if English has NO closing punctuation → translation MUST NOT end with ".", "?", "!", or "…".
53
-
- Do not add a final period if the English has none.
54
-
- Respect orthographic and typographic rules of the target language regarding punctuation, like using non-breaking spaces in French, adding opening "¿" or "¡" in Spanish, etc.
55
-
2) Preserve all HTML tags and their order. Do not add, remove, or reorder tags."""
"formal": f"Use formal {target_language_name} ({target_language.code}) throughout; maintain a consistent register and do not mix formal and informal modes.",
67
+
"rephrased": f"Provide an alternative wording that preserves the original meaning; adapt idioms and culturally marked expressions for {target_language_name} ({target_language.code}); you may restructure sentences but must not introduce new information or omit essential meaning.",
68
+
}
57
69
58
-
informal=textwrap.dedent(
59
-
f"""{intro_text}
60
-
Revise the {target_language} translation to use simpler language.
61
-
{common_rules}
62
-
3) Clarity and Simplicity: keep wording straightforward and consistent.
You are an expert {target_language_name} ({target_language.code}) localization specialist.
111
+
112
+
Your task: produce a {characteristic}{target_language_name} ({target_language.code}) translation of a UI string.
113
+
Use the provided machine translation as a reference, but you are not bound by it — rewrite freely to achieve the best result.
114
+
"""
74
115
)
75
116
76
-
rephrased=textwrap.dedent(
77
-
f"""{intro_text}
78
-
Provide an alternative translation that preserves the original meaning while varying the wording.
79
-
{common_rules}
80
-
3) Cultural and Idiomatic Fit: adapt idioms and culturally marked expressions appropriately for {target_language}; you may restructure sentences but must not introduce new information or omit essential meaning.
81
-
4) Clarity and Naturalness: ensure the result reads naturally and is easy to understand.
82
-
Output only the alternative translation."""
117
+
context_instructions= []
118
+
ifstring_id:
119
+
context_instructions.append(
120
+
"STRING ID: use it to infer the UI context (e.g., button, menu item, page title, tooltip) and adapt length and phrasing accordingly."
121
+
)
122
+
ifresource_comment:
123
+
context_instructions.append(
124
+
"RESOURCE COMMENT: general notes about the file — use it as additional context."
125
+
)
126
+
ifgroup_comment:
127
+
context_instructions.append(
128
+
"GROUP COMMENT: notes about the group of messages this string belongs to — use it as additional context."
129
+
)
130
+
ifstring_comment:
131
+
context_instructions.append(
132
+
"STRING COMMENT: treat it as authoritative translator notes — it may specify placeholders to preserve exactly, terms that must not be translated, or other constraints. STRING COMMENT requirements take precedence over all stylistic choices."
133
+
)
134
+
ifpinned_comments:
135
+
context_instructions.append(
136
+
"PINNED COMMENTS: this is a comment added by a project manager — treat them as high-priority guidance from the localization team."
137
+
)
138
+
ifterms:
139
+
context_instructions.append(
140
+
"TERMINOLOGY: use the given translations for those terms consistently in your output, unless you believe the existing translation to be incorrect for the context."
Your goal is to produce a natural, grammatically correct translation. Follow these rules strictly; if rules conflict, earlier rules take priority.
149
+
1) ENDING PUNCTUATION — PRESERVE SEMANTICS
150
+
- Determine the ending punctuation of the English source text (ignore trailing spaces and HTML tags).
151
+
- The translation MUST end with the equivalent punctuation. Both directions are hard constraints:
152
+
• English ends with "." → translation MUST end with "." (or target-language equivalent)
153
+
• English ends with "?" → translation MUST end with a question mark
154
+
• English ends with "!" → translation MUST end with an exclamation mark
155
+
• English ends with "…" → translation MUST end with an ellipsis
156
+
• English has NO ending punctuation → translation MUST NOT end with ".", "?", "!", or "…"
157
+
- Apply correct punctuation conventions for the target language (e.g. Spanish "¿ ¡", French non-breaking space before "?", "!", ":").
158
+
2) HTML TAGS
159
+
- Preserve all HTML tags exactly as in the source:
160
+
- Do not add, remove, reorder, or modify tags or attributes
161
+
- Translate only the text content between tags, or the attributes if they contain translatable text (e.g., "alt", "title").
162
+
- Keep punctuation placement consistent with the source structure (do not move punctuation across tag boundaries unless required by the target language grammar).
163
+
3) {style_goal}
164
+
165
+
Output only the translation, with no explanation."""
Transforms and returns text using GPT-4 based on specified characteristics like rephrasing or changing formality, by fetching English text, its machine translation, desired transformation characteristic, and target language from the request.
147
+
Transforms and returns text using GPT based on specified characteristics like rephrasing or changing formality, by fetching English text, its machine translation, desired transformation characteristic, and target language from the request.
0 commit comments