question about localization and multi threading

D

DAXU

Hello,

Suppose I have a windows form running in culture A, then the form
creates a new thread (call a method foo for example) and makes it
using culture B.
When method foo is running in the new thread, it may try to change a
label in the form. So code is like:
foo()
{
string message = resMan.GetString("strLabel");
if (label2.InvokeRequired)
label2.Invoke(new MethodInvoker(foo));
else
label2.Text = message;

}

The problem is that it will never show the label as culture B as foo
finally goes to the thread the windows form is running (culture A).
So what is the best way to handle it?

The real code was written without consideration of localizaton but now
requirement is added to support multi cultures.

Many Thanks
Jerry
 
D

DAXU

I am doing a risk reduction project to gather risks. It is not saying
that the real code would use different cultures in different threads.
That is just a idea in my mind and I wants to see the possible
solution if we do need it in future.

Also, I get this from another post:
"The CultureInfo.CurrentUICulture property defaults to the current
user interface language of Windows. Regardless of which system locale
setting
you choose, the application will load English resources on English
Windows. "

Supose I deploy the code to a UK site, by default the CurrentUICulture
will still be en-US (tested by create a virtual machine and set every
thing to uk related). However, I may have different resources for UK
and US, to let my code pick up UK resource, I am using this:
[DllImport("kernel32.dll", SetLastError = true)]
static extern short GetSystemDefaultLangID();
private int getSystemLcid()
{
short systemLcid = GetSystemDefaultLangID();
return systemLcid;
}

to get System Locale and use it to set current Cluture.

Is it the right way?

Cheers,
Jerry
 
P

Peter Duniho

[...]
The problem is that it will never show the label as culture B as foo
finally goes to the thread the windows form is running (culture A).
So what is the best way to handle it?

You're running into the problem because you use the same method to set the
text as to translate it. Don't do that.

Instead, make sure that the only thing you do in the invoked method is
that which is really required: to set the control's Text property. Get
the string to set it to in a different method that calls the
cross-thread-safe method, so that you are getting the string on the
expected thread.

Pete
 
P

Patrice

You could also avoid this by using a multinlingual version of Windows. AFAIK
this is basically how most applications are working i.e. the user interface
lanugage is a particular settings, the date/time formats and other behaviors
are another specific settings.

Else you could also invent your own criteria (or even do this based on an
application specific user preference if you wish).

Not tried but check both the effect of your settings with Culture and
UICulture...
---
Patrice


<[email protected]> a écrit dans le message de (e-mail address removed)...
I am doing a risk reduction project to gather risks. It is not saying
that the real code would use different cultures in different threads.
That is just a idea in my mind and I wants to see the possible
solution if we do need it in future.

Also, I get this from another post:
"The CultureInfo.CurrentUICulture property defaults to the current
user interface language of Windows. Regardless of which system locale
setting
you choose, the application will load English resources on English
Windows. "

Supose I deploy the code to a UK site, by default the CurrentUICulture
will still be en-US (tested by create a virtual machine and set every
thing to uk related). However, I may have different resources for UK
and US, to let my code pick up UK resource, I am using this:
[DllImport("kernel32.dll", SetLastError = true)]
static extern short GetSystemDefaultLangID();
private int getSystemLcid()
{
short systemLcid = GetSystemDefaultLangID();
return systemLcid;
}

to get System Locale and use it to set current Cluture.

Is it the right way?

Cheers,
Jerry
 
M

Mihai N.

"The CultureInfo.CurrentUICulture property defaults to the current
user interface language of Windows. Regardless of which system locale
setting
you choose, the application will load English resources on English
Windows. "

Supose I deploy the code to a UK site, by default the CurrentUICulture
will still be en-US (tested by create a virtual machine and set every
thing to uk related). However, I may have different resources for UK
and US, to let my code pick up UK resource, I am using this:

Just set your CurrentUICulture to what you want.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top