简体   繁体   中英

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.

How about:

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

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");

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM