getit
getit

Reputation: 621

How to get specific resource string regardless of current culture in c# .net web app

I need to send a bulk email to the users, but they have different language preferences. The email messages are in resource files: Email.resx, Email.fr.resx

How do I forcefully get string from one or the other? For ex, say I am logged in with en-CA culture, but I want to send a french email?

I tried:

System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("fr-CA");

System.Resources.ResourceSet rSet = Resources.Emails.ResourceManager.GetResourceSet(ci ,true, false);

string bodyMessageResource = rSet.GetString("EmailBody");

But rSet is null.

I just want to be able to select a string of the language of my choice.

Upvotes: 2

Views: 10059

Answers (4)

suizo
suizo

Reputation: 531

Instead of changing the Culture of the current tread you could pass the CultureInfo to the Ressource file. I wrote this extension method which returns the translated string from my core.{language-code}.resx file

public static string Translate(this string resourceId, CultureInfo cultureInfo)
{            
    return Resources.Text.Core.ResourceManager.GetString(resourceId, cultureInfo) ?? String.Format("Missing translation for Core resx ResourceId '{0}'", resourceId);
}

Upvotes: 1

Leonel Sanches da Silva
Leonel Sanches da Silva

Reputation: 7230

Try this:

ResourceManager resourceManager = FrenchLanguage.ResourceManager;
string msg = resourceManager.GetString("MyString");

Upvotes: 1

Ahmed HABBACHI
Ahmed HABBACHI

Reputation: 98

i insets to use the full name space of your resources i was stuck there for almost 3 hours to get it clear; use the threading to set the current culture info.

Application.CurrentCulture = new System.Globalization.CultureInfo("fr");
System.Threading.Thread.CurrentThread.CurrentUICulture = Application.CurrentCulture;
ResourceManager resourceManager = new ResourceManager("ProjectName.ResourcesFolder.MainApplicationResource", System.Reflection.Assembly.GetExecutingAssembly());
string msg = resourceManager.GetString("yourResourcedMsgName");

Upvotes: 1

Clafou
Clafou

Reputation: 15410

I think you were almost there. Try this:

System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("fr-CA");
ResourceManager resourceManager = new ResourceManager("MyResource", Assembly.GetExecutingAssembly());
string bodyMessageResource = resourceManager.GetString("EmailBody", ci);

Upvotes: 4

Related Questions