Tip: Detect if a Language Font is Installed (Such as East Asian)

There are times when your application might need to display Japanese fonts or some other East Asian languages, like Chinese or Korean—which are collectively know as CJK in acronym. On a Japanese Windows XP, this is not a problem. On a non-Japanese Windows XP, there is a problem, because the Japanese characters will appear as squared boxes, so (non-Japanese) users may wonder what the actual language and its content are. A way is needed to detect when the application is running on non-Japanese Windows XP without the East Asian Language Pack installed, and it needs to be able to notify the users that the East Asian Language Pack needs to be installed in order to display Japanese text.

Upon seeing the prompt, the user may choose the install the East Asian Language Pack if they can understand Japanese or they can ignore it ( or not run the application), if they don’t understand Japanese anyway. Please note this is not a problem on Windows Vista and Windows 7 as these two operating systems have all of the common languages installed.

I have searched for the solution on how to detect if an East Asian Language is installed on Windows XP, however I can find none. Instead, I find many useless replies taht say this is a non-issue: because only a Japanese user will run a Japanese application and they will have all the necessary fonts installed. While this is true in most cases, it is not true in all cases. For example, if I have an English OpenGL game demo that happens to display Japanese text in one of the signboards in the scene because it is depicting a busy street in a Japanese City, then I need a way to detect if Japanese language is installed so that I can generate the text glyphs for that scene. If Japanese language is not installed, I can choose to display English text instead.

I found out that there is a API, EnumSystemLanguageGroups function, which can do this. This is the function signature below.

BOOL EnumSystemLanguageGroups(
  __in  LANGUAGEGROUP_ENUMPROC lpLanguageGroupEnumProc,
  __in  DWORD dwFlags,
  __in  LONG_PTR lParam
);

The first parameter, lpLanguageGroupEnumProc, is a callback function, of EnumLanguageGroupsProc type, which is called during system language enumeration. The second parameter, dwFlags, is a flag that can be passed in LGRPID_INSTALLED or LGRPID_SUPPORTED, depending which is the minimum support you want to detect. The third parameter, lParam, is an address of a variable that will be passed into the callback function. You can set the language enumeration result into this variable inside the callback function. Below is a code sample on how to detect if Japanese is installed.

#include <string>
#include <iostream>
#include <Windows.h>
#include <Winnls.h>

BOOL CALLBACK JapaneseEnumLanguageGroupsProc(
    LGRPID LanguageGroup,             // language-group identifier
    LPTSTR lpLanguageGroupString,     // language-group identifier string
    LPTSTR lpLanguageGroupNameString, // language-group name string
    DWORD dwFlags,                    // options
    LONG_PTR  lParam                  // callback parameter
    )
{
    LONG* plLang = (LONG*)(lParam);
    std::wstring strLang=lpLanguageGroupNameString;
    if(L"Japanese"==strLang&&dwFlags==LGRPID_INSTALLED)
    {
        *plLang = 1;
        return FALSE; // Do not enumerate anymore
    }

    return TRUE;
}

bool IsJapaneseLangInstalled()
{
    LONG lLang=0;
    EnumSystemLanguageGroups(
        JapaneseEnumLanguageGroupsProc, // callback function
        LGRPID_SUPPORTED,               // language groups
        (LONG_PTR)&lLang          // callback parameter
        );

    if(lLang==1)
        return true;

    return false;
}

int _tmain(int argc, _TCHAR* argv[])
{
	std::wstring strAns = IsJapaneseLangInstalled() ? L"Yes" : L"No" ;
	std::wcout<<L"Japanese Language Installed : " <<  strAns  << std::endl;

	return 0;
}

Note: For Chinese language, you have to choose whether you want to detect if Traditional Chinese, Simplified Chinese, or both are installed. The sample code to detect CJK is included as a source code download of this article.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read