Language-specific operations of console applications can follow the locale settings of the system. This will guarantee locale-authentic formatting of numbers, date, time, currency values, and collation (sorting) operations. Locale support is available for text-mode applications through both C run-time (CRT) and Win32. However, these mechanisms should never be mixed. For new Windows applications, Win32 mechanisms are preferred over those in CRT with regard to world-ready code.
CRT locale support is built around the (_w)setlocale(category, locale) call, discussed in Chapter 3. A call to this function defines the results of all subsequent CRT-based locale-sensitive operations, not only the character encoding. The category argument defines scope of environment changes after setlocale is called.
In order to set the rules for formatting locale-sensitive data in accordance with the user locale, the following calls can be executed:
_wsetlocale (LC_COLLATE, L(".OCP") ); // sets the sort order _wsetlocale (LC_MONETARY, L(".OCP") ); // sets the currency formatting // rules _wsetlocale (LC_NUMERIC, L(".OCP") ); // sets the formatting of // numerals _wsetlocale (LC_TIME, L(".OCP") ); // defines the date/time // formatting
As mentioned in Chapter 3, ".OCP" and ".ACP" parameters always refer to the settings of the user locale, not the system locale. While selecting this locale for LC_CTYPE or LC_ALL is not a good choice, all other categories should be set to match the user locale, unless your console must be explicitly independent of the user's settings.
Console applications based on the Win32 API and .NET Framework are no different from other Win32 or .NET applications in their ability to access the Win32 NLS API or the System.Globalization namespace. Using them, however, might cause some display problems because of the special rules that apply to console input/output (I/O), as described in Chapter 3. The user locale or CurrentCulture might require the date and time to be formatted using characters in a range unsupported by the console. For example, you cannot write out dates in a long Hebrew format or times with Arabic A.M./P.M. symbols. Some special design decisions must be made to avoid displaying culturally correct yet unreadable data. However, you should avoid taking the easy route of hard-coding the format for your output.