Merge pull request #670 from hargata/Hargata/translation.getter

Add translation getter
This commit is contained in:
Hargata Softworks 2024-10-27 09:54:44 -06:00 committed by GitHub
commit e5a594367e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 212 additions and 4 deletions

View File

@ -284,6 +284,46 @@ namespace CarCareTracker.Controllers
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()
{

View File

@ -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<GenericRecordExportModel> genericRecords)
{

12
Models/Translations.cs Normal file
View File

@ -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>();
}
}

View File

@ -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">
@ -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>
}
@ -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>
@ -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({

View File

@ -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>

File diff suppressed because one or more lines are too long

View File

@ -234,3 +234,22 @@ function exportTranslation(){
}
});
}
function showTranslationDownloader() {
$.get('/Home/GetAvailableTranslations', function(data){
$('#translationDownloadModalContent').html(data);
$('#translationDownloadModal').modal('show');
})
}
function hideTranslationDownloader() {
$('#translationDownloadModal').modal('hide');
}
function downloadTranslation(continent, name) {
$.get(`/Home/DownloadTranslation?continent=${continent}&name=${name}`, function (data) {
if (data) {
successToast("Translation Downloaded");
updateSettings();
} else {
errorToast(genericErrorMessage());
}
})
}