Programmatically Define a Custom Control Culture in ASP.NET

I would like to programmatically set up a culture of my User Control that defines several shortcuts, buttons, and text fields ...

Typically for aspx pages, you override the InitializeCulture-Method and set the Culture and UICulture to achieve this, alas, ASCX-Controls does not have this method, so how exactly did I do this?

I set local resources mycontrol.ascx.de-DE.resx, mycontrol.ascx.resx and mycontrol.ascx.en-GB.resx, but only the default values ​​of the file (mycontrol.ascx.resx).

Thanks in advance.

Dennis

+4
source share
3 answers

The current culture has a thread width: Page.Culture and Page.UICulture actually set Thread.CurrentThread.CurrentCulture and Thread.CurrentThread.CurrentUICulture under the hood.

If a user control is defined in the page layout, I don’t think you can do anything. If you load it using LoadControl() , you can temporarily override the current thread culture before calling and restore it later, but that would be rather inconvenient:

 protected void Page_Load(object sender, EventArgs e) { // Some code... CultureInfo oldCulture = Thread.CurrentThread.CurrentCulture; CultureInfo oldUICulture = Thread.CurrentThread.CurrentUICulture; Thread.CurrentThread.CurrentCulture = yourNewCulture; Thread.CurrentThread.CurrentUICulture = yourNewUICulture; try { Controls.Add(LoadControl("yourUserControl.ascx")); } finally { Thread.CurrentThread.CurrentCulture = oldCulture; Thread.CurrentThread.CurrentUICulture = oldUICulture; } // Some other code... } 
+5
source

I also spent hours on this problem and finally got this solution. You only need to override FrameworkInitialize() instead of initilizeculture() , for example:

 protected override void FrameworkInitialize() { String selectedLanguage = LanguageID; Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage); Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage); base.FrameworkInitialize(); } 

Jus Put this code inside your ascx.cs file, this override function need not be called.

+7
source

I used the following code to change the language for asp.net mvc on my project page

 public ActionResult ChangeCulture (Culture lang, string returnUrl) { if (returnUrl.Length> = 3) { returnUrl = returnUrl.Substring (3); } return redirect ("/" + lang.ToString () + returnUrl); } 

also i used usercontrol (ascx) on the same page. When I changed the language with actionlink, the presentation language changed, but the user event pageload did not change, so the version language changes, but the user control language does not change

+2
source

Source: https://habr.com/ru/post/1338117/


All Articles