Skip to content
This repository was archived by the owner on Sep 11, 2023. It is now read-only.

Commit 878a4cc

Browse files
committed
translation provider optimizations
1 parent cd5f60e commit 878a4cc

File tree

1 file changed

+50
-34
lines changed

1 file changed

+50
-34
lines changed

Interop/TranslationProvider.cs

+50-34
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public TranslationProvider()
3030
Directory.CreateDirectory(_translationsDir);
3131
}
3232

33-
if (IsUpdateAvailable())
33+
if (IsUpdateAvailable() != null)
3434
{
3535
UpdateTranslations();
3636
}
@@ -138,7 +138,7 @@ public void ParseTranslationFiles()
138138
}
139139
catch (Exception ex)
140140
{
141-
MessageBox.Show($"There was a problem while updating the translations file.\n" +
141+
MessageBox.Show($"There was a problem while parsing the translation files.\n" +
142142
$"Details: {ex.Message}");
143143
}
144144
}
@@ -148,52 +148,68 @@ public void ParseTranslationFiles()
148148
/// </summary>
149149
public void UpdateTranslations()
150150
{
151-
// Clear temp folder before beggining
152-
DirUtils.ClearTempFolder();
153-
154-
// Download latest release zip file
155-
var wc = new WebClient();
156-
var downloadedFile = Path.Combine(_tempDir, "langs.zip");
157-
wc.Headers.Add(HttpRequestHeader.UserAgent, Constants.ProductHeaderValueName);
158-
wc.DownloadFile(_latestVersion.ZipballUrl, downloadedFile);
159-
160-
// Decompress and replace all of its files
161-
ZipFile.ExtractToDirectory(downloadedFile, _tempDir);
162-
var filesDir = Directory.GetFiles(Directory.GetDirectories(_tempDir)[0]).Where(x => x.EndsWith(".xml"));
163-
foreach (var file in filesDir)
151+
try
164152
{
165-
// Create wrapper
166-
var fInfo = new FileInfo(file);
167-
168-
// Replace current file with this one
169-
var destination = Path.Combine(_translationsDir, fInfo.Name);
170-
if (File.Exists(destination))
153+
// Clear temp folder before beggining
154+
DirUtils.ClearTempFolder();
155+
156+
// Download latest release zip file
157+
var wc = new WebClient();
158+
var downloadedFile = Path.Combine(_tempDir, "langs.zip");
159+
wc.Headers.Add(HttpRequestHeader.UserAgent, Constants.ProductHeaderValueName);
160+
wc.DownloadFile(_latestVersion.ZipballUrl, downloadedFile);
161+
162+
// Decompress and replace all of its files
163+
ZipFile.ExtractToDirectory(downloadedFile, _tempDir);
164+
var filesDir = Directory.GetFiles(Directory.GetDirectories(_tempDir)[0]).Where(x => x.EndsWith(".xml"));
165+
foreach (var file in filesDir)
171166
{
172-
File.Delete(destination);
167+
// Create wrapper
168+
var fInfo = new FileInfo(file);
169+
170+
// Replace current file with this one
171+
var destination = Path.Combine(_translationsDir, fInfo.Name);
172+
if (File.Exists(destination))
173+
{
174+
File.Delete(destination);
175+
}
176+
File.Move(fInfo.FullName, destination);
173177
}
174-
File.Move(fInfo.FullName, destination);
175-
}
176178

177-
// Delete all temp folder contents
178-
DirUtils.ClearTempFolder();
179+
// Delete all temp folder contents
180+
DirUtils.ClearTempFolder();
179181

180-
// Update version to options object
181-
Program.OptionsObject.TranslationsVersion = int.Parse(_latestVersion.Name);
182+
// Update version to options object
183+
Program.OptionsObject.TranslationsVersion = int.Parse(_latestVersion.Name);
184+
}
185+
catch (Exception ex)
186+
{
187+
MessageBox.Show($"There was a problem while downloading the new translation files.\n" +
188+
$"Details: {ex.Message}");
189+
}
182190
}
183191

184192
/// <summary>
185193
/// Compares the stored version of the translations release with the one from GitHub
186194
/// </summary>
187195
/// <returns>Whether there's an update available</returns>
188-
public bool IsUpdateAvailable()
196+
public bool? IsUpdateAvailable()
189197
{
190-
var client = new GitHubClient(new ProductHeaderValue(Constants.ProductHeaderValueName));
191-
var versionStored = Program.OptionsObject.TranslationsVersion;
198+
try
199+
{
200+
var client = new GitHubClient(new ProductHeaderValue(Constants.ProductHeaderValueName));
201+
var versionStored = Program.OptionsObject.TranslationsVersion;
192202

193-
_latestVersion = client.Repository.Release.GetAll(Constants.OrgName,
194-
Constants.TranslationsRepoName).Result[0];
203+
_latestVersion = client.Repository.Release.GetAll(Constants.OrgName,
204+
Constants.TranslationsRepoName).Result[0];
205+
206+
return versionStored < int.Parse(_latestVersion.Name);
207+
}
208+
catch (Exception)
209+
{
210+
return null;
211+
}
195212

196-
return versionStored < int.Parse(_latestVersion.Name);
197213
}
198214
}
199215
}

0 commit comments

Comments
 (0)