Skip to content

Commit

Permalink
Removing old google validation code check
Browse files Browse the repository at this point in the history
  • Loading branch information
salarcode committed Jan 15, 2018
1 parent 411b2a3 commit 734cc5d
Showing 1 changed file with 74 additions and 90 deletions.
164 changes: 74 additions & 90 deletions AutoResxTranslator/frmMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,13 +294,9 @@ void TranslateResxFilesAsync(string sourceResx, string sourceLng, List<string> d
if (string.IsNullOrWhiteSpace(orgText))
continue;

// Read the Key
var translatorUrlKey = GetGoogleTranslatorKey_UiThread(orgText);
if (!translatorUrlKey.Success)
continue;

// There is no longer a key to validate
// the key
var textTranslatorUrlKey = translatorUrlKey.Result;
var textTranslatorUrlKey = "";

string translated = string.Empty;
bool success = false;
Expand Down Expand Up @@ -433,72 +429,76 @@ private void ImportExcel(string excelFile, string resxFile, string sheetName, st

internal delegate OpResult GetGoogleTranslatorKeyDeligate(string textToTranslate);

internal OpResult GetGoogleTranslatorKey(string textToTranslate)
{
if (IsGoogleTranslatorLoaded())
{
try
{
// ReSharper disable once PossibleNullReferenceException
object result = webBrowser.Document.InvokeScript("Vj", new object[] { textToTranslate });
if (result == null)
{
return new OpResult
{
Success = false,
Result = "Failed to find the translation function! Cantact the author please.\n"
};
}
return new OpResult
{
Success = true,
Result = result.ToString()
};
}
catch (Exception ex)
{
return new OpResult
{
Success = false,
Result = ex.Message
};
}
}
else
{
return new OpResult
{
Success = false,
Result = "Google Translator is not loaded yet!"
};
}
}

internal OpResult GetGoogleTranslatorKey_UiThread(string textToTranslate)
{
if (this.InvokeRequired)
{
var asyncHandler = this.BeginInvoke(new GetGoogleTranslatorKeyDeligate(GetGoogleTranslatorKey),
new object[] {textToTranslate});

asyncHandler.AsyncWaitHandle.WaitOne();

var result = this.EndInvoke(asyncHandler) as OpResult;
if (result != null)
{
return result;
}
return new OpResult()
{
Success = false,
Result = "Failed to get the key for the translator from main ui thread."
};
}
else
{
return GetGoogleTranslatorKey(textToTranslate);
}
}
#region Old core to read old GoogleTranslation Validation Key

//internal OpResult GetGoogleTranslatorKey(string textToTranslate)
//{
// if (IsGoogleTranslatorLoaded())
// {
// try
// {
// // ReSharper disable once PossibleNullReferenceException
// object result = webBrowser.Document.InvokeScript("Vj", new object[] { textToTranslate });
// if (result == null)
// {
// return new OpResult
// {
// Success = false,
// Result = "Failed to find the translation function! Cantact the author please.\n"
// };
// }
// return new OpResult
// {
// Success = true,
// Result = result.ToString()
// };
// }
// catch (Exception ex)
// {
// return new OpResult
// {
// Success = false,
// Result = ex.Message
// };
// }
// }
// else
// {
// return new OpResult
// {
// Success = false,
// Result = "Google Translator is not loaded yet!"
// };
// }
//}

//internal OpResult GetGoogleTranslatorKey_UiThread(string textToTranslate)
//{
// if (this.InvokeRequired)
// {
// var asyncHandler = this.BeginInvoke(new GetGoogleTranslatorKeyDeligate(GetGoogleTranslatorKey),
// new object[] {textToTranslate});

// asyncHandler.AsyncWaitHandle.WaitOne();

// var result = this.EndInvoke(asyncHandler) as OpResult;
// if (result != null)
// {
// return result;
// }
// return new OpResult()
// {
// Success = false,
// Result = "Failed to get the key for the translator from main ui thread."
// };
// }
// else
// {
// return GetGoogleTranslatorKey(textToTranslate);
// }
//}

#endregion


bool IsGoogleTranslatorLoaded()
Expand All @@ -520,17 +520,6 @@ private void frmMain_Load(object sender, EventArgs e)

private void btnTranslate_Click(object sender, EventArgs e)
{
var a = new Action(() =>
{
var result = GetGoogleTranslatorKey_UiThread(txtSrc.Text);
if (result.Success)
{
}
});
a.BeginInvoke((v) => { }, null);
return;

if (cmbDesc.SelectedIndex == -1 || cmbSrc.SelectedIndex == -1)
{
Expand All @@ -547,13 +536,8 @@ private void btnTranslate_Click(object sender, EventArgs e)
var lngDest = ((KeyValuePair<string, string>)cmbDesc.SelectedItem).Key;
var text = txtSrc.Text;

var textTranslatorResult = GetGoogleTranslatorKey(text);
if (!textTranslatorResult.Success)
{
txtDesc.Text = textTranslatorResult.Result;
return;
}
var textTranslatorUrlKey = textTranslatorResult.Result;
// There is no longer a key to validate
var textTranslatorUrlKey = "";

IsBusy(true);
GTranslateService.TranslateAsync(
Expand Down

0 comments on commit 734cc5d

Please sign in to comment.