Is it possible to localize the appSettings informa

2019-02-24 09:41发布

问题:

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).

回答1:

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.



回答2:

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.



回答3:

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.



回答4:

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"];