Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I display the same view in other languages? The code below puts a textbox on the screen with a label in English. That’s nice, but limited. How do I display the same view in Spanish, German…?
My .Net MVC model contains property attributes ‘Display’ and ‘Required’.

Model…
[Required(ErrorMessage = "E-Mail is required")]
[DataType(DataType.EmailAddress)]
[Display(Name = "E-Mail:")]
public string Email { get; set; }

View…
@Html.LabelFor(m => m.Email)

@Html.TextBoxFor(m => m.Email, new { @class = "requiredField"})

@Html.ValidationMessage("Email", "*")


Thank you for your time and consideration.
Posted

You can create your own attribute class that derive from predefined one and the override appropriate function to load string from a resource.

Although I haven't tried it, it might also be possible to write your helper function like TextBoxFor and then load appropriate string. Resource key might be composed of the field name and a "function" suffix like Email_Required in the example above so that it can be done automaticaly.

Also, I have compared the performance of any solution.

In my project, I use the approch of having derived atribute classes for each pair of attribute/resource file that I use.

Say LoginDisplayNameAttribute(string key) would get a string from LoginDisplayResource using key. If the key is not found, then you might have some backup logic to try other locations or use the key itself as text.

Depending on how you compile or copy resources, it might be more or less easy to deal with missing resource as in some case an exception might be thrown if the key does not exist which might make debugging somewhat harder (you either have to deal with a lot of exception or it might not stop at other places when the exception really matters for debugging purpose...)

Thus if resource are not compiled but simply include, it might be best to use a signle source for each attribute class.

Most of this stuff can be found on the web as there are a lot of example for ASP.NET available.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900