#

Word Addin: Automatische Übersetzung in Google Translate
 
Betrifft: Übersetzung von Word Dokumenten in English
 
Dieser Beitrag enthält ein Word-Addin, mit welchem Texte unter Google Translate übersetzt werden können.
 
Dieses Word Addin kann in Word geladen werden und erscheint dann in der Ribbonbar als eigenes Ribbonbar-Bereich.
Beim Klick auf den Translate-Button in der Leiste wird der Word Text in ein Browser-Fenster übertragen, dann unter Google Translate übersetzt und anschliessend wieder in Word importiert.
Dabei werden die Formatierungen und Bilder und Links beibehalten.
Die aktuelle Sprache ist von Deutsch zu Englisch eingestellt und dient dazu, dass man deutsche Texte ins Englische übersetzten kann.
 
Im Anhang ist das Word Addin zum Download und für Entwickler der Code in C# zur freien Verfügung.

 
Das Programm enthält folgende Classen und Variablen

private string _sSplitSeq = "\n|| ";
private List<WordHyperlink> _arrWordLinks;
private class WordHyperlink
frmTranslate _frmTranslate;

 
und den Methoden

private async void do_translate_all()
//->Hauptmethode
 
private async Task<string> do_Translate_Browser(string sText)
//->Übersetzt den Text im Browser bei Google Translate
 
private void get_Hyperlinks()
//->sammelt Hyperlink Informationen

private string get_Sentences()
//->sammelt Original-Text Informationen
 

 
 
Video Tutorial

 
 
#Betrifft: Google Translate, Bing Translate (Microsoft Word Übersetzung)
Verwendet: C#, VSTO Ribbonbar Addin, Awesomeware Chromium WebBrowser Control
 
Kompletter Code in C# zur Verwendung

#region ------< Region Translate >------
//===================< Buttons >===================
 
private string _sSplitSeq = "\n|| ";
private List<WordHyperlink> _arrWordLinks;

 
private class WordHyperlink
{
public WordHyperlink(int nrSentence = 0, string hlAddress = "", string hlText = "")
{
_nrSentence = nrSentence;
_hlAddress = hlAddress;
_hlText = hlText;
}
 
int _nrSentence = 0;
string _hlAddress = "";
string _hlText = "";
 
public int NrSentence
{
get
{
return _nrSentence;
}
 
set
{
_nrSentence = value;
}
}
public string HlAddress
{
get
{
return _hlAddress;
}
 
set
{
_hlAddress = value;
}
}
public string HlText
{
get
{
return _hlText;
}
 
set
{
_hlText = value;
}
}
}

private async void do_translate_all()
{
//-------------------< do_translate_all() >-------------------
//< init >
Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
Word.Application app = Globals.ThisAddIn.Application;
//</ init >
 
_arrWordLinks = new List<WordHyperlink>();
_frmTranslate = new frmTranslate();
_frmTranslate.Show();
 
//< get_Translate_Text >
get_Hyperlinks();
//</ get_Translate_Text >
 
//< get_Translate_Text >
string sText_Original = get_Sentences();
//</ get_Translate_Text >
 
//< Translate in Browser >
string sText_Translated = await do_Translate_Browser(sText_Original);
//</ Translate in Browser >
 
_frmTranslate.Close();
 
//< correct >
sText_Translated = "\n" + sText_Translated;
//</ correct >
 
//MessageBox.Show(sText_Translated);
string[] arr = sText_Translated.Split(new string[] { _sSplitSeq }, StringSplitOptions.RemoveEmptyEntries);
 
//----< @Loop: Sentences Back >----
for (int iSentence = arr.Length - 1; iSentence >= 0; iSentence--)
{
//----< @Element: Sentences Back >----

//--< get Translation_Bloc >--
string sTranslation_Block = arr[iSentence];
string sNr = sTranslation_Block.Substring(0, sTranslation_Block.IndexOf("|") - 1);
int nrSentence = Convert.ToInt16(sNr);
string sSentence_Translated = sTranslation_Block.Substring(sTranslation_Block.IndexOf("|") + 2);
//--</ get Translation_Bloc >--
 
 
 
//--< Set Sentence in Doc >--
Word.Range range_Sentence = doc.Sentences[nrSentence];
 
string sSentence_Original = range_Sentence.Text;
//< correct >
int intTrail = sSentence_Original.Length - sSentence_Original.TrimEnd().Length;
//string sTrail = "";
//if (intTrail > 0) sTrail = sSentence_Original.Substring(sSentence_Original.Length - intTrail);
sSentence_Translated = sSentence_Translated.TrimEnd(); //cut \n
//sSentence_Translated= sSentence_Translated + sTrail;
range_Sentence.SetRange(range_Sentence.Start, range_Sentence.End - intTrail);
 
range_Sentence.Text = sSentence_Translated;
//sentence_Range.Text = sSentence_Translated ;
//</ correct >
 
//--</ Set Sentence in Doc >--
 
//--< replace Hyperlink >--
Word.Range range_document = doc.Range();
//int intStartSearch = 0;
for (int iHL = 0; iHL < _arrWordLinks.Count; iHL++)
{
WordHyperlink hl = _arrWordLinks[iHL];
if (hl.NrSentence == nrSentence)
{
Word.Range range_Find = doc.Sentences[nrSentence];
Word.Find objFind = range_Find.Find;
objFind.Execute("[lnk:");
if (objFind.Found == true)
{
//----< found link >----
Word.Range range_Found=objFind.Parent ;
int posStart = range_Found.Start;
if (posStart > 0)
{
range_Find = doc.Sentences[nrSentence];
objFind = range_Find.Find;
objFind.Execute("/]");
range_Found = objFind.Parent;
int posEnd = range_Found.Start;
if (posEnd > 0)
{
posEnd = posEnd+ 2;
Word.Range range_Link = doc.Range(posStart, posEnd);
//System.Diagnostics.Debug.WriteLine("Start=" + posStart + " End=" + posEnd + " Text:" + hl.HlText + " Adress:" + hl.HlAddress, "Debug.Translate:");

//< set hyperlink >
doc.Hyperlinks.Add(Anchor: range_Link, Address: hl.HlAddress, TextToDisplay: hl.HlText );
//</ set hyperlink >
}
}
//----</ found link >----
}
}
}
//--</ replace Hyperlink >--
 
 
//----</ @Element: Sentences Back >----
}
//----</ @Loop: Sentences Back >----
 
_arrWordLinks= null;

 
//-------------------</ do_translate_all() >-------------------
}
 
 
frmTranslate _frmTranslate;
private async Task<string> do_Translate_Browser(string sText)
{
//-------------------< do_Translate_Browser() >-------------------
 
 
//*opens website
Awesomium.Windows.Forms.WebControl webBrowser1 = _frmTranslate.browser_control;
webBrowser1.Source = new Uri("http://translate.google.com?hl=en#de/en/");
//----< wait doc.complete >----
DateTime dtStart = DateTime.Now;
while (webBrowser1.IsDocumentReady != true)
{
if ((DateTime.Now - dtStart).Seconds > 5) break;
await Task.Delay(1);
}
//----</ wait doc.complete >---
//----< wait doc.complete >----
dtStart = DateTime.Now;
while (webBrowser1.IsLoading == true)
{
if ((DateTime.Now - dtStart).Seconds > 5) break;
await Task.Delay(1);
}
//----</ wait doc.complete >---
 
//----< set source >----
dynamic document = (Awesomium.Core.JSObject)webBrowser1.ExecuteJavascriptWithResult("document");
using (document)
{
var textarea = document.getElementById("source");
if (textarea == null)
{
MessageBox.Show("no source found");
return "";
}
else
{
textarea.value = sText;
 
dynamic btnTranslate = document.getElementById("gt-submit");
btnTranslate.click();
}
}
//----</ set source >----
 

//----< wait x second >----
//*while google translates
await Task.Delay(2000);
//----</ wait x second >----
 
 
//------< get translation >------
//----< wait doc.complete >----
dtStart = DateTime.Now;
while (webBrowser1.IsDocumentReady != true)
{
if ((DateTime.Now - dtStart).Seconds > 5) break;
await Task.Delay(1);
}
//----</ wait doc.complete >---
//----< wait doc.complete >----
dtStart = DateTime.Now;
while (webBrowser1.IsLoading == true)
{
if ((DateTime.Now - dtStart).Seconds > 5) break;
await Task.Delay(1);
}
//----</ wait doc.complete >---
 
//----< wait doc.complete >----
dtStart = DateTime.Now;
string sTranslated = "";
while (sTranslated=="")
{
document = (Awesomium.Core.JSObject)webBrowser1.ExecuteJavascriptWithResult("document");
using (document)
{
var span_result = document.getElementById("result_box");
if (span_result != null)
{
string sResult_Test = span_result.textContent;
if (sResult_Test != "" && sResult_Test != "Translating..")
{
break;
}
}
}
 
if ((DateTime.Now - dtStart).Seconds > 10) break;
await Task.Delay(100);
//Console.WriteLine((DateTime.Now - dtStart).Milliseconds);
}
//----</ wait doc.complete >---
 
//----< get result >----
string sResult = "";
document = (Awesomium.Core.JSObject)webBrowser1.ExecuteJavascriptWithResult("document");
using (document)
{
 
 
var span_result = document.getElementById("result_box");
if (span_result == null)
{
MessageBox.Show("no result found");
return "";
}
else
{
var ctl = span_result;
sResult = span_result.innerText;
//MessageBox.Show("result=" + sResult);
}
}
//----</ get result >----
 
 

return sResult;
//htmlelement.properties https://developer.mozilla.org/de/docs/Web/API/Node/innerText
//.textContent: https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent
//------</ get translation >------
//-------------------</ do_Translate_Browser() >-------------------
}
 
private void get_Hyperlinks()
{
//----------< get_Hyperlinks() >----------
//*Sammelt den Text mit Trennzeichen und fuellt die Hyperlinks
//< init >
Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
Word.Application app = Globals.ThisAddIn.Application;
//</ init >
 
for (int iSentence = 1; iSentence <= doc.Sentences.Count; iSentence++)
{
//--< get hyperlinks >--
Word.Range range_Sentence = doc.Sentences[iSentence];
//foreach (Word.Range range_Sentence in doc.Sentences)
//{
foreach (Word.Hyperlink lnk in range_Sentence.Hyperlinks)
{
//< add link in sentence >
WordHyperlink wordLink = new WordHyperlink(iSentence, lnk.Address, lnk.TextToDisplay);
_arrWordLinks.Add(wordLink);
//</ add link in sentence >
 
lnk.TextToDisplay = "[lnk:\"" + lnk.TextToDisplay + "\"/]";
}
 
//}
//--</ get hyperlinks >--
}
 
//----------</ get_Hyperlinks() >----------
}
 
 
private string get_Sentences()
{
//----------< get_Sentences() >----------
//*Sammelt den Text mit Trennzeichen und fuellt die Hyperlinks
//< init >
Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
Word.Application app = Globals.ThisAddIn.Application;
//</ init >
 
string sSentences = "";
for (int iSentence = 1; iSentence <= doc.Sentences.Count; iSentence++)
{
Word.Range range_Sentence = doc.Sentences[iSentence];
//----< check if not in Table >----
bool inTable = false;
foreach (Word.Table tbl in doc.Tables)
{
if (range_Sentence.Start >= tbl.Range.Start && range_Sentence.End <= tbl.Range.End)
{ inTable = true; break; }
}
//----</ check if not in Table >----
 
if (inTable != true)
{
string sText = range_Sentence.Text;
if (sText.Trim().Length > 2)
{
sSentences += "" + _sSplitSeq + "" + iSentence + "|\n" + sText;
}
}
}
 
return sSentences;
//----------</ get_Sentences() >----------
}
#endregion ------</ Region Translate >------
 

 
Mobile

.

0123movie.net