Using Regional and Language Options to Change the Culture


A variation on the previous theme is to do away with the menu in the application altogether. Assume that the user controls the application's current culture using the Regional and Language Options dialog. Thus, whenever the user changes the setting in this dialog, the application should adapt to the changed setting. Unfortunately, the CultureInfo class does not surface a .NET event for this purpose, but the form can still trap this event by overriding the WndProc method and looking for a WM_SETTINGSCHANGE event, where the message's LParam is intl:

 [DllImport("kernel32.dll")] protected static extern int GetUserDefaultLCID(); private const int WM_SETTINGSCHANGE = 0x001A; protected override void WndProc(ref Message message) {     switch (message.Msg)     {         case WM_SETTINGSCHANGE:         {             if ((int) message.WParam == 0 &&                 message.LParam != IntPtr.Zero &&                 Marshal.PtrToStringAuto(message.LParam) == "intl")             {                 if (Thread.CurrentThread.CurrentCulture.LCID !=                     GetUserDefaultLCID())                 {                     Thread.CurrentThread.CurrentCulture =                         new CultureInfo(GetUserDefaultLCID());                     Thread.CurrentThread.CurrentUICulture =                         Thread.CurrentThread.CurrentCulture;                     ChangeFormCulture.ChangeAllForms(Thread.                         CurrentThread.CurrentUICulture.Name);                 }                 else                 {                     Thread.CurrentThread.                         CurrentCulture.ClearCachedData();                     Thread.CurrentThread.                         CurrentUICulture.ClearCachedData();                 }             }             break;         }     }     base.WndProc(ref message); } 


With the "culture change" event trapped, we check to see whether the new culture is the same as our current culture. This implementation assumes that the application's CurrentCulture and CurrentUICulture are the same, and that CurrentUICulture has been initialized to the CurrentCulture when the application started. If the culture has changed, we change the CurrentCulture and CurrentUICulture and call ChangeFormCulture.ChangeAllForms to change all the forms. If the new culture is the same culture as our current culture, the user has simply modified the settings for the existing culture (e.g., they have changed the date/time formats). In this case, we need to only clear our cached version of the culture so that the updated information is reread.

Although this solves the immediate problem, I must advise caution with this approach. For an application simply to rebuild all its forms, no matter what state it is currently in or what it is currently doing, is disturbing and might lead to unpredictable results.




.NET Internationalization(c) The Developer's Guide to Building Global Windows and Web Applications
.NET Internationalization: The Developers Guide to Building Global Windows and Web Applications
ISBN: 0321341384
EAN: 2147483647
Year: 2006
Pages: 213

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net