Sergio A.
Sergio A.

Reputation: 401

Loading dynamically RESX file not working

I have a webpage that has to be displayed in several different languages based on user selection. For that, I'm using RESX files for each of the asp.net webpages. I don't want to used the automatic detection of the language in the browser, but I want to set the language, again, based in the user selection. In order to accomplish this I'm doing the following:

Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("es-MX", false);
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("es-MX", false);

OR

Page.Culture = "es-MX";
Page.UICulture = "es-MX";

But neither of those are working as expected! I'm initializing the Culture in the Init method of the page but it will always display the default language. I'm inspecting the values of those properties and those have the culture correctly, but still is not being rendered using the RESX file. Any ideas? Suggestions?

Thanks

Upvotes: 1

Views: 1720

Answers (2)

Sergio A.
Sergio A.

Reputation: 401

In case someone runs into this issue when working with Explicit localization, here is what has to be done:

protected override void InitializeCulture()
{
    Page.Culture = "en-US";
    Page.UICulture = "en-US";
}

From the net-tutorials.com website:

Since the Page directive is just a shortcut to the Page class, this can be done from CodeBehind as well. However, we have to do it at a certain point, before the page is being rendered, to make sure that it has the desired effect. This is where the InitializeCulture() method comes into play, a method that is called by ASP.NET pretty early in the Page life cycle, which you can override.

Upvotes: 2

Dmitry Savy
Dmitry Savy

Reputation: 1067

Try this System.Resources.ResourceReader resourceReader = new System.Resources.ResourceReader("RES_PATH");

Now you can use this to load users language like es.resx

System.Resources.ResourceReader resourceReader 
    = new System.Resources.ResourceReader(HttpContext.Current.Request.UserLanguages[0] 
    + ".resource");

Upvotes: 0

Related Questions