Skip to content

Commit

Permalink
Fix order of environment variables when getting system default locale
Browse files Browse the repository at this point in the history
  • Loading branch information
dimztimz authored and Flamefire committed Jan 1, 2023
1 parent a7536a5 commit e5c613d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
4 changes: 2 additions & 2 deletions doc/locale_gen.txt
Expand Up @@ -46,8 +46,8 @@ Of course we can also specify the locale manually
- Even if your application uses wide strings everywhere, you should specify the
8-bit encoding to use for 8-bit stream IO operations like \c cout or \c fstream.
\n
- The default locale is defined by the environment variables \c LC_CTYPE , \c LC_ALL , and \c LANG
in that order (i.e. \c LC_CTYPE first and \c LANG last). On Windows, the library
- The default locale is defined by the environment variables \c LC_ALL , \c LC_CTYPE , and \c LANG
in that order (i.e. \c LC_ALL first and \c LANG last). On Windows, the library
also queries the \c LOCALE_USER_DEFAULT option in the Win32 API when these variables
are not set.

Expand Down
17 changes: 8 additions & 9 deletions include/boost/locale/util.hpp
Expand Up @@ -22,15 +22,14 @@ namespace boost { namespace locale {

/// \brief Return default system locale name in POSIX format.
///
/// This function tries to detect the locale using, LC_CTYPE, LC_ALL and LANG environment
/// variables in this order and if all of them unset, in POSIX platforms it returns "C"
///
/// On Windows additionally to check the above environment variables, this function
/// tries to creates locale name from ISO-339 and ISO-3199 country codes defined
/// for user default locale.
/// If \a use_utf8_on_windows is true it sets the encoding to UTF-8, otherwise, if system
/// locale supports ANSI code-page it defines the ANSI encoding like windows-1252, otherwise it fall-backs
/// to UTF-8 encoding if ANSI code-page is not available.
/// This function tries to detect the locale using LC_ALL, LC_CTYPE and LANG environment
/// variables in this order and if all of them are unset, on POSIX platforms it returns "C".
/// On Windows additionally to the above environment variables, this function
/// tries to create the locale name from ISO-339 and ISO-3199 country codes defined
/// for the users default locale.
/// If \a use_utf8_on_windows is true it sets the encoding to UTF-8,
/// otherwise, if the system locale supports ANSI codepages it defines the ANSI encoding, e.g. windows-1252,
/// otherwise (if ANSI codepage is not available) it uses UTF-8 encoding.
BOOST_LOCALE_DECL
std::string get_system_locale(bool use_utf8_on_windows = false);

Expand Down
23 changes: 11 additions & 12 deletions src/boost/locale/util/default_locale.cpp
Expand Up @@ -17,24 +17,24 @@
#endif

namespace boost { namespace locale { namespace util {
std::string get_system_locale(bool use_utf8)
std::string get_system_locale(bool use_utf8_on_windows)
{
const char* lang = 0;
if(!lang || !*lang)
lang = getenv("LC_CTYPE");
if(!lang || !*lang)
lang = getenv("LC_ALL");
if(!lang || !*lang)
lang = getenv("LC_CTYPE");
if(!lang || !*lang)
lang = getenv("LANG");
#ifndef BOOST_LOCALE_USE_WIN32_API
(void)use_utf8; // not relevant for non-windows
(void)use_utf8_on_windows; // not relevant for non-windows
if(!lang || !*lang)
lang = "C";
return lang;
#else
if(lang && *lang) {
if(lang && *lang)
return lang;
}

char buf[10];
if(GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SISO639LANGNAME, buf, sizeof(buf)) == 0)
return "C";
Expand All @@ -43,19 +43,18 @@ namespace boost { namespace locale { namespace util {
lc_name += "_";
lc_name += buf;
}
if(!use_utf8) {
if(use_utf8_on_windows)
lc_name += ".UTF-8";
else {
if(GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_IDEFAULTANSICODEPAGE, buf, sizeof(buf)) != 0) {
if(atoi(buf) == 0)
lc_name += ".UTF-8";
else {
lc_name += ".windows-";
lc_name += buf;
}
} else {
lc_name += "UTF-8";
}
} else {
lc_name += ".UTF-8";
} else
lc_name += ".UTF-8";
}
return lc_name;

Expand Down

0 comments on commit e5c613d

Please sign in to comment.