Phil Haack has an excellent ASP.Net MVC Validation Localization article that specifically helps you redefine your strings. This article relates more to DataAnnotations than to ASP.net MVC . This way, it will help you use DataAnnotattions.
The following are the simplest steps to add localized resources in Visual Studio.
- Open the
Project Properties dialog box. - Select the
Resources tab. - Click to create a new default resource file.
- This will create two files in your
Properties folder.- Resources.resx
- Resources.Designer.cs
- When Resource.resx has an open, change its
Access Modifier to Public . - Add your lines.
To add additional resource files for specific cultures, you will need to.
- Right-click your
Project in Solution Explorer . - Choose Add β New Item β Resource File.
- Name it
Resources.en-us.resx . (replace "en-us" with the appropriate code) - Click Add
- Drag it to the
Properties folder. - Open Resources.en-us.resx and change its
Access Modifier to Public . - Add your lines.
- Repeat for each Culture, support.
During build, VS will convert the .resx files to .resource files and create wrapper classes for you. You can then access through the YourAssembly.Properties.Resources namespace.
With the help of this instruction.
using YourAssembly.Properties;
You can decorate with the following attributes:
[Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "MyStringName")]
Note. I used the Properties folder to ensure consistency. To use App_GlobalResources, move the .resx files there and modify the using statement to match the directory name. Like this:
using YourAssembly.App_GlobalResources;
Edit: closest to what you can get for strongly typed resource names would be to do something like this:
public class ResourceNames { public const string EmailRequired = "EmailRequired"; }
Then you can decorate with such attributes.
[Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = ResourceNames.EmailRequired)]
To enable automatic client culture detection, add globalizationsection to the web.config file.
<configuration> <system.web> <globalization enableClientBasedCulture="true" culture="auto:en-us" uiCulture="auto:en-us"/> </system.web> <configuration>
Here I turned on a client-based culture and set the culture and ugliness to βautoβ with a default value of βen-usβ.
Creating separate satellite assemblies:
At MSDN, building satellite assemblies will also help. If you are new to satellite builds, make sure you read Packaging and Deploying Resources .
When creating satellite assemblies in the past, I found it useful to use VS build events. These are the steps that I would take.
- Create a separate
Class Library project in my solution. - Create or add my
.resx files to this project. - Add a
Post-Build Event to the Project Properties . (As below)
VS Post-Build Script example:
set RESGEN="C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\resgen.exe" set LINKER="C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\al.exe" set ASSEMBLY=$(TargetName) set SOURCEDIR=$(ProjectDir) Set OUTDIR=$(TargetDir) REM Build Default Culture Resources (en) %RESGEN% %SOURCEDIR%en\%ASSEMBLY%.en.resx %SOURCEDIR%en\%ASSEMBLY%.resources REM Embed Default Culture %LINKER% /t:lib /embed:%SOURCEDIR%en\%ASSEMBLY%.resources /culture:en /out:%OUTDIR%%ASSEMBLY%.resources.dll REM Embed English Culture IF NOT EXIST %OUTDIR%en\ MKDIR $%OUTDIR%en\ %LINKER% /t:lib /embed:%SOURCEDIR%en\%ASSEMBLY%.resources /culture:en /out:%OUTDIR%en\%ASSEMBLY%.resources.dll REM These are just a byproduct of using the project build event to run the resource build script IF EXIST %OUTDIR%%ASSEMBLY%.dll DEL %OUTDIR%%ASSEMBLY%.dll IF EXIST %OUTDIR%%ASSEMBLY%.pdb DEL %OUTDIR%%ASSEMBLY%.pdb
If you do not want to use ResGen.exe to convert your .resx files, you can do something like this.
using System; using System.Collections; using System.IO; using System.Resources; namespace ResXConverter { public class ResxToResource { public void Convert(string resxPath, string resourcePath) { using (ResXResourceReader resxReader = new ResXResourceReader(resxPath)) using (IResourceWriter resWriter = new ResourceWriter( new FileStream(resourcePath, FileMode.Create, FileAccess.Write))) { foreach (DictionaryEntry entry in resxReader) { resWriter.AddResource(entry.Key.ToString(), entry.Value); } resWriter.Generate(); resWriter.Close(); } } } }
One possible way back to perform the conversion in this way is to need a reference to System.Windows.Forms.dll . You still have to use Assembly Linker .
Edit: since wRAR reminded us that you are signing your assemblies, your keys must match .