7

I am just extending my this question a bit.

I have my App_LocalResources in my MVC web application (I don't have it in separate dll).

I have a my Model in different assembly. In the Model I have 2 classes Country and City:

public class Country: MyContainer
{
    public City city {get;set;}
} 

public class City
{
    public CityName {get;set;}
}

public class MyContainer
{
    public Cid {get;set;}
}

So in my action method I create and pass an object of country as my viewmodel.

And in the view I use this:

@Html.LabelFor(mdl=>mdl.City.CityName)
@Html.LabelFor(mdl=>mdl.Cid)

So this works well and label with text are rendered in English.

But how do I modify this so that it reads the text from my Resource files in my web application?

Community
  • 1
  • 1
thinkmmk
  • 487
  • 1
  • 8
  • 22

3 Answers3

14

You could write a custom display attribute:

public class LocalizedDisplayNameAttribute : DisplayNameAttribute
{
    public LocalizedDisplayNameAttribute(string key): base(FormatMessage(key))
    {
    }

    private static string FormatMessage(string key)
    {
        // TODO: fetch the corresponding string from your resource file
        throw new NotImplementedException();
    }
}

and then:

public class City
{
    [LocalizedDisplayName("cityname")]
    public string CityName { get; set; }
}

You may also checkout the following localization guide. It provides a full implementation of a sample attribute.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    Thanks Darin for your reply, I got that piece of code. But the issue now is that my Country,City and MyContainer classes are in different project (its named Location.Model). And my resource file is in my mvc3 web application project. The Location.Model seems to me is autogenerated code, coz in the cs files theres comment " // This code was generated from a template." and also it has some .tt file in it. So please help me to figure out where and how I do the changes. Do i need to seperate my Resources project ... please help.. – thinkmmk Mar 16 '12 at 06:51
  • @thinkmmk, you should not pass use autogenerated models to your views. I suppose that's some EF autogenerated domain model. You should always define view models which are classes specifically designed for a given view. – Darin Dimitrov Mar 16 '12 at 06:53
  • I got your point, but this project seems to be quite complex and vast . So how do i need to move ahead with minimal changes to be made. Do I need to create a new object of a new class and pass that as a model? But the thing is this will create a duplicate class. – thinkmmk Mar 16 '12 at 07:14
  • @thinkmmk, normally view models are not exact duplicates of your domain models. They are specifically adapted to the view requirements. Personally I always design view models and never pass any domain models to the views. – Darin Dimitrov Mar 16 '12 at 07:17
  • Due to some reason if suppose I cant create a seperate ViewModel, then what else can I do. Is there any other way like dynamically setting the attributes of the property in the Controller. – thinkmmk Mar 17 '12 at 04:48
  • Hi darin, one way I found a way for decorating the auto-generated model class without really touching them is through MetaDataType. http://www.codeproject.com/Articles/148486/Adding-Metadata-to-Entities-in-The-Data-Model – thinkmmk Mar 18 '12 at 14:23
  • There's no need to create a new attribute. The system one, Display, works just fine. See my answer for an example. – pipedreambomb Dec 19 '16 at 15:01
7

You can use [Display(ResourceType = typeof(App_LocalResources), Name = "AgreeTandCs")] where App_LocalResources is the name of the resource class (your .resx) and Name is the name of the static string you want to reference. Use LabelFor as usual in your view, and it will automagically pull in your resource.

In my example, the label displays the string stored with the variable name AgreeTandCs, and if you're viewing it in English it will be shown in the page as "I agree to these Terms and Conditions".

pipedreambomb
  • 3,711
  • 1
  • 22
  • 18
0

You can also use translation with {0}, {2} parameters inside translation string. He is my example Localize Compare attribute

Community
  • 1
  • 1
Rafal
  • 209
  • 2
  • 3