Localization and AppSettings

This commit is contained in:
Spudnut2000 2025-06-10 22:32:10 -05:00
parent 1cfdf5d9be
commit b20b026aff
9 changed files with 150 additions and 0 deletions

View File

@ -0,0 +1,26 @@
using Newtonsoft.Json;
namespace C969Project;
public static class AppSettings
{
public static Dictionary<string, Dictionary<string, string>> Settings { get; private set; } = new();
public static void Initialize()
{
string contents;
using (StreamReader r = new StreamReader("appsettings.json"))
{
contents = r.ReadToEnd();
}
Settings = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, string>>>(contents);
}
public static string GetSetting(string category, string setting)
{
return Settings[category][setting];
}
}

View File

@ -11,6 +11,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.5" />
<PackageReference Include="MySql.EntityFrameworkCore" Version="9.0.3" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>
@ -19,4 +20,13 @@
</Compile>
</ItemGroup>
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="locale\**\*.*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@ -0,0 +1,70 @@
using System.Globalization;
using Newtonsoft.Json;
namespace C969Project;
public static class Localization
{
public static Dictionary<string, Dictionary<string, string>> Localizations = new();
public static string CurrentLanguage { get; set; } = "en-US";
public static void Initialize()
{
LoadLocalizations();
DetermineLocale();
}
public static string GetLocalization(string lang, string id)
{
return Localizations[lang][id] ?? "Couldn't find localization";
}
public static string GetLocalization(string id) => GetLocalization(CurrentLanguage, id);
private static void DetermineLocale()
{
try
{
RegionInfo region = RegionInfo.CurrentRegion;
string code = region.TwoLetterISORegionName;
if (code.Equals("IS", StringComparison.OrdinalIgnoreCase))
{
CurrentLanguage = "is-IS";
}
else
{
CurrentLanguage = "en-US";
}
}
catch (Exception e)
{
Console.WriteLine($@"Localization Error: {e.Message}, defaulting to en-US");
CurrentLanguage = "en-US";
}
}
private static void LoadLocalizations()
{
if (Directory.Exists($"{AppDomain.CurrentDomain.BaseDirectory}/locale"))
{
var dir = new DirectoryInfo($"{AppDomain.CurrentDomain.BaseDirectory}/locale");
foreach (var file in dir.GetFiles("*.json"))
{
using (StreamReader r = new StreamReader(file.FullName))
{
var json = JsonConvert.DeserializeObject<Dictionary<string, string>>(r.ReadToEnd());
if (json != null) Localizations.Add(file.Name.Replace(".json", ""), json);
}
}
}
}
private static void AddLocalization(string language, Dictionary<string, string> dictionary)
{
if (!Localizations.ContainsKey(language))
{
Localizations.Add(language, dictionary);
}
}
}

View File

@ -32,6 +32,7 @@
passwordTextBox = new System.Windows.Forms.TextBox();
loginButton = new System.Windows.Forms.Button();
panel1 = new System.Windows.Forms.Panel();
loginLabel = new System.Windows.Forms.Label();
panel1.SuspendLayout();
SuspendLayout();
//
@ -62,6 +63,7 @@
//
// panel1
//
panel1.Controls.Add(loginLabel);
panel1.Controls.Add(loginButton);
panel1.Controls.Add(passwordTextBox);
panel1.Controls.Add(usernameTextBox);
@ -70,6 +72,16 @@
panel1.Size = new System.Drawing.Size(776, 426);
panel1.TabIndex = 0;
//
// loginLabel
//
loginLabel.Font = new System.Drawing.Font("Segoe UI", 10F);
loginLabel.Location = new System.Drawing.Point(241, 99);
loginLabel.Name = "loginLabel";
loginLabel.Size = new System.Drawing.Size(228, 40);
loginLabel.TabIndex = 4;
loginLabel.Text = "Login";
loginLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// LoginForm
//
AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
@ -82,6 +94,8 @@
ResumeLayout(false);
}
private System.Windows.Forms.Label loginLabel;
private System.Windows.Forms.TextBox usernameTextBox;
private System.Windows.Forms.TextBox passwordTextBox;
private System.Windows.Forms.Button loginButton;

View File

@ -5,6 +5,13 @@ namespace C969Project
public LoginForm()
{
InitializeComponent();
// Determine user location and set the Localization CureentLanguage based on either US or IS location
loginLabel.Text = Localization.GetLocalization("login_text");
usernameTextBox.PlaceholderText = Localization.GetLocalization("username_text");
passwordTextBox.PlaceholderText = Localization.GetLocalization("password_text");
loginButton.Text = Localization.GetLocalization("login_text");
}
}
}

View File

@ -8,6 +8,9 @@ namespace C969Project
[STAThread]
static void Main()
{
AppSettings.Initialize();
Localization.Initialize();
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();

View File

@ -0,0 +1,8 @@
{
"ConnectionStrings": {
"DefaultConnection": ""
},
"AppSettings": {
"DefaultLocale": "en-US"
}
}

View File

@ -0,0 +1,6 @@
{
"login_text": "Login",
"password_text": "Password",
"username_text": "Username",
"login_error_message": "Invalid username or password"
}

View File

@ -0,0 +1,6 @@
{
"login_text": "Skráðu þig inn",
"password_text": "Aðgangsorð",
"username_text": "Notandanafn",
"login_error_message": "Innskráningin eða lykilorðið er ógilt"
}