Home > Windows API > How to convert language id’s like 1033 to “English – United States” form?

How to convert language id’s like 1033 to “English – United States” form?

Use GetLocaleInfo function to get language name and country name from a language id number. First we have to prepare a LCID using MAKELCID, then call GetLocaleInfo twice, first to get language name and then 2nd to get country name.

// Get language as string
bool GetLangStringFromLangId( DWORD dwLangID_i, CString& csLang_o )
{
   const int MAX_LANG_LEN = 50;

   // Prepare LCID
   const LCID lcidLang = MAKELCID( dwLangID_i, SORT_DEFAULT );

   // Will hold language
   TCHAR szLangBuffer[MAX_LANG_LEN] = { 0 };

   // Get language
   DWORD dwCount = GetLocaleInfo( lcidLang, LOCALE_SENGLANGUAGE, szLangBuffer, MAX_LANG_LEN );
   if( !dwCount )
   {
      AfxTrace( _T( "Failed to get locale language information" ));
      return false;
   }// End if

   // Will hold country
   TCHAR szCountryBuffer[MAX_LANG_LEN] = { 0 };

   // Get country
   dwCount = GetLocaleInfo( lcidLang, LOCALE_SENGCOUNTRY, szCountryBuffer, MAX_LANG_LEN );

   if( !dwCount )
   {
      AfxTrace( _T( "Failed to get locale country information" ));
      return false;
   }// End if

   // Prepare language string
   csLang_o.Format( _T( "%s, %s" ), szLangBuffer, szCountryBuffer );

   // Return execution status
   return true;
}// End GetLangStringFromLangId

Usage:

CString csLanguage;
GetLangStringFromLangId( 1033, csLanguage );
MessageBox( csLanguage ); // Will show English, United States.
Advertisements
  1. chuchu
    June 13, 2008 at 4:46 am

    how can i make the opposite.
    “English” -> 1033 or something like 0x## format?

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: