Installation for .NET 4.5+

Full Framework ASP.NET uses the web.config Configuration file for configuration.

If you installed the NuGet package the package manager automatically installed and formatted a default configuration configured for SQL Server with an invalid Connection String that you have to fill in.

Here's what the configuration looks like with a valid connection string.

<configuration>
  <configSections>
    <section name="DbResourceConfiguration" requirePermission="false" 
			 type="System.Configuration.NameValueSectionHandler,System,Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  </configSections>  

  <DbResourceConfiguration>
    <!-- ConnectionString can be a fully qualified cn or a connection string name -->
    <add key="ConnectionString" value="server=.;database=Localizations;integrated security=true;" />
    <add key="DataProvider" value="SqlServer" />
    <add key="ResourceTableName" value="Localizations" />
    <add key="AddMissingResources" value="False" />

    <!-- Resource Imports and Exports -->
    <add key="ResxExportProjectType" value="Project" />
    <add key="StronglyTypedGlobalResource" value="~/Properties/Resources.cs" />
    <add key="ResourceBaseNamespace" value="WebApplication1.Properties" />    
    <add key="ResxBaseFolder" value="~/Properties" />

    <!-- WebForms specific only -->
    <add key="LocalizationFormWebPath" value="~/LocalizationAdmin/" />
    <add key="DesignTimeVirtualPath" value="" />
    <add key="ShowLocalizationControlOptions" value="False" />
    <add key="ShowControlIcons" value="False" />

    <!-- Google Translate API -->
    <add key="GoogleApiKey" value="" />

    <!-- Bing Translation -->
    <add key="BingClientId" value="" />
  </DbResourceConfiguration>

  <!-- Enable ASP.NET Resource Provider  -->
  <system.web>
    <globalization resourceProviderFactoryType=
     "Westwind.Globalization.DbSimpleResourceProviderFactory,Westwind.Globalization.Web" />
  </system.web>
</configuration>

Overriding Configuration Settings in code (Full Framework)

To override configuration settings that are set in web.config you can access the DbResourceConfiguration.Current instance that's used to configure the application.

protected void Application_Start()
{                       
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);

    // Specify where config information comes from (config file is default - can be separate Json/Xml)
    //DbResourceConfiguration.ConfigurationMode = ConfigurationModes.ConfigFile;
    
    var config = DbResourceConfiguration.Current;
    
    config.ConnectionString = "SqlServerLocalizations";
    config.DbResourceDataManagerType = typeof(DbResourceSqlServerDataManager);

    //DbResourceConfiguration.Current.ConnectionString = "MySqlLocalizations";
    //DbResourceConfiguration.Current.DbResourceDataManagerType = typeof(DbResourceMySqlDataManager);

    // force ResourceMode explicitly. Default is AspNetResourceProvider
    GeneratedResourceSettings.ResourceAccessMode = ResourceAccessMode.DbResourceManager;
    //GeneratedResourceSettings.ResourceAccessMode = ResourceAccessMode.Resx;

    // *** Remove or Add custom resource converters
    // *** By default the MarkdownResourceConverter is provided
    //DbResourceConfiguration.Current.ResourceSetValueConverters.Clear();
    //DbResourceConfiguration.Current.ResourceSetValueConverters.Add(new MarkdownResourceSetValueConverter());
}

Settings made here occur before any requests are made to the providers, so Application_Start is a good way to initialize settings, including selecting a provider. Any of the Configuration values can be set here, or anywhere else by using the static DbResourceConfiguration.Current instance.

Setting ASP.NET Locale based on Browser Locale (Full Framework)

In order to do automatic localization based on a browser's language used you can sniff the browser's default language and set the UiCulture in the Begin_Request handler of your ASP.NET application class. A helper method to provide this functionality automatically is provided.

protected void Application_BeginRequest()
{
    // Automatically set the user's locale to what the browser returns
    // and optionally only allow certain locales/locale-prefixes
    WebUtils.SetUserLocale(allowLocales: "en,de");
}

This forces the user's Culture and UI Culture to whatever the browser is using, and explicitly. Now when a page is rendered it will use the UiCulture of the browser. The optional allowLocales enforces that only certain locales can be set - anything not matched is defaulted to the server's default locale.

The way .NET resource managers work, if there's no match for the locale the user provides, resources fall back to the closest matching locale or the invariant locale. So if the user comes in with it-IT but you don't support it or it-IT in your resources the user will see resources for invariant. Likewise if a user comes in with de-CH (Swiss german) and you de (without a locale specific suffix) the de German version will be returned. Resource Fallback tries to ensure that always something is returned.


© West Wind Technologies, 2006 - 2019 • Updated: 01/24/18
Comment or report problem with topic