dmartincy commited on
Commit
71aa48d
·
1 Parent(s): 8304957

Add translation pairs

Browse files
Files changed (1) hide show
  1. document-authoring.js +24 -14
document-authoring.js CHANGED
@@ -7,7 +7,15 @@ app.innerHTML = `
7
  <span class="spinner"></span> Translation services starting...
8
  </div>
9
  <div id="translationControls">
10
- <select id="languageSelect">
 
 
 
 
 
 
 
 
11
  <option value="Spanish">Spanish</option>
12
  <option value="French">French</option>
13
  <option value="German">German</option>
@@ -120,7 +128,7 @@ script.onload = async () => {
120
  return groups;
121
  }
122
 
123
- async function translate(content, lang) {
124
  try {
125
  const response = await fetch('/client/api/v1/chat/completions', {
126
  method: 'POST',
@@ -131,7 +139,7 @@ script.onload = async () => {
131
  messages: [
132
  {
133
  role: "system",
134
- content: `Translate the following text to ${lang}. Each segment is marked with ||| delimiters. Translate each segment independently while preserving original capitalization and punctuation. Add spaces at the start or end of segments only if they exist in the original text.`
135
  },
136
  {
137
  role: "user",
@@ -165,27 +173,21 @@ script.onload = async () => {
165
  }
166
 
167
  // Enhanced recursive translation function
168
- async function translateRecursive(docJson, lang) {
169
  const textRuns = flattenTextRuns(docJson);
170
  const groups = groupAdjacentRuns(textRuns);
171
 
172
  for (let group of groups) {
173
- // Prepare content for translation, preserving spaces
174
  const content = group.map(run => `|||${run.text}|||`).join('\n');
 
175
 
176
- // Get translations
177
- const translatedContent = await translate(content, lang);
178
-
179
- // Parse translations
180
  const translations = translatedContent
181
  .split('|||')
182
  .filter(t => t.trim())
183
  .map(t => t.trim());
184
 
185
- // Update document
186
  group.forEach((run, index) => {
187
  if (translations[index]) {
188
- // Preserve original leading/trailing spaces
189
  const originalText = run.text;
190
  let translatedText = translations[index];
191
 
@@ -204,16 +206,24 @@ script.onload = async () => {
204
 
205
  // Add translation button handler
206
  const translateButton = document.getElementById('translateButton');
207
- const languageSelect = document.getElementById('languageSelect');
 
208
 
209
  translateButton.addEventListener('click', async () => {
210
  try {
211
- const selectedLanguage = languageSelect.value;
 
 
 
 
 
 
 
212
  translateButton.disabled = true;
213
  translateButton.innerHTML = '<span class="spinner"></span> Translating...';
214
 
215
  const jsonDoc = await editor.currentDocument().saveDocument();
216
- await translateRecursive(jsonDoc.container.document, selectedLanguage);
217
  const translatedDoc = await docAuthSystem.loadDocument(jsonDoc);
218
  editor.setCurrentDocument(translatedDoc);
219
  } catch (error) {
 
7
  <span class="spinner"></span> Translation services starting...
8
  </div>
9
  <div id="translationControls">
10
+ <select id="sourceLanguageSelect">
11
+ <option value="English" selected>English</option>
12
+ <option value="Spanish">Spanish</option>
13
+ <option value="French">French</option>
14
+ <option value="German">German</option>
15
+ </select>
16
+ <span style="margin: 0 10px;">→</span>
17
+ <select id="targetLanguageSelect">
18
+ <option value="English">English</option>
19
  <option value="Spanish">Spanish</option>
20
  <option value="French">French</option>
21
  <option value="German">German</option>
 
128
  return groups;
129
  }
130
 
131
+ async function translate(content, targetLang, sourceLang = 'English') {
132
  try {
133
  const response = await fetch('/client/api/v1/chat/completions', {
134
  method: 'POST',
 
139
  messages: [
140
  {
141
  role: "system",
142
+ content: `Translate the following text from ${sourceLang} to ${targetLang}. Each segment is marked with ||| delimiters. Translate each segment independently while preserving original capitalization and punctuation. Add spaces at the start or end of segments only if they exist in the original text.`
143
  },
144
  {
145
  role: "user",
 
173
  }
174
 
175
  // Enhanced recursive translation function
176
+ async function translateRecursive(docJson, targetLang, sourceLang = 'English') {
177
  const textRuns = flattenTextRuns(docJson);
178
  const groups = groupAdjacentRuns(textRuns);
179
 
180
  for (let group of groups) {
 
181
  const content = group.map(run => `|||${run.text}|||`).join('\n');
182
+ const translatedContent = await translate(content, targetLang, sourceLang);
183
 
 
 
 
 
184
  const translations = translatedContent
185
  .split('|||')
186
  .filter(t => t.trim())
187
  .map(t => t.trim());
188
 
 
189
  group.forEach((run, index) => {
190
  if (translations[index]) {
 
191
  const originalText = run.text;
192
  let translatedText = translations[index];
193
 
 
206
 
207
  // Add translation button handler
208
  const translateButton = document.getElementById('translateButton');
209
+ const sourceLanguageSelect = document.getElementById('sourceLanguageSelect');
210
+ const targetLanguageSelect = document.getElementById('targetLanguageSelect');
211
 
212
  translateButton.addEventListener('click', async () => {
213
  try {
214
+ const sourceLanguage = sourceLanguageSelect.value;
215
+ const targetLanguage = targetLanguageSelect.value;
216
+
217
+ if (sourceLanguage === targetLanguage) {
218
+ alert('Please select different languages for source and target');
219
+ return;
220
+ }
221
+
222
  translateButton.disabled = true;
223
  translateButton.innerHTML = '<span class="spinner"></span> Translating...';
224
 
225
  const jsonDoc = await editor.currentDocument().saveDocument();
226
+ await translateRecursive(jsonDoc.container.document, targetLanguage, sourceLanguage);
227
  const translatedDoc = await docAuthSystem.loadDocument(jsonDoc);
228
  editor.setCurrentDocument(translatedDoc);
229
  } catch (error) {