Load Resources for other Language

2019-06-28 08:32发布

问题:

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

`