0

I want to have a label in my form and change its text when my system language changes

Something like this:

else if (Thread.CurrentThread.CurrentCulture.Name == "en-US")
        {
            label1.Text = "En";
        }
        else
            label1.Text = "Not En";

1: this code always shows "En", what should I do ?

2: where should I put my code if I want it always Check ?

Sam Leach
  • 12,746
  • 9
  • 45
  • 73
user3105142
  • 13
  • 2
  • 6
  • For windows forms, see:http://stackoverflow.com/questions/5710127/get-operating-system-language-in-c-sharp and http://stackoverflow.com/questions/11711426/proper-way-to-change-language-at-runtime – NoChance Dec 20 '13 at 22:58
  • 1
    You can't change the system default language without logging out and logging back in. I'll take a wild guess that you really only changed the keyboard layout. Ask at superuser.com how to do it properly. – Hans Passant Dec 20 '13 at 23:01
  • +1 to Hans Passant's comment - my suggested duplicate talks about input language (i.e. keyboard input), not system current UI language. – Alexei Levenkov Dec 20 '13 at 23:10

2 Answers2

0

You need to read up on globalization in .Net. What you're going to want to do is create resource files, one per supported language and let the infrastructure figure out the appropriate resource to use for the given resource ID.

http://msdn.microsoft.com/en-us/library/vstudio/aa992030(v=vs.100).aspx

I'd recommend getting a tools like ResourceBlender or Zeta Resource Editor to help with the translation. The default Visual Studio support is...not very good.

Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
0

Since this is a user interface, you have a message loop and you can detect region/language settings changes using the SystemEvents.UserPreferenceChanged event. However even after using that event, you will need to refresh the CultureInfo using ClearCachedData. Below is a really basic implementation that prints to the Output window. Note that SystemEvents.UserPreferenceChanged is a static event so you will want to detach from the event when the form is closed or it will keep the form instance alive.

public AppForm() // constructor
{
    SystemEvents.UserPreferenceChanged += SystemEvents_UserPreferenceChanged;
}

private void SystemEvents_UserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e)
{
    Debug.Print("Settings changed category: {0}", e.Category);
    CultureInfo.CurrentCulture.ClearCachedData();
    CultureInfo.CurrentUICulture.ClearCachedData();
    Debug.Print("Current Culture: {0}", CultureInfo.CurrentCulture);
    Debug.Print("Current UI Culture: {0}", CultureInfo.CurrentUICulture);
}
Mike Zboray
  • 39,828
  • 3
  • 90
  • 122