Search code examples
c#.netasp.net-mvc-4resx

Resource File lookup with variable


Project type: C# Mvc 4

Hi all I was wondering if anyone with experience in Resource files can help me:

I have a basic setup for string localization so I have created resource files for different cultures and they work fine, i.e. In the view if you put:

@Resources.Title

It successfully looks up the string with the key title in the resource file respective to the active culture on your browser.

The question I have is if it is possible to do the lookup dynamically e.g.:

@Resources.Model.Title where Model.Title is a variable known at runtime

Thanks in advance


Solution

  • You can use the following:

    @Resources.ResourceManager.GetString(Model.Title)
    

    There is also an overload where you can supply a CultureInfo to get the string in a different language then the current one. Otherwise the culture from the Thread.CurrentThread.CurrentUICulture is used.