Load Resources for other Language

2019-06-28 07:34发布

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?

3条回答
来,给爷笑一个
2楼-- · 2019-06-28 08:19
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楼-- · 2019-06-28 08:28

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

查看更多
啃猪蹄的小仙女
4楼-- · 2019-06-28 08:30

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

`

查看更多
登录 后发表回答