private void DownloadTranslations(object sender, EventArgs e)
{
if (dataGridView_translations.SelectedRows.Count == 0)
{
MessageBox.Show(
Translation.GetTranslation("TL_NO_TLS_SELECTED"),
Translation.GetTranslation("TL_NO_TLS_SELECTED_TITLE"),
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
return;
}
string translationsPath = Path.Combine(MaidFiddler.DATA_PATH, Translation.TRANSLATIONS_PATH);
LoadingBarGUI loadingBarGui = new LoadingBarGUI(
Translation.GetTranslation("LOADING"),
$"{Translation.GetTranslation("TL_TRANSLATION_DOWNLOAD")}",
true,
g =>
{
g.Timer.Start();
Thread downloaderThread = new Thread(
() =>
{
foreach (DataGridViewRow selectedRow in dataGridView_translations.SelectedRows)
{
string tlFileName = langCodes[selectedRow.Index];
g.TextLabel.Text =
$"{Translation.GetTranslation("TL_TRANSLATION_DOWNLOAD")} {selectedRow.Cells[0]}";
Debugger.WriteLine(LogLevel.Info, $"Downloading language ID {selectedRow.Index}");
try
{
HttpWebRequest webRequest =
(HttpWebRequest)
WebRequest.Create($"{MaidFiddler.RESOURCE_URL}/Resources/Translations/{tlFileName}.txt");
Debugger.WriteLine(
LogLevel.Info,
$"Getting translation file from {MaidFiddler.RESOURCE_URL}/Resources/Translations/{tlFileName}.txt");
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
Debugger.WriteLine(LogLevel.Info, "Got response!");
Debugger.WriteLine(LogLevel.Info, $"Response: {response.StatusCode}");
if (response.StatusCode == HttpStatusCode.NotFound)
{
MessageBox.Show(
"Failed to retreive translation: File not found.",
"Boop!",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
g.DialogResult = DialogResult.Abort;
g.Timer.Stop();
g.Close();
return;
}
Stream s = response.GetResponseStream();
Debugger.WriteLine(LogLevel.Info, "Reading response");
byte[] responseBuffer = new byte[1024];
List <byte> stringBuffer = new List <byte>();
int read;
do
{
read = s.Read(responseBuffer, 0, responseBuffer.Length);
stringBuffer.AddRange(responseBuffer, 0, read);
} while (read > 0);
using (TextWriter tw = File.CreateText(Path.Combine(translationsPath, $"{tlFileName}.txt")))
tw.Write(Encoding.UTF8.GetString(stringBuffer.ToArray()));
}
catch (WebException we)
{
MessageBox.Show(
$"Failed to retreive translation.\nResponse: {we.Message}",
"Boop!",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
g.DialogResult = DialogResult.Abort;
g.Timer.Stop();
g.Close();
}
catch (Exception ex)
{
MessageBox.Show(
$"Could not download the translation.\nInfo: {ex}",
"Boop!",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
g.DialogResult = DialogResult.Abort;
g.Timer.Stop();
g.Close();
}
}
g.DialogResult = DialogResult.OK;
g.Timer.Stop();
g.Close();
});
downloaderThread.Start();
});
DialogResult result = loadingBarGui.ShowDialog(this);
loadingBarGui.Dispose();
if (result != DialogResult.OK)
{
return;
}
MessageBox.Show(
Translation.GetTranslation("TL_DOWNLOAD_DONE"),
Translation.GetTranslation("TL_DOWNLOAD_DONE_TITLE"),
MessageBoxButtons.OK);
InitLanguageTable();
}