Reputation: 204
Is it possible to include a string in a razor statement?
For example, the following code:
Page.Title = Resources.HomeStrings_en.Title;
I would like to be like so:
var locale = "_en";
Page.Title = Resources.HomeStrings + locale + .Title;
Clearly, this does not compile, but how can I go about doing this?
Upvotes: 0
Views: 183
Reputation: 14133
Resources are static classes with a property for each key, so you can't do that.
You need to use ResourceManager.
var resset= Resources.ResourceManager.GetResourceSet(Thread.CurrentThread.CurrentUICulture, true, false);
resset.GetString("HomeStrings" + "_en" + ".Title")
One thing that makes me think... is that .NET Resources handle the language change using the Culture... going to retrieve the data from the right file. And it seems you are using one file to hold all your languages... and this is not recommended.
Here you can have a tutorial of Internationalization in ASP.NET MVC: http://afana.me/post/aspnet-mvc-internationalization.aspx
Upvotes: 1
Reputation: 553
You could use Reflection
. If HomeStrings
is a property of Resources
for instance, you can try the following :
PropertyInfo property = Resources.GetType().GetProperty("HomeStrings" + locale);
Page.Title = (property.GetValue(Resources) as the type of HomeStrings).Title;
Of course it works with attributes, methods, etc. Just make sure to do all the necessary tests: property != null
, (... as the type of HomeStrings) != null
,...
Upvotes: 1