From 29a3c815fca037f060546dfec947a730d73a5f98 Mon Sep 17 00:00:00 2001 From: "DESKTOP-GENO133\\IvanPlex" Date: Fri, 2 Feb 2024 21:29:44 -0700 Subject: [PATCH] added translation helper. --- Helper/TranslationHelper.cs | 42 +++++++++++++++++ Models/UserConfig.cs | 1 + Program.cs | 1 + Views/Home/Index.cshtml | 21 +++++---- Views/Home/_Settings.cshtml | 84 +++++++++++++++++---------------- wwwroot/translations/en_US.json | 1 + 6 files changed, 101 insertions(+), 49 deletions(-) create mode 100644 Helper/TranslationHelper.cs create mode 100644 wwwroot/translations/en_US.json diff --git a/Helper/TranslationHelper.cs b/Helper/TranslationHelper.cs new file mode 100644 index 0000000..cbc8ed4 --- /dev/null +++ b/Helper/TranslationHelper.cs @@ -0,0 +1,42 @@ +using CarCareTracker.Models; +using System.Text.Json; + +namespace CarCareTracker.Helper +{ + public interface ITranslationHelper + { + string Translate(string userLanguage, string text); + } + public class TranslationHelper : ITranslationHelper + { + private readonly IFileHelper _fileHelper; + private readonly IConfiguration _config; + public TranslationHelper(IFileHelper fileHelper, IConfiguration config) + { + _fileHelper = fileHelper; + _config = config; + } + public string Translate(string userLanguage, string text) + { + bool create = bool.Parse(_config["LUBELOGGER_TRANSLATOR"] ?? "false"); + //transform input text into key. + string translationKey = text.Replace(" ", "_"); + var translationFilePath = _fileHelper.GetFullFilePath($"/translations/{userLanguage}.json", false); + if (File.Exists(translationFilePath)) + { + var translationFile = File.ReadAllText(translationFilePath); + var translationDictionary = JsonSerializer.Deserialize>(translationFile); + if (translationDictionary != null && translationDictionary.ContainsKey(translationKey)) + { + return translationDictionary[translationKey]; + } else if (create) + { + //create entry + translationDictionary.Add(translationKey, text); + File.WriteAllText(translationFilePath, JsonSerializer.Serialize(translationDictionary)); + } + } + return create ? string.Empty : text; + } + } +} diff --git a/Models/UserConfig.cs b/Models/UserConfig.cs index 342317d..7ff5f1d 100644 --- a/Models/UserConfig.cs +++ b/Models/UserConfig.cs @@ -17,6 +17,7 @@ public string PreferredGasMileageUnit { get; set; } = string.Empty; public string UserNameHash { get; set; } public string UserPasswordHash { get; set;} + public string UserLanguage { get; set; } = "en_US"; public List VisibleTabs { get; set; } = new List() { ImportMode.Dashboard, ImportMode.ServiceRecord, diff --git a/Program.cs b/Program.cs index c0be2b6..f3775a1 100644 --- a/Program.cs +++ b/Program.cs @@ -33,6 +33,7 @@ builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); +builder.Services.AddSingleton(); //configure logic builder.Services.AddSingleton(); diff --git a/Views/Home/Index.cshtml b/Views/Home/Index.cshtml index 86ffe5b..b3391e5 100644 --- a/Views/Home/Index.cshtml +++ b/Views/Home/Index.cshtml @@ -1,7 +1,10 @@ @using CarCareTracker.Helper @inject IConfigHelper config +@inject ITranslationHelper translator @{ - var enableAuth = config.GetUserConfig(User).EnableAuth; + var userConfig = config.GetUserConfig(User); + var enableAuth = userConfig.EnableAuth; + var userLanguage = userConfig.UserLanguage; var logoUrl = config.GetLogoUrl(); } @model string @@ -14,21 +17,21 @@
@@ -45,10 +48,10 @@
diff --git a/Views/Home/_Settings.cshtml b/Views/Home/_Settings.cshtml index f195c74..29309ad 100644 --- a/Views/Home/_Settings.cshtml +++ b/Views/Home/_Settings.cshtml @@ -1,9 +1,13 @@ @using CarCareTracker.Helper @model UserConfig +@inject ITranslationHelper translator +@{ + var userLanguage = Model.UserLanguage; +}
-
Settings
+
@translator.Translate(userLanguage,"Settings")

@@ -11,82 +15,82 @@
- +
- +
- +
- +
- +
- +
- +
- +
- +
- +
@if (User.IsInRole(nameof(UserData.IsRootUser))) {
- +
}
- Visible Tabs + @translator.Translate(userLanguage,"Visible Tabs")
  • - +
  • - +
  • - +
  • - +
  • - +
  • - +
@@ -94,55 +98,55 @@
  • - +
  • - +
  • - +
  • - +
  • - +
- Default Tab + @translator.Translate(userLanguage,"Default Tab")
@if (User.IsInRole(nameof(UserData.IsRootUser))) {
- Backups + @translator.Translate(userLanguage, "Backups")
- +
- +
@@ -152,7 +156,7 @@
-
About
+
@translator.Translate(userLanguage, "About")

diff --git a/wwwroot/translations/en_US.json b/wwwroot/translations/en_US.json new file mode 100644 index 0000000..b0af124 --- /dev/null +++ b/wwwroot/translations/en_US.json @@ -0,0 +1 @@ +{"Garage":"Garage","Settings":"Settings","Admin_Panel":"Admin Panel","Logout":"Logout","Dark_Mode":"Dark Mode","Enable_CSV_Imports":"Enable CSV Imports","Use_Imperial_Calculation_for_Fuel_Economy_Calculations(MPG)":"Use Imperial Calculation for Fuel Economy Calculations(MPG)","This_Will_Also_Change_Units_to_Miles_and_Gallons":"This Will Also Change Units to Miles and Gallons","Use_UK_MPG_Calculation":"Use UK MPG Calculation","Input_Gas_Consumption_in_Liters,_it_will_be_converted_to_UK_Gals_for_MPG_Calculation":"Input Gas Consumption in Liters, it will be converted to UK Gals for MPG Calculation","Sort_lists_in_Descending_Order(Newest_to_Oldest)":"Sort lists in Descending Order(Newest to Oldest)","Use_Three_Decimals_For_Fuel_Cost":"Use Three Decimals For Fuel Cost","Display_Saved_Notes_in_Markdown":"Display Saved Notes in Markdown","Auto_Refresh_Lapsed_Recurring_Reminders":"Auto Refresh Lapsed Recurring Reminders","Auto_Insert_Odometer_Records":"Auto Insert Odometer Records","Only_when_Adding_Service/Repair/Upgrade/Fuel_Record_or_Completing_a_Plan":"Only when Adding Service/Repair/Upgrade/Fuel Record or Completing a Plan","Enable_Authentication":"Enable Authentication","Visible_Tabs":"Visible Tabs","Service_Records":"Service Records","Dashboard":"Dashboard","Repairs":"Repairs","Upgrades":"Upgrades","Fuel":"Fuel","Odometer":"Odometer","Taxes":"Taxes","Notes":"Notes","Reminder":"Reminder","Supplies":"Supplies","Planner":"Planner","Default_Tab":"Default Tab","Service_Record":"Service Record","Tax":"Tax","Reminders":"Reminders","Backups":"Backups","Make":"Make","Restore":"Restore","About":"About","Replace_$0.00_Costs_with_---":"Replace $0.00 Costs with ---"} \ No newline at end of file