This toolkit also works with ASP.NET MVC although you are likely to use the functionality slightly differently for MVC than for Web Forms.

The toolkit provides the following uses for MVC applications:

  • Interactive Resource Editing by importing and exporting resources to and from Resx
  • Localized JavaScript access to ASP.NET Resources from MVC pages

MVC applications work best by NOT using the ASP.NET resource architecture of App_LocalResources and App_GlobalResources. Because MVC doesn't use controls any of the control lookup benefits of declarative markup that the Web Forms based provider system provides is lost in MVC applications. Instead MVC applications tend to work best with traditional resources defined in your .NET projects and exposed as strongly typed resources.

The benefit this toolkit provides is primarily as a resource editing component rather than by running the data base resource manager. Due to limitations on how Visual Studio generates strongly typed resources you cannot easily switch the ResourceManager on these resource types. So rather than running the database resources you can import Resx resources, edit them and eventually export them again to Resx. This way you get interactive edit support along with standard compiled in resources or satellite resource assemblies.

The process to use the Administration interface with MVC looks like this:

Assembly References

You'll need to add 3 Assembly references in order to use the globalization provider:

  • Westwind.Globalization
  • Westwind.Web
  • Westwind.Utilities

You may also need add a reference to System.Configuration.

Modify Web.Config

Make sure you add the following to web.config:

<configSections> <!-- Custom Sections -->Ý <section name="DbResourceProvider" type="Westwind.Globalization.DbResourceProviderSection,Westwind.Globalization" requirePermission="false"/> <!-- End Custom Sections -->Ý </configSections>

Then add the actual dbProvider configuration. For MVC application using Resx resources this configuration will be only for the administrative interface, not for using the data driven provider at runtime:

<!-- Using ConnectionStrings entry. You can also use a full SQL connection string. -->Ý <DbResourceProvider connectionString="CodePasteConnectionString" projectType="Class" resourceTableName="Localizations" designTimeVirtualPath="/codepastemvc" showLocalizationControlOptions="true" showControlIcons="true" localizationFormWebPath="~/localizationadmin/LocalizationAdmin.aspx" addMissingResources="false" useVsNetResourceNaming="false" stronglyTypedGlobalResource="~/Resources.cs,CodePasteMvc.Resources" /> <!-- ResourceProvider Configuration section -->Ý

The important setting for an MVC is the projectType="Class" which means resources will not be treated as local or global resources as they are in Web Form projects. Rather resources are imported by their resource paths using the same resource naming that Visual Studio uses for resource namespaces and type names (ie. projectname.path.resourcefilename).

Copying the Administration Form

The LocalizationAdmin interface is provided as a fully self contained folder using a WebForms page. The admin interface is a single page that can be dropped anywhere in your MVC project. Whereever you do drop it make sure to set the localizationFormWebPath to point to that location where the actual LocalizationAdmin.aspx form lives.

You can find the LocalizationAdmin folder in the sample Westwind.GlobalizationWeb sample application.Simply copy the LocalizationAdmin folder and drag it into your MVC application at any location you choose.

Although this is an ASPX WebForm interface it will run just fine in your MVC project. The page, is fully self contained in the folder structure which includes support script, style sheet and resources for english and german.

Limitations of running in MVC

Unlike the WebForms interface there's easy way to run the data driven resource provider/resource manager in MVC (or any other project). This is due to the nature of the Microsoft ResXFileCodeGenerator and PublicResXFileCodeGenerator which generate hard coded resource provider references.

The recommended procedure for resource editing is a follows:

  • Create your base resource files in Visual Studio - can be empty but each should exist
  • Use the Admin interface to Import Resources from Resx
  • Edit your resources in the interactive editor
  • Use the Admin interface to Export Resources back to Resx

To use Resources in MVC applications make sure you set up the Custom Tool option (Properties | Custom Tool) to use the PublicResXFileCodeGenerator to ensure your resources are accessible in MVC pages. To use a Resx Resource then is as easy as:

<%= Html.Encode( CodePasteMvc.Resources.Resources.SNIPPET_SAVED) %>

You can simplify this by adding the namespace to your common namespaces:

<pages> <namespaces> <add namespace="System.Web.Mvc" /> <add namespace="System.Web.Mvc.Html" /> <add namespace="System.Web.Routing" /> <add namespace="CodePasteMvc.Resources" /> </namespaces> </pages>

With this in place the syntax is a little easier:

<%= Html.Encode(Resources.SNIPPET_SAVED) %>

Using Resource Strings in JavaScript using JavaScriptResourceHandler

Finally you can also utilize server resources on the client through JavaScript by using the JavaScriptResourceHandler. This class allows you to specify a variable name to which all resources in a given resource set are assigned appropriately normalized for the current locale.

First you have to set up the handler in your web.config file:

<handlers> <add name="JavaScriptResourceHandler" verb="GET" path="JavaScriptResourceHandler.axd" type="Westwind.Globalization.JavaScriptResourceHandler,Westwind.Globalization"/> </handlers>

Then in any page that you want to have client resource strings embedded as an external JavaScript resource you can embed the following script into the page to load the resources:

<script src="<%= JavaScriptResourceHandler.GetJavaScriptResourcesUrl("globalResources","CodePasteMvc.Resources.Resources") %>" type="text/javascript"></script>

(JavaScriptResourceHandler lives in Westwind.Globalization if you manually need to add the namespace)

You specify the name an object that is to be created and each of the resource keys hangs off of it as a property:

var globalResources = { CouldNotCreateNewCustomer: "Neuer Kunde konnte nicht erstellt werden", CouldNotLoadCustomer: "Kunde konnte nicht erstellt werden", CustomerSaved: "Kunde wurde gespeichert", Today: "Heute", Yesterday: "Gestern" };

You can access multiple resource files from a single page as long as each is given a unique name. This feature is highly convenient for sharing ASP.NET resources both on the server and the client from the same localization store.

For more information please check the Localized JavaScript Resources topic.