diff --git a/C969Project/AppSettings.cs b/C969Project/AppSettings.cs new file mode 100644 index 0000000..9baf273 --- /dev/null +++ b/C969Project/AppSettings.cs @@ -0,0 +1,26 @@ + + +using Newtonsoft.Json; + +namespace C969Project; + +public static class AppSettings +{ + public static Dictionary> Settings { get; private set; } = new(); + + public static void Initialize() + { + string contents; + using (StreamReader r = new StreamReader("appsettings.json")) + { + contents = r.ReadToEnd(); + } + + Settings = JsonConvert.DeserializeObject>>(contents); + } + + public static string GetSetting(string category, string setting) + { + return Settings[category][setting]; + } +} \ No newline at end of file diff --git a/C969Project/C969Project.csproj b/C969Project/C969Project.csproj index 086f81d..d7dbd80 100644 --- a/C969Project/C969Project.csproj +++ b/C969Project/C969Project.csproj @@ -11,6 +11,7 @@ + @@ -19,4 +20,13 @@ + + + Always + + + Always + + + \ No newline at end of file diff --git a/C969Project/Localization.cs b/C969Project/Localization.cs new file mode 100644 index 0000000..e3d91f2 --- /dev/null +++ b/C969Project/Localization.cs @@ -0,0 +1,70 @@ +using System.Globalization; +using Newtonsoft.Json; + +namespace C969Project; + +public static class Localization +{ + public static Dictionary> 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>(r.ReadToEnd()); + if (json != null) Localizations.Add(file.Name.Replace(".json", ""), json); + } + } + } + } + + private static void AddLocalization(string language, Dictionary dictionary) + { + if (!Localizations.ContainsKey(language)) + { + Localizations.Add(language, dictionary); + } + } +} \ No newline at end of file diff --git a/C969Project/LoginForm.Designer.cs b/C969Project/LoginForm.Designer.cs index 2060368..222d8b1 100644 --- a/C969Project/LoginForm.Designer.cs +++ b/C969Project/LoginForm.Designer.cs @@ -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; diff --git a/C969Project/LoginForm.cs b/C969Project/LoginForm.cs index 8efab69..b2a3b1e 100644 --- a/C969Project/LoginForm.cs +++ b/C969Project/LoginForm.cs @@ -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"); } } } diff --git a/C969Project/Program.cs b/C969Project/Program.cs index abca6df..d028618 100644 --- a/C969Project/Program.cs +++ b/C969Project/Program.cs @@ -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(); diff --git a/C969Project/appsettings.json b/C969Project/appsettings.json new file mode 100644 index 0000000..da49e2f --- /dev/null +++ b/C969Project/appsettings.json @@ -0,0 +1,8 @@ +{ + "ConnectionStrings": { + "DefaultConnection": "" + }, + "AppSettings": { + "DefaultLocale": "en-US" + } +} \ No newline at end of file diff --git a/C969Project/locale/en-US.json b/C969Project/locale/en-US.json new file mode 100644 index 0000000..3888208 --- /dev/null +++ b/C969Project/locale/en-US.json @@ -0,0 +1,6 @@ +{ + "login_text": "Login", + "password_text": "Password", + "username_text": "Username", + "login_error_message": "Invalid username or password" +} \ No newline at end of file diff --git a/C969Project/locale/is-IS.json b/C969Project/locale/is-IS.json new file mode 100644 index 0000000..30d0f7d --- /dev/null +++ b/C969Project/locale/is-IS.json @@ -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" +} \ No newline at end of file