diff --git a/Controllers/HomeController.cs b/Controllers/HomeController.cs index 6b90c4d..1be1313 100644 --- a/Controllers/HomeController.cs +++ b/Controllers/HomeController.cs @@ -284,6 +284,46 @@ namespace CarCareTracker.Controllers var result = _translationHelper.ExportTranslation(translationData); return Json(result); } + [Authorize(Roles = nameof(UserData.IsRootUser))] + [HttpGet] + public async Task GetAvailableTranslations() + { + try + { + var httpClient = new HttpClient(); + var translations = await httpClient.GetFromJsonAsync(StaticHelper.TranslationDirectoryPath) ?? new Translations(); + return PartialView("_Translations", translations); + } + catch (Exception ex) + { + _logger.LogError($"Unable to retrieve translations: {ex.Message}"); + return PartialView("_Translations", new Translations()); + } + } + [Authorize(Roles = nameof(UserData.IsRootUser))] + [HttpGet] + public async Task DownloadTranslation(string continent, string name) + { + try + { + var httpClient = new HttpClient(); + var translationData = await httpClient.GetFromJsonAsync>(StaticHelper.GetTranslationDownloadPath(continent, name)) ?? new Dictionary(); + if (translationData.Any()) + { + _translationHelper.SaveTranslation(name, translationData); + } else + { + _logger.LogError($"Unable to download translation: {name}"); + return Json(false); + } + return Json(true); + } + catch (Exception ex) + { + _logger.LogError($"Unable to download translation: {ex.Message}"); + return Json(false); + } + } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { diff --git a/Helper/StaticHelper.cs b/Helper/StaticHelper.cs index e6564b6..cdb4828 100644 --- a/Helper/StaticHelper.cs +++ b/Helper/StaticHelper.cs @@ -16,6 +16,8 @@ namespace CarCareTracker.Helper public static string ReminderEmailTemplate = "defaults/reminderemailtemplate.txt"; public static string DefaultAllowedFileExtensions = ".png,.jpg,.jpeg,.pdf,.xls,.xlsx,.docx"; public static string SponsorsPath = "https://hargata.github.io/hargata/sponsors.json"; + public static string TranslationPath = "https://hargata.github.io/lubelog_translations"; + public static string TranslationDirectoryPath = $"{TranslationPath}/directory.json"; public static string GetTitleCaseReminderUrgency(ReminderUrgency input) { switch (input) @@ -332,6 +334,51 @@ namespace CarCareTracker.Helper } } } + //Translations + public static string GetTranslationDownloadPath(string continent, string name) + { + if (string.IsNullOrWhiteSpace(continent) || string.IsNullOrWhiteSpace(name)){ + return string.Empty; + } + else + { + switch (continent) + { + case "NorthAmerica": + continent = "North America"; + break; + case "SouthAmerica": + continent = "South America"; + break; + } + return $"{TranslationPath}/{continent}/{name}.json"; + } + } + public static string GetTranslationName(string name) + { + if (string.IsNullOrWhiteSpace(name)) + { + return string.Empty; + } else + { + try + { + string cleanedName = name.Contains("_") ? name.Replace("_", "-") : name; + string displayName = CultureInfo.GetCultureInfo(cleanedName).DisplayName; + if (string.IsNullOrWhiteSpace(displayName)) + { + return name; + } + else + { + return displayName; + } + } catch (Exception ex) + { + return name; + } + } + } //CSV Write Methods public static void WriteGenericRecordExportModel(CsvWriter _csv, IEnumerable genericRecords) { diff --git a/Models/Translations.cs b/Models/Translations.cs new file mode 100644 index 0000000..7bb90bc --- /dev/null +++ b/Models/Translations.cs @@ -0,0 +1,12 @@ +namespace CarCareTracker.Models +{ + public class Translations + { + public List Africa { get; set; } = new List(); + public List Asia { get; set; } = new List(); + public List Europe { get; set; } = new List(); + public List NorthAmerica { get; set; } = new List(); + public List SouthAmerica { get; set; } = new List(); + public List Oceania { get; set; } = new List(); + } +} diff --git a/Views/Home/_Settings.cshtml b/Views/Home/_Settings.cshtml index 71499eb..4f3a549 100644 --- a/Views/Home/_Settings.cshtml +++ b/Views/Home/_Settings.cshtml @@ -171,7 +171,7 @@
@@ -183,7 +183,7 @@ } @@ -209,7 +209,16 @@
- +
+ + + +
+
@@ -308,6 +317,12 @@
+