Click here to Skip to main content
13,560,290 members
Click here to Skip to main content
Add your own
alternative version

Stats

5.4K views
92 downloads
6 bookmarked
Posted 21 Sep 2017
Licenced CPOL

Detecting active language

, 21 Sep 2017
Rate this:
Please Sign up or sign in to vote.
How to detect the currently use language regardless of the active application

Introduction

This article was written following a need to detect the currently used language, regardless of the currently used application. For the purpose of demonstration, the language's direciton (either left to right or right to left) is displayed.

Locales and Languages

According to Microsoft MSDN, the term "language" indicates a collection of properties used in spoken and written communication. Each language has a language name and a language identifier that indicate the particular code page (ANSI, DOS, Macintosh) used to represent the geographical location for the language on the operating system. A neutral language is indicated by a name such as "en" for English. A more geographically specific language can be indicated by a name that includes both locale and country/region information. For example, the locale English (United States) has the language name "en-US". The one used for Hebrew is He-HE. Some users can switch between two or more installed languages during their day to day work. For example, a user who writes in Arabic or Hebrew will certainly need to use Latin characters as well, so in these cases pressing Left SHIFT + ALT / Right SHIFT + ALT make this switch.

The User Interface

In terms of user interface, since our goal would be to detect the language used in the currently active screen, our small program should be passive. Stay behind and yet appear on top (so you can see the indication it provides).

As a result, you will be able to view the current status (in our case, the current active language) regardless of the active application. In the example bellow, the active window is Notepad and the currently used language is Hebrew.

The Source Code

In order to detect the currently selected language, we call GetLocaleInfoW(). There is a more recent version, GetLocaleInfoEx() which is recomended if you target Windows Vista and higher. In my example, I used GetLocaleInfoW(). My example concentrate in one aspect of the locality: the direction of the text which can be either from left to right or from right to left. With the same API you can get the language name, the default currently and much more information.

    auto layout = GetKeyboardLayout(GetWindowThreadProcessId(win->GetSafeHwnd(), NULL));
    auto lcid = MAKELCID(LOWORD(layout), SORT_DEFAULT);
    LOCALESIGNATURE localesig;
    if (GetLocaleInfoW(lcid, LOCALE_FONTSIGNATURE, (LPWSTR)&localesig, sizeof(localesig) / sizeof(WCHAR)) != 0)
        ret = (localesig.lsUsb[3] & 0x08000000) != 0;

As a result, "ret" will hold either TRUE if the default text direction is from Right to Left, or FALSE, or the default text direction is from Left to Right.

Language Identifyers

There is a list of available and supported languages in this list. Each language identifier is composed of a primary language identifier indicating the language and a sublanguage identifier indicating the country/region. The language identifier corresponds to a particular locale, for example, English (United States), represented as "en-US". The language identifier is used as part of the locale identifier.

Why MFC

I used MFC for the interface however all API calls are pure Win32. Using MFC allows quick creation of applications, especially when user interface is needed. In my case, I create a Timer by calling 

SetTimer(TIMER_EVENT_SHOWLANG, 1000,NULL);

and then, I can place my code in 

void CDisplayLocaleDlg::OnTimer(UINT_PTR nIDEvent)

knowing it will be called every second.

When you compile the code and run it, you can use any other software or go to any other window and still see the window's title and the currently selected language direction shown on the user interface of our program. That can be expanded to display:

- Default calendar: see: https://msdn.microsoft.com/en-us/library/windows/desktop/dd317734(v=vs.85).aspx

- Default currency: see: https://msdn.microsoft.com/en-us/library/windows/desktop/dd318084(v=vs.85).aspx

 

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Michael Haephrati
CEO Secured Globe, Inc.
United States United States
Michael Haephrati, CEO and co-founder of Secured Globe, Inc. Worked on many ventures starting from HarmonySoft, designing Rashumon, the first Graphical Multi-lingual word processor for Amiga computer. During 1995-1996 he worked as a Contractor with Apple at Cupertino.



You may also be interested in...

Comments and Discussions

 
Questionthank you Pin
Jacopo MAX2-Oct-17 2:48
memberJacopo MAX2-Oct-17 2:48 
AnswerRe: thank you Pin
Michael Haephrati2-Oct-17 5:30
professionalMichael Haephrati2-Oct-17 5:30 
QuestionHave you consider to post this as a tip? Pin
Nelek21-Sep-17 19:34
protectorNelek21-Sep-17 19:34 
AnswerRe: Have you consider to post this as a tip? Pin
Michael Haephrati24-Sep-17 2:33
professionalMichael Haephrati24-Sep-17 2:33 
GeneralRe: Have you consider to post this as a tip? Pin
Nelek24-Sep-17 21:42
protectorNelek24-Sep-17 21:42 
AnswerRe: Have you consider to post this as a tip? Pin
Michael Haephrati25-Sep-17 0:48
professionalMichael Haephrati25-Sep-17 0:48 
GeneralRe: Have you consider to post this as a tip? Pin
Nelek25-Sep-17 7:22
protectorNelek25-Sep-17 7:22 
AnswerRe: Have you consider to post this as a tip? Pin
Michael Haephrati25-Sep-17 9:12
professionalMichael Haephrati25-Sep-17 9:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02-2016 | 2.8.180527.3 | Last Updated 21 Sep 2017
Article Copyright 2017 by Michael Haephrati
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid