Load Resources for other Language

99封情书 提交于 2019-12-13 13:31:29

问题


I have an application, which works with Resources for translation. This is working great. Now, I have a special requirement. For this, I have to load the resource-dll for another language (for example, the application starts and works with English, then I have to also to load the German-translations) and look into it for a translation.

Is there an easy-way to do this?


回答1:


You need to load the resourcemanager and If you need the resources for an specific language you will need to ask for them using the specific culture, using:

GetObject(String, CultureInfo)

You can create the culture that you need using:

new CultureInfo(string name)

Or

CultureInfo.CreateSpecificCulture(string name)

Or

CultureInfo.GetCultureInfo(string name)

The name is the culture name: "en" English, "de" German... You can see a full list on the following link: cultures




回答2:


using System.Resources;
using System.Reflection;

Assembly gerResAssembly = Assembly.LoadFrom("YourGerResourceAssembly.dll");
var resMgr = new ResourceManager("StringResources.Strings", gerResAssembly);
string gerString = resMgr.GetString("TheNameOfTheString");



回答3:


You can make it, with the GetString calling the together with the specific CultureInfo you need. for example:

using System.Resources;
using System.Reflection;

Assembly gerResAssembly = Assembly.LoadFrom("YourGerResourceAssembly.dll");
var resMgr = new ResourceManager("StringResources.Strings", gerResAssembly);

// for example german:
string strDE = resMgr.GetString("TheNameOfTheString",  new CultureInfo("de"));
// for example spanish
string strES = resMgr.GetString("TheNameOfTheString",  new CultureInfo("es"));

`



来源:https://stackoverflow.com/questions/4774836/load-resources-for-other-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!