using System; using System.Collections.Generic; using System.Text; using System.Globalization; using System.Linq; using System.Threading; using System.Windows.Forms; /* This unit allows various forms to update themselves any time the current culture changes. * * The structure of this unit is based on LayoutManager.cs, although this is a little simpler. * If you want to be notified when the culture changes, you have two choices. Either implement * ICultureListener or listen to the CultureChanged event. ICultureListener is good for windows * that go away when you close them, and CultureChanged is good for windows that might be * resued after being closed. * * Note that a modal dialog box is a simpler case. You don't have to worry about changes * while the window is visible. So the best thing is to configure the culture specific * parts of the window each time the window is made visible. There is no reason for a * callback to that window, except the normal Form.VisibleChanged method. * * Typically there will be exactly one CultureManager per client. See * TradeIdeas.TIProGUI.GuiEnvironment.CultureManager if you're looking for the obvious place. * However, nothing about this is limited to one object. Perhaps one per GUI thread might * work. But typically we only have one GUI thread. */ namespace TradeIdeas.TIProGUI { public delegate void CultureChanged(); public interface ICultureListener { void CultureChanged(); } public class CultureManager { public event CultureChanged CultureChanged; public void SetCulture(CultureInfo newCulture) { if (newCulture == Thread.CurrentThread.CurrentUICulture) // Nothing to do! return; Thread.CurrentThread.CurrentUICulture = newCulture; List< ICultureListener> listeners = new List(); foreach (Form form in Application.OpenForms.Cast
().Where(x => !x.IsDisposed).ToList()) { ICultureListener possible = form as ICultureListener; if (null != possible) listeners.Add(possible); } foreach (ICultureListener listener in listeners) try { listener.CultureChanged(); } catch { } if (null != CultureChanged) try { CultureChanged(); } catch { } } public CultureInfo Current { get { // It seems like there are several veriations of this all avaliable. // This property is the preferred way to get the current culture. return Thread.CurrentThread.CurrentUICulture; } set { SetCulture(value); } } } }