tbf/TestBenchFramework/PreferencesDlg.cs

136 lines
3.6 KiB
C#
Raw Normal View History

///
/// Copyright (c) 2013-2015 Sensus Metering Systems
///
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using TBF.Resources;
namespace TBF
{
/// <summary>
/// Dialog to edit local settings that specify UI preferences, language, etc. (S1.1)
/// </summary>
public partial class PreferencesDlg : Form
{
string[] LanguageName = new string[]
{
"English",
"Česky Czech)",
"中国 (Chinese simplified)",
"Deutch (German)",
"Español (Spanish)",
"Français (French)",
"Italiano (Italian)",
"Polski (Polish)",
"Română (Romanian)",
"Русский (Russion)",
"Slovensky (Slovak)",
};
/// <summary>
/// Available languages.
/// Method ToString should give a supported culture info string:
/// ("en","de","fr","es","it","cn", etc.).
/// </summary>
string[] LanguageCode = new string[]
{
"en",
"cs",
"zh-CN",
"de",
"es",
"fr",
"it",
"pl",
"ro",
"ru",
"sk",
};
// Local copy of 'local settings' initialized to Program.localSettings.
// Dialog modifies this copy and updates Program.localSettings just
// when 'OK' is pressed.
LocalSettings localSettings;
public PreferencesDlg()
{
Program.LocalSettings.Save();
localSettings = LocalSettings.Load(Program.LocalSettingsFileName);
if (localSettings == null) throw new Exception("Cannot load local settings");
InitializeComponent();
// Initialize the list box with langauges
for (int i = 0; i < LanguageName.Length; i++)
{
languageListBox.Items.Add(LanguageName[i]);
if (localSettings.Language == LanguageCode[i])
{
languageListBox.Text = LanguageName[i];
}
}
}
private void PreferencesDlg_Load(object sender, EventArgs e)
{
Text = Strings.Language;
okButton.Text = Strings.OkBtnText;
cancelButton.Text = Strings.CancelBtnText;
}
private void languageListBox_SelectedIndexChanged(object sender, EventArgs e)
{
localSettings.Language = LanguageCode[languageListBox.SelectedIndex];
}
/// <summary>
/// Update setting, return DialogResult.OK
/// </summary>
/// <param name="sender">Not used</param>
/// <param name="e">Not used</param>
private void okButton_Click(object sender, EventArgs e)
{
try
{
string culture = localSettings.Language.Replace('_', '-');
CultureInfo ci = new CultureInfo(culture);
System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
localSettings.Save();
Program.LocalSettings = LocalSettings.Load(Program.LocalSettingsFileName);
DialogResult response = MessageBox.Show(Strings.Restart_To_Apply_Changes);
DialogResult = DialogResult.OK;
Close();
}
catch
{
MessageBox.Show("Selected language not supported.\nUsing English.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
localSettings.Language = "en";
System.Threading.Thread.CurrentThread.CurrentUICulture =
new System.Globalization.CultureInfo(localSettings.Language);
}
return;
}
/// <summary>
/// Throw away changes, return DialogResult.Cancel
/// </summary>
/// <param name="sender">Not used</param>
/// <param name="e">Not used</param>
private void cancelButton_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
return;
}
}
}