-
-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathmain.py
224 lines (201 loc) · 8.57 KB
/
main.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import os
import google.generativeai as genai
from textual.app import App, ComposeResult
from textual.widgets import Header, Footer, Input, Button, Static
from textual.containers import Container
from textual.binding import Binding
import polib
from pathlib import Path
import subprocess
class TranslationApp(App):
BINDINGS = [
Binding("left", "previous", "Previous Entry"),
Binding("right", "next", "Next Entry"),
Binding("enter", "accept", "Accept Translation"),
Binding("ctrl+s", "save", "Save Changes"),
Binding("ctrl+g", "generate", "Generate Translation"),
]
CSS = """
#status-container {
height: auto;
padding: 1;
background: $accent-darken-2;
color: $text;
text-align: center;
text-style: bold;
}
#status-line {
text-style: bold;
}
#translation-container {
layout: grid;
grid-size: 4;
grid-columns: 1fr;
padding: 1;
}
.section-label {
text-align: center;
background: $accent;
color: $text;
padding: 1;
border: solid $background;
}
.text-content {
background: $surface;
color: $text;
padding: 1;
border: solid $background;
height: auto;
min-height: 5;
max-height: 100;
overflow-y: auto;
}
.translation-input {
width: 100%;
min-height: 5;
height: auto;
}
#button-container {
layout: horizontal;
height: auto;
align: center middle;
padding: 1;
}
"""
def __init__(self, po_file_path: Path):
super().__init__()
self.po_file_path = po_file_path
self.po_file = polib.pofile(str(po_file_path))
# Preserve original wrapping format
self.po_file.wrapwidth = 0 # Disable wrapping to maintain original format
self.current_entry_index = 0
@staticmethod
def translate_text(prompt: str) -> str:
import textwrap
genai.configure(api_key=os.getenv('GEMINI_API'))
model = genai.GenerativeModel("gemini-1.5-flash")
response = model.generate_content(prompt)
# Wrap text at 72 characters, which is a common standard for PO files
wrapped_text = textwrap.fill(response.text, width=72)
return wrapped_text
def compose(self) -> ComposeResult:
yield Header()
yield Container(
Static(id="status-line"),
id="status-container"
)
yield Container(
Static("Source Text", classes="section-label"),
Static(id="source-text", classes="text-content"),
Static("Raw Translation", classes="section-label"),
Static(id="raw-translation", classes="text-content"),
Static("New Translation", classes="section-label"),
Input(classes="translation-input", id="translation-input"),
Container(
Button("Previous [←]", id="prev-btn"),
Button("Accept [↵]", id="accept-btn"),
Button("Next [→]", id="next-btn"),
Button("Generate [^G]", id="generate-btn"),
Button("Save [^S]", id="save-btn", variant="primary"),
id="button-container"
),
id="translation-container"
)
yield Footer()
def on_mount(self) -> None:
self.show_current_entry()
def show_current_entry(self) -> None:
if self.current_entry_index < len(self.po_file):
entry = self.po_file[self.current_entry_index]
status = f"File: {self.po_file_path.name} | Entry: {self.current_entry_index + 1}/{len(self.po_file)}"
self.query_one("#status-line").update(status)
self.query_one("#source-text").update(entry.msgid)
self.query_one("#raw-translation").update(entry.msgstr)
async def on_button_pressed(self, event: Button.Pressed) -> None:
if event.button.id == "accept-btn":
translation = self.query_one("#translation-input").value
if translation:
self.po_file[self.current_entry_index].msgstr = translation
self.navigate_entry(1)
elif event.button.id == "next-btn":
self.navigate_entry(1)
elif event.button.id == "prev-btn":
self.navigate_entry(-1)
elif event.button.id == "generate-btn":
entry = self.po_file[self.current_entry_index]
try:
prompt = ("Translate the following Python documentation into Traditional Chinese"
f"for {self.po_file_path}:{entry.occurrences} with message {entry.msgid}. Ensure "
"that the translation is accurate and uses appropriate technical terminology. The"
" output must be in Traditional Chinese. Pay careful attention to context, idiomatic"
" expressions, and any specialized vocabulary related to Python programming. Maintain "
"the structure and format of the original documentation as much as possible to ensure"
" clarity and usability for readers.")
translation = self.translate_text(prompt)
if translation:
self.query_one("#translation-input").value = translation
self.notify("Translation generated!")
else:
self.notify("Failed to generate translation", severity="error")
except Exception as e:
self.notify(f"Error: {str(e)}", severity="error")
elif event.button.id == "save-btn":
self.po_file.save(str(self.po_file_path))
try:
subprocess.run(['powrap', str(self.po_file_path)], check=True)
self.notify("Changes saved and wrapped successfully!")
except subprocess.CalledProcessError:
self.notify("Save successful, but powrap failed!", severity="error")
except FileNotFoundError:
self.notify("Save successful, but powrap not found!", severity="warning")
def navigate_entry(self, direction: int) -> None:
new_index = self.current_entry_index + direction
if 0 <= new_index < len(self.po_file):
self.current_entry_index = new_index
self.show_current_entry()
self.query_one("#translation-input").value = ""
def action_previous(self) -> None:
self.navigate_entry(-1)
def action_next(self) -> None:
self.navigate_entry(1)
def action_accept(self) -> None:
translation = self.query_one("#translation-input").value
if translation:
self.po_file[self.current_entry_index].msgstr = translation
self.navigate_entry(1)
def action_save(self) -> None:
self.po_file.save(str(self.po_file_path))
try:
subprocess.run(['powrap', str(self.po_file_path)], check=True)
self.notify("Changes saved and wrapped successfully!")
except subprocess.CalledProcessError:
self.notify("Save successful, but powrap failed!", severity="error")
except FileNotFoundError:
self.notify("Save successful, but powrap not found!", severity="warning")
def action_generate(self) -> None:
entry = self.po_file[self.current_entry_index]
try:
prompt = ("Translate the following Python documentation into Traditional Chinese"
f"for {self.po_file_path}:{entry.occurrences} with message {entry.msgid[1]}. Ensure "
"that the translation is accurate and uses appropriate technical terminology. The"
" output must be in Traditional Chinese. Pay careful attention to context, idiomatic"
" expressions, and any specialized vocabulary related to Python programming. Maintain "
"the structure and format of the original documentation as much as possible to ensure"
" clarity and usability for readers.")
translation = self.translate_text(prompt)
if translation:
self.query_one("#translation-input").value = translation
self.notify("Translation generated!")
else:
self.notify("Failed to generate translation", severity="error")
except Exception as e:
self.notify(f"Error: {str(e)}", severity="error")
def main(po_file_path: str):
app = TranslationApp(Path(po_file_path))
app.run()
if __name__ == "__main__":
import sys
if len(sys.argv) > 1:
main(sys.argv[1])
else:
print("Please provide a PO file path")