Skip to content

Commit

Permalink
Merge pull request #670 from hargata/Hargata/translation.getter
Browse files Browse the repository at this point in the history
Add translation getter
  • Loading branch information
hargata authored Oct 27, 2024
2 parents 36ac61d + 12aaf8c commit e5a5943
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 4 deletions.
40 changes: 40 additions & 0 deletions Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,46 @@ public IActionResult ExportTranslation(Dictionary<string, string> translationDat
var result = _translationHelper.ExportTranslation(translationData);
return Json(result);
}
[Authorize(Roles = nameof(UserData.IsRootUser))]
[HttpGet]
public async Task<IActionResult> GetAvailableTranslations()
{
try
{
var httpClient = new HttpClient();
var translations = await httpClient.GetFromJsonAsync<Translations>(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<IActionResult> DownloadTranslation(string continent, string name)
{
try
{
var httpClient = new HttpClient();
var translationData = await httpClient.GetFromJsonAsync<Dictionary<string,string>>(StaticHelper.GetTranslationDownloadPath(continent, name)) ?? new Dictionary<string, string>();
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()
{
Expand Down
47 changes: 47 additions & 0 deletions Helper/StaticHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public static class StaticHelper
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)
Expand Down Expand Up @@ -332,6 +334,51 @@ public static string GetVehicleIdentifier(VehicleViewModel vehicle)
}
}
}
//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<GenericRecordExportModel> genericRecords)
{
Expand Down
12 changes: 12 additions & 0 deletions Models/Translations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace CarCareTracker.Models
{
public class Translations
{
public List<string> Africa { get; set; } = new List<string>();
public List<string> Asia { get; set; } = new List<string>();
public List<string> Europe { get; set; } = new List<string>();
public List<string> NorthAmerica { get; set; } = new List<string>();
public List<string> SouthAmerica { get; set; } = new List<string>();
public List<string> Oceania { get; set; } = new List<string>();
}
}
21 changes: 18 additions & 3 deletions Views/Home/_Settings.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@
<select class="form-select" onchange="updateSettings()" id="defaultLanguage">
@foreach (string uiLanguage in Model.UILanguages)
{
<!option @(Model.UserConfig.UserLanguage == uiLanguage ? "selected" : "")>@uiLanguage</!option>
<!option value="@uiLanguage" @(Model.UserConfig.UserLanguage == uiLanguage ? "selected" : "")>@StaticHelper.GetTranslationName(uiLanguage)</!option>
}
</select>
<div class="input-group-text">
Expand All @@ -183,7 +183,7 @@
<select class="form-select" onchange="updateSettings()" id="defaultLanguage">
@foreach (string uiLanguage in Model.UILanguages)
{
<!option @(Model.UserConfig.UserLanguage == uiLanguage ? "selected" : "")>@uiLanguage</!option>
<!option value="@uiLanguage" @(Model.UserConfig.UserLanguage == uiLanguage ? "selected" : "")>@StaticHelper.GetTranslationName(uiLanguage)</!option>
}
</select>
}
Expand All @@ -209,7 +209,16 @@
<div class="row">
<div class="col-6 d-grid">
<input onChange="uploadLanguage(this)" type="file" accept=".json" class="d-none" id="inputLanguage">
<button onclick="openUploadLanguage()" class="btn btn-primary btn-md">@translator.Translate(userLanguage, "Upload")</button>
<div class="btn-group">
<button onclick="openUploadLanguage()" class="btn btn-primary btn-md">@translator.Translate(userLanguage, "Upload")</button>
<button type="button" class="btn btn-md btn-primary btn-md dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false">
<span class="visually-hidden">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#" onclick="showTranslationDownloader()">@translator.Translate(userLanguage, "Get Translations")</a></li>
</ul>
</div>

</div>
<div class="col-6 d-grid">
<button onclick="deleteLanguage()" @(Model.UserConfig.UserLanguage == "en_US" ? "disabled" : "") class="btn btn-danger btn-md">@translator.Translate(userLanguage, "Delete")</button>
Expand Down Expand Up @@ -308,6 +317,12 @@
</div>
</div>
</div>
<div class="modal fade" data-bs-focus="false" id="translationDownloadModal" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content" id="translationDownloadModalContent">
</div>
</div>
</div>
<script>
function showReminderUrgencyThresholdModal(){
Swal.fire({
Expand Down
75 changes: 75 additions & 0 deletions Views/Home/_Translations.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
@using CarCareTracker.Helper
@inject IConfigHelper config
@inject ITranslationHelper translator
@model Translations
@{
var userConfig = config.GetUserConfig(User);
var userLanguage = userConfig.UserLanguage;
}
<div class="modal-header">
<h5 class="modal-title" id="translationDownloaderModalLabel">@translator.Translate(userLanguage, "Available Translations")</h5>
<button type="button" class="btn-close" onclick="hideTranslationDownloader()" aria-label="Close"></button>
</div>
<div class="modal-body" onkeydown="handleEnter(this)">
<form class="form-inline">
<div class="form-group" style="max-height:50vh; overflow-x:hidden; overflow-y:scroll;">
@foreach(var translation in Model.Africa)
{
<div class="row mb-2">
<div class="col-10">@StaticHelper.GetTranslationName(translation)</div>
<div class="col-2">
<button type="button" class="btn btn-primary" onclick="downloadTranslation('Africa','@translation')"><i class="bi bi-download"></i></button>
</div>
</div>
}
@foreach (var translation in Model.Asia)
{
<div class="row mb-2">
<div class="col-10">@StaticHelper.GetTranslationName(translation)</div>
<div class="col-2">
<button type="button" class="btn btn-primary" onclick="downloadTranslation('Asia','@translation')"><i class="bi bi-download"></i></button>
</div>
</div>
}
@foreach (var translation in Model.Europe)
{
<div class="row mb-2">
<div class="col-10">@StaticHelper.GetTranslationName(translation)</div>
<div class="col-2">
<button type="button" class="btn btn-primary" onclick="downloadTranslation('Europe','@translation')"><i class="bi bi-download"></i></button>
</div>
</div>
}
@foreach (var translation in Model.NorthAmerica)
{
<div class="row mb-2">
<div class="col-10">@StaticHelper.GetTranslationName(translation)</div>
<div class="col-2">
<button type="button" class="btn btn-primary" onclick="downloadTranslation('NorthAmerica','@translation')"><i class="bi bi-download"></i></button>
</div>
</div>
}
@foreach (var translation in Model.SouthAmerica)
{
<div class="row mb-2">
<div class="col-10">@StaticHelper.GetTranslationName(translation)</div>
<div class="col-2">
<button type="button" class="btn btn-primary" onclick="downloadTranslation('SouthAmerica','@translation')"><i class="bi bi-download"></i></button>
</div>
</div>
}
@foreach (var translation in Model.Oceania)
{
<div class="row mb-2">
<div class="col-10">@StaticHelper.GetTranslationName(translation)</div>
<div class="col-2">
<button type="button" class="btn btn-primary" onclick="downloadTranslation('Oceania','@translation')"><i class="bi bi-download"></i></button>
</div>
</div>
}
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" onclick="hideTranslationDownloader()">@translator.Translate(userLanguage, "Cancel")</button>
</div>
Loading

0 comments on commit e5a5943

Please sign in to comment.