views:

93

answers:

4

I have something like this in mind:

<appSettings>
 <add key="ConfigName" value="configuration" culture="1033" />
 <add key="ConfigName" value="konfiguracja" culture="1045" />
</appSettings>

but the add element has only 2 attributes - key and value, so I guess it's not supported.

Next thing that comes to my mind is:

<appSettings>
 <add key="ConfigName-1033" value="configuration" />
 <add key="ConfigName-1045" value="konfiguracja" />
</appSettings>

Can anybody suggest a better solution?


UPDATE - solution I implemented:

The downside of moving this information to resource files (Oded's answer) is the fact that it can no longer be easily modified on developers' and testers' machines.

However, here's what I did - I left the settings in the web.config file (they cannot be localized, but they can be modified with no need to recompile the code) and added them to resource files (they can be localized; they are be used in production environment only, where the web.config settings are set to empty strings).

+4  A: 

You should not store localization data in config files.

Use satellite assemblies and resource files for localization.

If all you have is a small number of items, holding things in config could be OK, but experience suggests that the number of items will grow until this will end up being a maintenance nightmare.

See this guide (Globalization and localization demystified in ASP.NET 2.0) for more details.

Oded
I wonder who downvoted this excellent answer.
klausbyskov
@klausbyskov: I did, because this answer doesn't answer the question (no offense to @Oded, of course). While the information presented here is spot on, it's pretty unresponsive; the question was how to localize appSettings, not "What's the best way to store localized data".
Ben Collins
I wonder too. This is the only correct answer. Also things that are never exposed to the user, shouldn't be localized anyway. "ConnectionString" should always be "ConnectionString"..One thing I hate the most is developers localizing code. Code should be English, period.
mare
@Ben Collins - when experience tell you that something a fellow programmer is trying to do is plain wrong, it is you duty to point it out to them. SO is also about _educating_ each other about best practices not persisting bad/incorrect practice.
Oded
@mare: I'm not trying to localize code here - I simplified the example, so it's not obvious, but the setting I need to localize stores a URL. It's stored in the `web.config` file because it's different in the test and production environment.
Marek Grzenkowicz
@Oded - while I don't really disagree, there are times at which there is a reason to do things that would normally seem "just wrong". I try to be as charitable as possible in answering SO questions. There may be any number of reasons that @Marek wants to do things this way (although he explained in the previous comment).
Ben Collins
+2  A: 

If it's really only a few settings, your suggestion above would work just dandy. I don't think there's any inherent support in ASP.NET for localizing configuration settings.

Ben Collins
A: 

Why not have an appsettings.culture file and copy it to your application folder. Based on the culture of the application you can override the appconfig with the correct localized app settings file.

Ikaso
Can I access this file automatically, i.e. set current culture for the appSettings and then use `WebConfigurationManager.AppSettings`?
Marek Grzenkowicz
I think that you can do that using WebConfigurationManager.OpenWebConfiguration(configPath).
Ikaso
A: 

Add this to the configSections section of your web.config:

<section name="ConfigNames" type="System.Configuration.NameValueSectionHandler" />

Add this to your web config outside of the <system.web>...</system.web> section:

<ConfigNames>
  <add key="configuration" value="1033"/>
  <add key="konfiguracja" value="1045"/>
</ConfigNames>

Then you can access you settings like this:

var configNames = (NameValueCollection)ConfigurationManager.GetSection("emailLists");
//and get at them however you like:
var culture = configNames["konfiguracja"];
BritishDeveloper
This is semantically (logically) wrong. If anything, this should be done like this: <add key="Configuration_1033" value="Configuration"/> <add key="Configuration_1045" value="Konfiguracja"/>. You don't localize keys (??), you localize values - that's the right pattern and you can see it also outside of web.config, like in resource files.
mare
I dont know what he wants it for - I assumed the number was the value. I gave an example of something that WILL work. Rather than having a million lines in your appSettings it's nice to keep different settings separate - if you don't want to that's fine, it is merely a suggestion but this DOES work so can't possibly be 'wrong'
BritishDeveloper
@BritishDeveloper: _configuration_ and _konfiguracja_ are values of the _ConfigName_ setting for 2 different cultures (1033 and 1045).
Marek Grzenkowicz
cool. well in that case you want <add key="1033" value="configuration"/> and <add key="1045" value="konfiguracja"/>. And you can access a value with configNames["1033"]. Anyway the method I used still stands - I prefer to logically separate batches of similar configurations in this way, rather than putting them all in the same place with every other setting
BritishDeveloper