65.9K
CodeProject is changing. Read more.
Home

Localized Application Settings

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.50/5 (6 votes)

Oct 1, 2007

CPOL

2 min read

viewsIcon

32522

downloadIcon

479

An article on localizing application settings (Settings.settings).

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:

Screenshot - de-CH.jpg

Screenshot - en-US.jpg

private void Form1_Load(object sender, EventArgs e) {
    //Fills the ComboBox with the current culture language list
    comboBox1.DataSource = Properties.Settings.Current.Languages;
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
    if (!changingIndex) {
        changingIndex = true;

        //Changes the current culture to the language selected in the ComboBox
        System.Threading.Thread.CurrentThread.CurrentCulture
            = System.Globalization.CultureInfo.CreateSpecificCulture(
             (string)comboBox1.Items[comboBox1.SelectedIndex]);

        //Fills the ComboBox with the language list of the selected language
        comboBox1.DataSource = Properties.Settings.Current.Languages;

        //Recreates the columns, according to how many were listed
        //in the "Columns" application setting
        dataGridView1.ColumnCount = Properties.Settings.Current.Columns.Count;

        //Sets the column headers from the "Columns" Application Setting
        for (int count = 0; count < dataGridView1.ColumnCount; count++)
            dataGridView1.Columns[count].HeaderText = 
                          Properties.Settings.Current.Columns[count];

        //Deletes the rows from the grid
        dataGridView1.Rows.Clear();

        //Populates the grid from the "Values" Application Setting
        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

Screenshot - solution.jpg

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:

  1. Add Settings.cs to your project and drag it into the Properties folder.
  2. Create a folder for each language, such as "de-CH", and drag it into the "Properties" folder.
  3. Drag and drop or copy and paste Settings.settings into the new language folder (in this case, "Properties\de-CH").
  4. Translate the string settings in the new Application Settings file ("Properties\de-CH\Settings.settings").
  5. 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 { }
    }
  6. Edit Settings.cs and add each Application Setting to the ISettings interface that you want to localize.
  7. #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);

Screenshot - output.gif

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

Screenshot - othersettings.jpg

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.
Localized Application Settings - CodeProject