Reputation: 13
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.
Upvotes: 1
Views: 2010
Reputation: 56429
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();
Upvotes: 1