-1

I want function that acts on language of system change. For example,when the language changed from English to Spanish,etc.

Here is my code, I have used this code in loop to check again and again either language changed or not but it not working. When I change my system language sometime it hault and sometime it give same name again and again.

string current_language=InputLanguage.CurrentInputLanguage.Culture.Name;
string current_layout = InputLanguage.CurrentInputLanguage.LayoutName;

Is there any solution?

Here I have add image of language bar where I change my language

amitklein
  • 1,302
  • 6
  • 23
  • 1
    https://stackoverflow.com/a/4711600/2716623 – vasily.sib May 22 '20 at 09:51
  • What you are referring to is the input language, not the system language. It's per-thread, not per-system. You also need to tell what you are using for your application (winforms, wpf, uwp). – GSerg May 22 '20 at 09:52
  • I am asking about current language of system. I am using window form –  May 22 '20 at 10:07
  • I have tried to receive event when user changes system's culture but its not working..@vasily.sib –  May 22 '20 at 10:08
  • 1
    Because it's not the system culture, it's the current input language. https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.form.inputlanguagechanged?view=netcore-3.1 – GSerg May 22 '20 at 10:26
  • answer updated below – AlanK May 25 '20 at 13:19

1 Answers1

0

CultureInfo.InstalledUICulture for system install language CultureInfo.CurrentCulture for user regional settings (numeric formatting preference) CultureInfo.CurrentUICulture for current user language

The current thread gets its culture info on startup from its launching thread and it can be "changed from within" like this CultureInfo.CurrentUICulture = new CultureInfo("en-GB"); but to detect "change from without" you need to ask Windows. Here is a simple demo calling GetUserDefaultLCID() using P/Invoke.

    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Reflection;
    using System.Runtime.InteropServices;
    using System.Threading;

    namespace ConsoleApp2
    {
      class Program
      {
        private static readonly Dictionary<uint, CultureInfo> Cultures;

        static Program()
        {
          Cultures = new Dictionary<uint, CultureInfo>
                     { // These just happen to be what I have installed
                       { 0x0809, new CultureInfo("en-GB") },
                       { 0x0409, new CultureInfo("en-US") }, 
                       { 0x1C09, new CultureInfo("en-ZA") }
                     };
        }

        [DllImport("kernel32.dll")] static extern uint GetUserDefaultLCID();

        static void MonitorCulture()
        {
          while (true)
          {
            Console.WriteLine($"CurrentUICulture    : {Cultures[GetUserDefaultLCID()].EnglishName}");
            Thread.Sleep(2000);
          }
        }

        static void Main(string[] args)
        {
          var monitor = new Thread(MonitorCulture);
          monitor.Start();
          Console.WriteLine("Press a key to exit...");
          Console.ReadKey();
        }
      }
    }

Which produced this: [1]: https://i.stack.imgur.com/Sou6y.png as I changed Language while the program was running.

AlanK
  • 1,827
  • 13
  • 16
  • I have used this but its not working...I have add two language but when I switch language it doesn't show different name. –  May 22 '20 at 15:09
  • @MobeenGhaffar Answer updated with further explanation and example – AlanK May 23 '20 at 20:57