Localization and AppSettings
This commit is contained in:
parent
1cfdf5d9be
commit
b20b026aff
26
C969Project/AppSettings.cs
Normal file
26
C969Project/AppSettings.cs
Normal 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];
|
||||||
|
}
|
||||||
|
}
|
@ -11,6 +11,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.5" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.5" />
|
||||||
<PackageReference Include="MySql.EntityFrameworkCore" Version="9.0.3" />
|
<PackageReference Include="MySql.EntityFrameworkCore" Version="9.0.3" />
|
||||||
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@ -19,4 +20,13 @@
|
|||||||
</Compile>
|
</Compile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Update="appsettings.json">
|
||||||
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
|
<None Update="locale\**\*.*">
|
||||||
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
70
C969Project/Localization.cs
Normal file
70
C969Project/Localization.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
14
C969Project/LoginForm.Designer.cs
generated
14
C969Project/LoginForm.Designer.cs
generated
@ -32,6 +32,7 @@
|
|||||||
passwordTextBox = new System.Windows.Forms.TextBox();
|
passwordTextBox = new System.Windows.Forms.TextBox();
|
||||||
loginButton = new System.Windows.Forms.Button();
|
loginButton = new System.Windows.Forms.Button();
|
||||||
panel1 = new System.Windows.Forms.Panel();
|
panel1 = new System.Windows.Forms.Panel();
|
||||||
|
loginLabel = new System.Windows.Forms.Label();
|
||||||
panel1.SuspendLayout();
|
panel1.SuspendLayout();
|
||||||
SuspendLayout();
|
SuspendLayout();
|
||||||
//
|
//
|
||||||
@ -62,6 +63,7 @@
|
|||||||
//
|
//
|
||||||
// panel1
|
// panel1
|
||||||
//
|
//
|
||||||
|
panel1.Controls.Add(loginLabel);
|
||||||
panel1.Controls.Add(loginButton);
|
panel1.Controls.Add(loginButton);
|
||||||
panel1.Controls.Add(passwordTextBox);
|
panel1.Controls.Add(passwordTextBox);
|
||||||
panel1.Controls.Add(usernameTextBox);
|
panel1.Controls.Add(usernameTextBox);
|
||||||
@ -70,6 +72,16 @@
|
|||||||
panel1.Size = new System.Drawing.Size(776, 426);
|
panel1.Size = new System.Drawing.Size(776, 426);
|
||||||
panel1.TabIndex = 0;
|
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
|
// LoginForm
|
||||||
//
|
//
|
||||||
AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
|
AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
|
||||||
@ -82,6 +94,8 @@
|
|||||||
ResumeLayout(false);
|
ResumeLayout(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private System.Windows.Forms.Label loginLabel;
|
||||||
|
|
||||||
private System.Windows.Forms.TextBox usernameTextBox;
|
private System.Windows.Forms.TextBox usernameTextBox;
|
||||||
private System.Windows.Forms.TextBox passwordTextBox;
|
private System.Windows.Forms.TextBox passwordTextBox;
|
||||||
private System.Windows.Forms.Button loginButton;
|
private System.Windows.Forms.Button loginButton;
|
||||||
|
@ -5,6 +5,13 @@ namespace C969Project
|
|||||||
public LoginForm()
|
public LoginForm()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
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");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,9 @@ namespace C969Project
|
|||||||
[STAThread]
|
[STAThread]
|
||||||
static void Main()
|
static void Main()
|
||||||
{
|
{
|
||||||
|
AppSettings.Initialize();
|
||||||
|
Localization.Initialize();
|
||||||
|
|
||||||
// To customize application configuration such as set high DPI settings or default font,
|
// To customize application configuration such as set high DPI settings or default font,
|
||||||
// see https://aka.ms/applicationconfiguration.
|
// see https://aka.ms/applicationconfiguration.
|
||||||
ApplicationConfiguration.Initialize();
|
ApplicationConfiguration.Initialize();
|
||||||
|
8
C969Project/appsettings.json
Normal file
8
C969Project/appsettings.json
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"ConnectionStrings": {
|
||||||
|
"DefaultConnection": ""
|
||||||
|
},
|
||||||
|
"AppSettings": {
|
||||||
|
"DefaultLocale": "en-US"
|
||||||
|
}
|
||||||
|
}
|
6
C969Project/locale/en-US.json
Normal file
6
C969Project/locale/en-US.json
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"login_text": "Login",
|
||||||
|
"password_text": "Password",
|
||||||
|
"username_text": "Username",
|
||||||
|
"login_error_message": "Invalid username or password"
|
||||||
|
}
|
6
C969Project/locale/is-IS.json
Normal file
6
C969Project/locale/is-IS.json
Normal 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"
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user