Introduction
This article discusses a method of localizing Application Settings
(Settings.settings
). Application Settings are a Microsoft Visual Studio 2005 feature which allow one to store and retrieve property settings and other information
for one's application dynamically. To localize application settings, generally resource files are used and there are many great articles
on localization with resource files,
such as ".NET - Localization using Resource file"
by Prakash Kumar Singh. Yet, for small applications or other situations, it might be nice
to just localize the Application Settings, enabling one to make use of localized
string collections and the ease of modifying XML files outside of the application.
This idea probably works the best with Applications Settings of the Application scope, such as displaying error messages, warnings, or information in the current language. Yet, I suppose that
it could also be used with local scope Application Settings, such as saving user data in different languages, depending upon what language the user selected.
As a little example, I created a LocalizedGrid, which loads the grid column titles, grid data, and populates a combo box from the Application Settings
according to the current culture which is set by selecting the language in the combobox:
private void Form1_Load(object sender, EventArgs e) {
comboBox1.DataSource = Properties.Settings.Current.Languages;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
if (!changingIndex) {
changingIndex = true;
System.Threading.Thread.CurrentThread.CurrentCulture
= System.Globalization.CultureInfo.CreateSpecificCulture(
(string)comboBox1.Items[comboBox1.SelectedIndex]);
comboBox1.DataSource = Properties.Settings.Current.Languages;
dataGridView1.ColumnCount = Properties.Settings.Current.Columns.Count;
for (int count = 0; count < dataGridView1.ColumnCount; count++)
dataGridView1.Columns[count].HeaderText =
Properties.Settings.Current.Columns[count];
dataGridView1.Rows.Clear();
for (int count = 0; count < Properties.Settings.Current.Values.Count; count++)
dataGridView1.Rows.Add(Properties.Settings.Current.Values[count].Split(';'));
changingIndex = false;
}
}
Background
I couldn't find any document or article on localizing Application Settings, so I decided to design my own.
Using the code
Settings.cs localizes Settings.settings by loading "Properties\de-CH\Settings. settings when "de-CH" is the current culture.
To localize Settings.settings
, follow these steps:
- Add Settings.cs to your project and drag it into the Properties folder.
- Create a folder for each language, such as "de-CH", and drag it into the "Properties" folder.
- Drag and drop or copy and paste
Settings.settings
into the new language folder (in this case, "Properties\de-CH"). - Translate the string settings in the new Application Settings file ("Properties\de-CH\Settings.settings").
- Edit Settings.cs and add a namespace and partial
Settings
class which inherits ISettings
for each language.
#warning Use the namespace for your project
namespace CodeProject.Properties.de_CH {
partial class Settings : ISettings { }
}
- Edit Settings.cs and add each Application Setting to the
ISettings
interface that you want to localize.
#warning Use the namespace for your project
namespace CodeProject.Properties {
interface ISettings {
string Greeting { get; }
}}
That's it. Now, to access the localized setting, just call Properties.Settings.Current
instead of Properties.Settings.Default
:
Console.WriteLine(Properties.Settings.Current.Greeting);
System.Threading.Thread.CurrentThread.CurrentCulture
= System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
Console.WriteLine(Properties.Settings.Current.Greeting);
Output when changing the current culture.
Settings.cs
Settings.cs contains an interface, ISettings
, which is used to connect all of the language-specific Settings.settings
together. It contains
partial classes for each language Settings.settings
class to connect the ISettings
interface. The ISettings
class contains a property for each
Application Setting which is translated, and the partial Settings
class, "Properties\Settings.cs", contains the "Current
" property which returns
the CurrentCulture
ApplicationSettingsBase
.
Multiple Localized Application Settings
For multiple application files, just repeat the same steps above and rename "Settings" in the new files to the Application Settings name, such as "OtherSettings",
OtherSettings.cs (instead of Settings.cs).
#warning Use the namespace for your project
namespace CodeProject.Properties.de_CH {
partial class OtherSettings : IOtherSettings { }
}
#warning Use the namespace for your project
namespace CodeProject.Properties {
interface IOtherSettings {
string Language { get; }
}
partial class OtherSettings : IOtherSettings {
public static IOtherSettings Current {
get {
SettingsBase result = null;
string culture =
System.Globalization.CultureInfo.CurrentCulture.Name.Replace("-", "_");
Type type = typeof(OtherSettings);
Type cultureType = Type.GetType(String.Concat(
type.Namespace, ".", culture, ".", type.Name));
if (cultureType != null)
result = (SettingsBase)Activator.CreateInstance(cultureType);
else
result = (SettingsBase)Activator.CreateInstance(type);
return ((IOtherSettings)(ApplicationSettingsBase.Synchronized(result)));
}
}
}
}
History
- 2.10.07: Improved the introduction and added LocalizedGrid_src.zip and LocalizedGridSampleApp.zip.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.