Reputation: 2945
I need to display a list of installed languages in a ComboBox
in a WPF
application. For example, I have English (United States) and English (India) installed. I want to show these both in my ComboBox
I am using the CultureInfo
class for this. Below is a snippet of what I am trying. I am able to get all the cultures. But I need only those cultures which are installed through system settings.
var cultureInfos = CultureInfo.GetCultures(CultureTypes.NeutralCultures);
foreach (var culture in cultureInfos)
{
Console.WriteLine(culture.Name);
}
Upvotes: 2
Views: 1325
Reputation: 15217
You can use the native function GetKeyboardLayoutList
to get a list of installed input languages.
Here is a sample:
IEnumerable<CultureInfo> GetInstalledInputLanguages()
{
// first determine the number of installed languages
uint size = GetKeyboardLayoutList(0, null);
IntPtr[] ids = new IntPtr[size];
// then get the handles list of those languages
GetKeyboardLayoutList(ids.Length, ids);
foreach (int id in ids) // note the explicit cast IntPtr -> int
{
yield return new CultureInfo(id & 0xFFFF);
}
}
[DllImport("user32.dll")]
static extern uint GetKeyboardLayoutList(int nBuff, [Out] IntPtr [] lpList);
Upvotes: 5