Erik
Erik

Reputation: 1

How to use dynamic localization in an asp .net web application

I have added a resourcefile named language.resx to my App_GlobalResources folder, which enables me to access strings like App_GlobalResoureces.Language.MyString. How do I make this dynamic? So if I have a string called ResourceID, how do I pull the correct resource from the resource file?

so far I tried this:

string s = "MyString";

System.Resources.ResourceManager = new System.Resources.ResourceManager("App_GlobalResources.Language", System.Reflection.Assembly.GetExecutingAssembly());
string lang = mngr.GetString(s);

but this is not working (it says it cannot find the namespace or something).

It has been suggested to use a database for this, but I dont want this since its just a couple of enums I want to translate.

Upvotes: 0

Views: 2710

Answers (3)

Erik
Erik

Reputation: 1

It's embarassing easy. Why is this extremely simple stuff always so hard to find

The correct code is:

App_GlobalResources.Language.ResourceManager.GetString("MyString");

Upvotes: 0

Meligy
Meligy

Reputation: 36594

This is proxy code generated by Visual Studio. If you want to locate the file yourself (what I thought you meant by dynamic) you'd do it as in the code sinnpet aobe. Actually, I got this code by copying the contents of the "get" accessor of the properties geenrated for resource files :)

Upvotes: 0

Meligy
Meligy

Reputation: 36594

How about:

string s = "MyString";
var mngr = new System.Resources.ResourceManager("Resources.Language", 
    System.Reflection.Assembly.Load("App_GlobalResources"));
string lang = mngr.GetString(s);

Upvotes: 1

Related Questions