Answered by:
How to change current UI culture at runtime?
Question
Answers
-
That is correct. Changing the UI culture at runtime is not supported (in v1 or v2):
http://www.danielmoth.com/Blog/2004/11/cultureinfo.html
Cheers
Daniel
All replies
-
That is correct. Changing the UI culture at runtime is not supported (in v1 or v2):
http://www.danielmoth.com/Blog/2004/11/cultureinfo.html
Cheers
Daniel -
-
-
Well, no, that wasn't what I was asking.
I read and accepted the fact that I cannot change the culture at run time. I was hoping that something in the development environment would allow me to test other cultures during development, which is a separate issue.
---Judy
-
Judy, your question is still not clear. During development, the obvious way to test things is
1. Change the language on the device
2. Run your app & test
3. Go to step 1 and repeat
You accept that you cannot change it at runtime. If you read the link, you know can change it at runtime *but* it would not take automatic effect on your forms without restarting - let's be specific here.
So if the runtime route is out, the only thing left is the design time. At design time, you can change your form's Localizable property and assign new strings (again, following the links from my article covers that).
Other than that, there is not anything else to say on the topic.
If I am still not getting your question, try rephrasing it please.
Cheers
Daniel -
Daniel:
Maybe there is a really easy step that I'm not seeing. I want to make a test run of a localized app in the MS emulator. I have made the form localizable and set the language strings, but cannot find a way to test it. I'd like to run it once in, say, French. Then stop execution and run it in Spanish. Then a different language. I don't expect to change it during the run, but would just like to be able to see the output.
Is there a way?
---Judy
-
-
why don't you try this code:Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("fr-FR");Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-FR");The first line is used for changing the culture on the server and the 2nd line is used for changing the user interface.Have fun,Tidus
-
-
It works!!! But!!! You must modify this property before the form has loaded.
For example:
[STAThread]
public static void Main(string[] args)
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo("it");
Application.Run(new MyMainForm);
}
That's why you can use it only for testing. But if I want to change it during run-time. Like selecting menu item "Language->Italian", how I can do it (the simplest way please
) ? -
Of course it does not work, there's no such property on NETCF. It won’t even compile for that matter. Just change your device locale to whatever it should be.
Alternatively you could use reflections to change locale for entire application. This is hack which can stop working at any moment so use it only if you absolutely have to. If you're VB user translate it with one of these online translators.
public static void SetDefaultLocale( System.Globalization.CultureInfo locale){
if (null == locale)
{
throw new ArgumentNullException("locale");
}
System.Reflection.FieldInfo fi = typeof(System.Globalization.CultureInfo).GetField("m_userDefaultCulture", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
if (null == fi)
{
throw new NotSupportedException("Setting locale is not supported in this version of the framework.");
}
fi.SetValue(null, locale);
}
- Proposed as answer by Shammi Theodore Thursday, December 25, 2008 3:38 PM
-
-
-
AMD wrote: HI.
I have a form and I want to change its UI culture at runtime.
The method System.Threading.Thread.CurrentThread.CurrentUICulture = ... does not working in .Net CF.
I am using VS .Net 2005Beta2, C#.
Thanks.
If your culture is not supported in PocketPC, just use another Culture string name which is supported in your device, like the following code in line 6.
Code Snippet
public static void LoadResource(Form form, string lan)
{
ResourceManager rm = new ResourceManager(form.GetType().FullName, Assembly.GetExecutingAssembly());
CultureInfo ci;
if (lan == "fa")
{
ci = new CultureInfo("en-GB");
}
else if (lan == "en")
{
ci = new CultureInfo("en-US");
}
else if (lan == "de")
{
ci = new CultureInfo("de-DE");
}
else
{
ci = new CultureInfo("en-US");
}
IDictionaryEnumerator id = rm.GetResourceSet(ci, true, true).GetEnumerator();
while (id.MoveNext())
{
string key = (string)id.Key;
if (key.StartsWith("$"))
{
key = key.Substring(1);
}
int index = key.IndexOf('.');
string cName = key.Substring(0, index);
string cProperty = key.Substring(index + 1);
if (cName == "this")
{
form.GetType().GetProperty(cProperty).SetValue(form, id.Value, null);
}
else
{
//= new Control();// = form.Controls[cName];
IEnumerator ie = form.Controls.GetEnumerator();
while (ie.MoveNext())
{
if (((Control)ie.Current).Name == cName)
{
Control control = (Control)ie.Current;
control.GetType().GetProperty(cProperty).SetValue(control, id.Value, null);
}
}
}
}
}
-
Thanks to the reflections solution, I am able to overcome a big headache associated with registration for my software. I can now change the CultureInfo during runtime, just when its required and revert it back immediately.Ilya Tumanov said:Of course it does not work, there's no such property on NETCF. It won’t even compile for that matter. Just change your device locale to whatever it should be.
Alternatively you could use reflections to change locale for entire application. This is hack which can stop working at any moment so use it only if you absolutely have to. If you're VB user translate it with one of these online translators.
public static void SetDefaultLocale( System.Globalization.CultureInfo locale){
if (null == locale)
{
throw new ArgumentNullException("locale");
}
System.Reflection.FieldInfo fi = typeof(System.Globalization.CultureInfo).GetField("m_userDefaultCulture", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
if (null == fi)
{
throw new NotSupportedException("Setting locale is not supported in this version of the framework.");
}
fi.SetValue(null, locale);
}

