frenchie
frenchie

Reputation: 51937

Selecting resource file in code behind at runtime

Suppose I have 2 resources files; one's called HomeEN.resx and the other one is called HomeFR.resx. I've looked at online tutorials that show how the selection of the resource file is done automatically, based on the user's setting in his browser.

I want to select the resource file at runtime, something like this:

protected void Page_Load(object sender, EventArgs e)
{      
    switch (TheLanguage) {

       case 1:
            // select the English file HomeEN.resx;
            break;

       case 2:
            // select the French file HomeFR.resx;
            break;
    }
}

How do I write these statements?

Upvotes: 1

Views: 3362

Answers (3)

Naresh
Naresh

Reputation: 374

If you want to access both resources, you can use ResourceManager class

 ResourceManager rm = new ResourceManager("Strings", typeof(Example).Assembly);
string strDE = rm.GetString("TheNameOfTheResource",  new CultureInfo("de"));
string strES = rm.GetString("TheNameOfTheResource",  new CultureInfo("es"));

Upvotes: 1

jle
jle

Reputation: 9489

protected void Page_Load(object sender, EventArgs e)
{      
System.Threading.Thread.CurrentThread.CurrentCulture = someCulture;
    System.Threading.Thread.CurrentThread.CurrentUICulture = someCulture;
}

After that, if you follow @devio's solution above as well, the resource files will be selected automatically.

Upvotes: 1

devio
devio

Reputation: 37215

The correct resource files are automatically read by setting the Page's Culture and UICulture properties. See the MSDN samples

You just need to rename your files to match the expected pattern, Home.en.resx and Home.fr.resx respectively.

Upvotes: 3

Related Questions