简体   繁体   English

如何在ASP .NET Web应用程序中使用动态本地化

[英]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. 我已将名为language.resx的资源文件添加到我的App_GlobalResources文件夹中,这使我能够访问诸如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? 因此,如果我有一个名为ResourceID的字符串,如何从资源文件中提取正确的资源?

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. 这是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. 如果您想自己定位文件(我认为动态的含义),可以按照代码sinnpet aobe进行。 Actually, I got this code by copying the contents of the "get" accessor of the properties geenrated for resource files :) 实际上,我是通过复制资源文件所具有的属性的“获取”访问器的内容来获得此代码的:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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