1

My project requires me to have a multilingual feature, so using Global.resx I have a list of English terms and in Global.fr.resx I have a list of French terms. This works perfectly. I can call in the term I require by using @Resources.Global. Whatever.

However, I now need to display these terms dynamically from the RESX file in a foreach, from values gathered from the entity framework. Any idea?

Bit more info:

The code is very simple, I'm still learning C#, I'm working in the View, so literally I have @Resources.Global.Whatever to display static terms and then I have a foreach loop:

<ul>

@foreach (var category in Model.categories)

{ <li>@category.categoryName</li> }

</ul>

Where I want @category.categoryName to be called from @Resource.Global too.

Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
  • Show your problematic code, it's in a View ? You mean for example using an `Html.DisplayFor` and get value from resx, or what ? – Raphaël Althaus Feb 01 '13 at 10:13
  • How is EntityFramework related? What do you want the `foreach` for? Also: take a look at http://stackoverflow.com/questions/2041000/loop-through-all-the-resources-in-a-resx-file – istepaniuk Feb 01 '13 at 10:26

1 Answers1

1

Assuming I understand you correctly, you want to retrieve items from a resource file based on a dynamic key name (i.e. something in a variable?). You could create an extension method for this, like so (obviously replacing the namespace to the namespace of your resource file):

 public static string Resource(this string name)
 {
     return new ResourceManager(
             "YourApp.Namespace.Resources",
             Assembly.GetExecutingAssembly()
                .GetString(name) ?? name;
 }

Then for a foreach, you could do this (assuming yourList is a list of strings):

foreach (string item in yourList)
{
    item = item.Resource();
}

Or even just using Linq?

List<string> resourceStrings = yourList.Select(s => s.Resource()).ToList();
Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148