1

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 .

Yash
  • 11
  • 2
  • LCID is just a number. If you get a string like "809" then you have to convert it from its hex representation so you get 2057. Lots of ways to do that, depends on your favorite programming language of course. – Hans Passant Aug 16 '14 at 09:05
  • Can you add the code that you currently have? – Eric Brown Aug 17 '14 at 14:39

1 Answers1

0

@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.

Eric Brown
  • 13,774
  • 7
  • 30
  • 71
  • That indeed worked. Thanks. But there is one problem. LCIDToLocaleName is not available on windows xp. But, I need to do this in win xp also. I searched for alternatives but couldn't find one. So, any idea how we can do the same thing on win xp? – Yash Aug 22 '14 at 19:01