how to convert SAPI's MS LANG ID to BCP 47 language tag?

被刻印的时光 ゝ 提交于 2019-12-22 01:32:54

问题


The call to SAPI's get language method returns an MS LangID, but for my purpose, it needs to be converted to a BCP 47 language tag (eg. en-GB) . how do we do it?

I am not able to do it using LCIDToLocalName, as to use this function, I need to convert the returned value into the LCID format first.

For eg, it returns "809" for english, now how do I convert it into LCID first, as LCIDHex for English is "0809", and LCIDec is "2057".

Any help would be appreciated.

Edit: Following is the code

if (S_OK != SpEnumTokens(SPCAT_VOICES, NULL, NULL, &voice_tokens))
    return FALSE;

unsigned long voice_count, i = 0;
hr = voice_tokens->GetCount(&voice_count);
cout << " count " << voice_count << endl;
for (unsigned int i = 0; i < voice_count; i++){
    //cout << i << endl;
    CComPtr<ISpObjectToken> voice_token;
    if (S_OK != voice_tokens->Next(1, &voice_token, NULL))
        return FALSE;

    WCHAR *description;
    if (S_OK != SpGetDescription(voice_token, &description))
        return FALSE;


    CComPtr<ISpDataKey> attributes;
    if (S_OK != voice_token->OpenKey(kAttributesKey, &attributes))
        return FALSE;

    WCHAR *gender_s;
    TtsGenderType gender;
    if (S_OK == attributes->GetStringValue(kGenderValue, &gender_s)){
        if (0 == _wcsicmp(gender_s, L"male"))
            gender = TTS_GENDER_MALE;
        else if (0 == _wcsicmp(gender_s, L"female"))
            gender = TTS_GENDER_FEMALE;
    }


    WCHAR *language;
    if (S_OK != attributes->GetStringValue(kLanguageValue, &language))
        return FALSE;

    wprintf(L"%s\n", language);

The last line prints hex values such as 409 and 809, but I want it in format like En-US .


回答1:


@HansPassant is correct. You've got a string; parse it as hex. Use _wtoi (or your favorite parser), then pass to LCIDToLocaleName.

For XP, you can use DownlevelLCIDToLocaleName; the required DLL is downloadable from the Microsoft Download Center.



来源:https://stackoverflow.com/questions/25337602/how-to-convert-sapis-ms-lang-id-to-bcp-47-language-tag

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!