tradez tradez - 25 days ago 7
C# Question

Can I call an entry in a resource file using a method or variable?

I have several resource files built for a multilingual MVC 5 site I am working on.

I am using a CMS and the user can select the button text on certain content segments, like "Download" or "View More".

Rendering a value from my resource file using

@Resources.Resource.ViewMore
in my view works fine, but since the content is dynamic depending on what button text the user selects in the CMS, I need to use something like
@Resources.Resource.@Model.getButtonText()
but obviously that will not work.

Any suggestions on how to handle this?

Answer

You can use ResourceManager class for this

@{    
    ResourceManager rm = new ResourceManager(typeof(Resources.Resource));
    @rm.GetString(Model.getButtonText());
}