Wednesday, August 24, 2016

Working with Resource Files in ASP.NET MVC

If you need to localize your ASP.NET MVC Web Application, then you will probably need to use Resource files.

You have to start by first adding a Resources File to your Web Application:

The primary resource file should be named without a language code to specify the default language (ex: DefaultResources.resx) For many organizations, the default resource file will correspond to the en-US language resource file.

Once it has been added to your Web Application, you will need to make sure that you change the "Access Modifier" to "Public".  By default, the Access Modifier is set to "No code generation".  If this is left at the default, the Resource file will not be available to use within the application as a strongly typed file:

Next, you need to add additional Resource files for each additional language that you are planning to support:

The list of language codes that you can support for your application are listed here:

Next, on the properties for the Resource files, you will have to change the "Build Action" from "Content" to "Embedded Resource":

Once you have done that, the Resource File should now be strongly typed for use within your ASP.NET MVC Web Application and you should be able to reference it as follows throughout your Controller Code and Razor Views: ex: App_LocalResources.DataAnnotation_Localization.RequiredMessage

That is all there is to it!!

No comments:

Post a Comment