Localización en la aplicación MVC3

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?

Noticias

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?

preguntado el 28 de agosto de 12 a las 11:08

4 Respuestas

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 diseñador.cs file which got generated when adding the resource file. Make sure the Herramienta personalizada for the resource file is set to PublicResXFileCodeGenerator. This can be done in the Properties window for the resource file.

After the update of the original question:

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());

sin que importe

public class CustomMetadataValidationProvider : DataAnnotationsModelValidatorProvider
{

}

You might also be interested in the way Huerto CMS solves it. They make use of .po files.

Respondido el 20 de junio de 20 a las 10:06

thanks, but can you give me an exeample how cam I get correct value of some property in my view? - revoluciónkpi

Show what in the view? If you have a custom attribute and hook it up into MVC it should behave like for example the DisplayAttribute you would normally make use of. - Kris van der Mast

In controller it should be a logic that will change value of each property in model by the localization key? - revoluciónkpi

If you go for the formula of the extra provider then you only need to register it in the global.asax and you're good to go. It's better to not mix this in into your controllers no. Hence the attributed way of working out of the box in MVC. Just wondering why you suddenly want to go for a .xml approach instead of resource files. - Kris van der Mast

You could use resource files to store localized versions of the strings. Checkout the siguiente guía.

Respondido 28 ago 12, 11:08

If you decide to go against the standard way to localize an application you will also have to write custom data annotation attributes that will read from your custom XML file. - darin dimitrov

In controller it should be a logic that will change value of each property in model by the localization key? - revoluciónkpi

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

Respondido 28 ago 12, 11:08

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; }
}

Si utiliza la opcion de custom metadata providers in my Griffin.MvcContrib: http://www.codeproject.com/Articles/352583/Localization-in-ASP-NET-MVC-with-Griffin-MvcContri

Código fuente: https://github.com/jgauffin/griffin.mvccontrib

Noticias

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);

Respondido 28 ago 12, 13:08

thanks, but in my case I have some .xml files and I need to get value from them, also I m not using validation in my model - revoluciónkpi

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.