【问题标题】:Programmatically listing all supported locales in Windows 10以编程方式列出 Windows 10 中所有支持的语言环境
【发布时间】:2017-08-10 09:40:32
【问题描述】:

Windows 10 支持自定义区域设置,即没有 LCID 的区域设置,例如旧版本 Windows 中的旧区域设置。在 Windows 10 的每次更新中,Microsoft 一直在为少数民族和土著语言添加较少使用的语言环境。

最近对 Windows 10 的更新添加了 Võro kiil (vro) 区域设置。当使用EnumSystemLocalesEx 枚举所有支持的语言环境时,vro 不会以任何形式出现。但是,在用于添加新语言或键盘的系统设置 UI 中,确实会出现 Võro kiil。

但是,如果用户随后启用此语言,则当您调用 EnumSystemLocalesExvrovro-Latnvro-Latn-001 时,现在会列出。如果用户随后从 UI 中删除此区域设置,则它不再出现在此函数调用的结果中。

问题:有没有办法(是否支持)获取操作系统所有已知语言环境的列表,无论用户是否启用它们?

我觉得非常奇怪的是,此输出包含其他少数民族语言,如 Skolt Sami,而无需用户提前启用。

如果 C/C++ API 中不存在该 API,我将非常乐意接受使用 .NET 框架的答案,只要我能真正获得这些数据。


生成语言环境输出的示例代码:

#include <cstdio>
#include "stdafx.h"
#include "windows.h"

BOOL CALLBACK Callback(LPWSTR pStr, DWORD dwFlags, LPARAM lparam)
{
    wprintf(L"%ls\n", pStr);
    return TRUE;
}

int main()
{
    EnumSystemLocalesEx(Callback, 0, 0, 0);
    return 0;
}

从系统设置中的“区域和语言”屏幕启用 Võro kiil 后,最后三个结果是 vro-Latnvro-Latn-001vro。未启用时,它们根本不会出现在输出中。


使用 .NET API 似乎具有相同的行为。

#include "stdafx.h"

using namespace System;
using namespace System::Globalization;
int main()
{
    System::Collections::IEnumerator^ enum0 = CultureInfo::GetCultures(CultureTypes::AllCultures)->GetEnumerator();
    while (enum0->MoveNext())
    {
        CultureInfo^ ci = safe_cast<CultureInfo^>(enum0->Current);
        Console::WriteLine("{0}", ci->Name);
    }
}

【问题讨论】:

  • 请为EnumSystemLocalesEx 提供minimal reproducible example,以便我们尝试重现并确定这是否真的是API 问题或您使用它的方式问题。
  • 已添加示例。
  • 我可以重现,但我不知道为什么会这样。如果找不到其他 API,您可以使用进程监视器来确定设置应用程序使用的注册表项,以获取所有可安装语言环境的列表。
  • 我检查了活动监视器,它似乎没有从注册表中提取数据。根据一些注册表调用的堆栈,我最好的猜测是它存储在系统设置应用程序本身使用的 Winlangdb.dll 中……它没有记录的 API 或可用的调试符号。
  • 你到底需要这个做什么?也许有更好/其他的方法。

标签: c# c++ windows winapi localization


【解决方案1】:

实际上,Windows 的新语言模型意味着除了具有历史 LCID 的那些之外没有“列表”。

Windows 8.1 和 10 设置工具链接到 bcp47langs.dllwinlangdb.dll,只要提供的输入是有效的 ISO 639-3 语言代码,它们就提供启用语言和输入法的功能。

在某些情况下,如果您希望您的语言出现在 UI 中或通过这些 API 显示,您必须至少提供脚本,有时还必须提供地区。 Erzya 语言的 myv-Cyrl 就是一个例子。

使用这些 API

在与 PowerShell 捆绑的 cmdlet 上使用 MSIL 反汇编程序,我发现了一个 p/invoke 定义,它使我能够成功地从 C# 和 Rust 代码中使用这些 API。

在这里,为了后代:

// Decompiled with JetBrains decompiler
// Type: Microsoft.InternationalSettings.Commands.LPAPIWrapper
// Assembly: Microsoft.InternationalSettings.Commands, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// MVID: E0B49792-544F-4FBD-8C35-D4DA177385AF
// Assembly location: C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.InternationalSettings.Commands\v4.0_3.0.0.0__31bf3856ad364e35\Microsoft.InternationalSettings.Commands.dll

using System;
using System.Runtime.InteropServices;
using System.Text;

namespace Microsoft.InternationalSettings.Commands
{
  internal class LPAPIWrapper
  {
    public static uint GEO_NATION = 1;
    public static uint GEO_FRIENDLYNAME = 8;
    public static uint GEOCLASS_NATION = 16;
    public static uint GEOCLASS_REGION = 14;

    [DllImport("kernelbase.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern int NlsUpdateLocale(string LocaleName, int Flags);

    [DllImport("intl.cpl", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern int IntlUpdateSystemLocale(string LocaleName, int dwFlags);

    [DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern int SystemParametersInfo(
      uint Action,
      uint UnsignedParam,
      IntPtr Param,
      uint WinIni);

    [DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern int SendNotifyMessage(
      IntPtr wWwnd,
      uint Msg,
      IntPtr wParam,
      string lParam);

    [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern int GetSystemDefaultLocaleName(
      StringBuilder LocaleName,
      int LocaleNameSize);

    [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern int GetUserGeoID(uint GeoClass);

    [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern int SetUserGeoID(int GeoId);

    [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern int GetGeoInfo(
      int Location,
      uint GeoType,
      StringBuilder GeoData,
      int Length,
      ushort LangID);

    [DllImport("bcp47langs.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern int GetUserLanguages(char Delimiter, [MarshalAs(UnmanagedType.HString)] ref string UserLanguages);

    [DllImport("bcp47langs.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern int GetUserLanguageInputMethods(
      string Language,
      char Delimiter,
      [MarshalAs(UnmanagedType.HString)] ref string InputMethods);

    [DllImport("bcp47langs.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern int LcidFromBcp47([MarshalAs(UnmanagedType.HString)] string LanguageTag, ref int Lcid);

    [DllImport("bcp47langs.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern int GetPendingUserDisplayLanguage([MarshalAs(UnmanagedType.HString)] ref string language);

    [DllImport("bcp47langs.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern int GetUserDisplayLanguageOverride([MarshalAs(UnmanagedType.HString)] ref string language);

    [DllImport("bcp47langs.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern int SetUserDisplayLanguageOverride(string LanguageTag);

    [DllImport("bcp47langs.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern int ClearUserDisplayLanguageOverride();

    [DllImport("bcp47langs.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern int GetHttpAcceptLanguageOptOut(ref bool IsOptOut);

    [DllImport("bcp47langs.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern int SetHttpAcceptLanguageOptOut();

    [DllImport("bcp47langs.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern int ClearHttpAcceptLanguageOptOut();

    [DllImport("bcp47langs.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern int GetUserLocaleFromLanguageProfileOptOut(ref bool IsOptOut);

    [DllImport("bcp47langs.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern int SetUserLocaleFromLanguageProfileOptOut();

    [DllImport("bcp47langs.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern int ClearUserLocaleFromLanguageProfileOptOut();

    [DllImport("bcp47langs.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern int RemoveInputsForAllLanguagesInternal();

    [DllImport("bcp47langs.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern int SetInputMethodOverride(string TipString);

    [DllImport("bcp47langs.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern int Bcp47GetIsoLanguageCode([MarshalAs(UnmanagedType.HString)] string languageTag, [MarshalAs(UnmanagedType.HString)] ref string isoLanguageCode);

    [DllImport("ext-ms-win-globalization-input-l1-1-2.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern int WGIGetDefaultInputMethodForLanguage(
      [MarshalAs(UnmanagedType.HString)] string Language,
      [MarshalAs(UnmanagedType.HString)] ref string DefaultTipString);

    [DllImport("ext-ms-win-globalization-input-l1-1-2.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern int WGITransformInputMethodsForLanguage(
      [MarshalAs(UnmanagedType.HString)] string TipString,
      [MarshalAs(UnmanagedType.HString)] string Language,
      [MarshalAs(UnmanagedType.HString)] ref string TransformedTipString);

    [DllImport("winlangdb.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern int SetUserLanguages(char Delimiter, [MarshalAs(UnmanagedType.HString)] string UserLanguages);

    [DllImport("winlangdb.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern int GetLanguageNames(
      string Language,
      StringBuilder Autonym,
      StringBuilder EnglishName,
      StringBuilder LocalName,
      StringBuilder ScriptName);

    [DllImport("ext-ms-win-globalization-input-l1-1-2.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern int WGIIsImeInputMethod([MarshalAs(UnmanagedType.HString)] string TipString, ref int result);

    [DllImport("winlangdb.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern int EnsureLanguageProfileExists();

    [DllImport("input.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern int InstallLayoutOrTip(string TipString, int Flags);

    [DllImport("input.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern int SetDefaultLayoutOrTip(string TipString, int Flags);

    [DllImport("input.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern int GetLayoutDescription(
      string LayoutId,
      StringBuilder LayoutDescription,
      ref int DescriptionLength);

    private LPAPIWrapper()
    {
    }
  }
}
 

【讨论】:

    猜你喜欢
    • 2011-06-26
    • 1970-01-01
    • 2012-08-30
    • 1970-01-01
    • 2019-03-10
    • 1970-01-01
    • 2012-09-04
    • 2011-06-29
    • 1970-01-01
    相关资源
    最近更新 更多