Reputation: 2682
In my MVC 3 Application I need to make localization. I would like to ask for some advise what is the best way to do this. And one more question. I am using the model like this in my application:
public class MyModel
{
[HiddenInput(DisplayValue = false)]
public Guid? DepartmentId { get; set; }
[Display(Name = "Caption")]
public string Caption { get; set; }
[Display(Name = "Owner")]
public string Owner { get; set; }
[Display(Name = "Enabled")]
public bool Enabled { get; set; }
}
How can I use localization in this model class?
Update
I decide to create a custom resource.xml instead of using asp.net's Resource .resx implemention, how can I use localization in this model class?
Upvotes: 4
Views: 671
Reputation: 101192
You can do it without modifying the model:
public class MyModel
{
[HiddenInput(DisplayValue = false)]
public Guid? DepartmentId { get; set; }
public string Caption { get; set; }
public string Owner { get; set; }
public bool Enabled { get; set; }
}
If you use the custom metadata providers in my Griffin.MvcContrib: http://www.codeproject.com/Articles/352583/Localization-in-ASP-NET-MVC-with-Griffin-MvcContri
Source code: https://github.com/jgauffin/griffin.mvccontrib
Update
You can use XML as a data source. Simply implement the interface ILocalizedStringProvider and load the strings from the XML file using it.
Then configure MVC like this:
var stringProvider = new MyXmlProvider(@"C:\AppData\MyStrings.xml");
ModelMetadataProviders.Current = new LocalizedModelMetadataProvider(stringProvider);
Upvotes: 0
Reputation: 16613
Make use of the following settings in the [Display] attribute:
[Display(Name = "Caption", ResourceType = typeof(SomeResource))]
Where SomeResource is the class name of the Resource file. To get the namespace and class name correct simply open the designer.cs file which got generated when adding the resource file. Make sure the Custom Tool for the resource file is set to PublicResXFileCodeGenerator. This can be done in the Properties window for the resource file.
If you go for a resources.xml file then likely an overload or a new attribute would be in place where you for example make use of an XPath expression and the location of the xml file.
You could hook up a new provider like:
ModelValidatorProviders.Providers.Add(new CustomMetadataValidationProvider());
where
public class CustomMetadataValidationProvider : DataAnnotationsModelValidatorProvider
{
}
You might also be interested in the way Orchard CMS solves it. They make use of .po files.
Upvotes: 5
Reputation: 15463
If you want to store the localized strings in custom files, rather than RESX, you can implement a resource provider. Check out the following article for more info:
http://msdn.microsoft.com/en-us/library/aa905797.aspx#exaspnet20rpm_topic4
Upvotes: 0
Reputation: 1039498
You could use resource files to store localized versions of the strings. Checkout the following guide.
Upvotes: 0