Click here to Skip to main content
13,000,829 members (56,772 online)
Click here to Skip to main content
Add your own
alternative version

Stats

23.3K views
409 downloads
27 bookmarked
Posted 1 Oct 2007

Localized Application Settings

, 1 Oct 2007
Rate this:
Please Sign up or sign in to vote.
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Daniel Kuettel
Software Developer (Senior)
Switzerland Switzerland
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 2 Pin
Aniket S Kulkarni2-Apr-12 6:54
memberAniket S Kulkarni2-Apr-12 6:54 
GeneralNice work! Pin
Member 342384718-Apr-08 10:50
memberMember 342384718-Apr-08 10:50 
QuestionAny examples? Pin
Uwe Keim1-Oct-07 19:25
sitebuilderUwe Keim1-Oct-07 19:25 
AnswerRe: Any examples? Pin
Daniel Kuettel2-Oct-07 12:29
memberDaniel Kuettel2-Oct-07 12:29 
GeneralRe: Any examples? Pin
Uwe Keim2-Oct-07 19:39
sitebuilderUwe Keim2-Oct-07 19:39 
GeneralRe: Any examples? Pin
Daniel Kuettel2-Oct-07 22:36
memberDaniel Kuettel2-Oct-07 22:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170624.1 | Last Updated 1 Oct 2007
Article Copyright 2007 by Daniel Kuettel
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid