1 //
2 //  Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
3 //
4 //  Distributed under the Boost Software License, Version 1.0. (See
5 //  accompanying file LICENSE_1_0.txt or copy at
6 //  http://www.boost.org/LICENSE_1_0.txt)
7 //
8 
9 #define BOOST_LOCALE_SOURCE
10 #include <boost/locale/util.hpp>
11 #include <boost/config.hpp>
12 #include <stdlib.h>
13 
14 #ifdef BOOST_MSVC
15 #  pragma warning(disable : 4996)
16 #endif
17 
18 #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
19 #ifndef NOMINMAX
20 #define NOMINMAX
21 #endif
22 #include <windows.h>
23 #define BOOST_LOCALE_USE_WIN32_API
24 #endif
25 
26 namespace boost {
27     namespace locale {
28         namespace util {
get_system_locale(bool use_utf8)29             std::string get_system_locale(bool use_utf8)
30             {
31                 char const *lang = 0;
32                 if(!lang || !*lang)
33                     lang = getenv("LC_CTYPE");
34                 if(!lang || !*lang)
35                     lang = getenv("LC_ALL");
36                 if(!lang || !*lang)
37                     lang = getenv("LANG");
38                 #ifndef BOOST_LOCALE_USE_WIN32_API
39                 (void)use_utf8; // not relevant for non-windows
40                 if(!lang || !*lang)
41                     lang = "C";
42                 return lang;
43                 #else
44                 if(lang && *lang) {
45                     return lang;
46                 }
47                 char buf[10];
48                 if(GetLocaleInfoA(LOCALE_USER_DEFAULT,LOCALE_SISO639LANGNAME,buf,sizeof(buf))==0)
49                     return "C";
50                 std::string lc_name = buf;
51                 if(GetLocaleInfoA(LOCALE_USER_DEFAULT,LOCALE_SISO3166CTRYNAME,buf,sizeof(buf))!=0) {
52                     lc_name += "_";
53                     lc_name += buf;
54                 }
55                 if(!use_utf8) {
56                     if(GetLocaleInfoA(LOCALE_USER_DEFAULT,LOCALE_IDEFAULTANSICODEPAGE,buf,sizeof(buf))!=0) {
57                         if(atoi(buf)==0)
58                             lc_name+=".UTF-8";
59                         else {
60                             lc_name +=".windows-";
61                             lc_name +=buf;
62                         }
63                     }
64                     else {
65                         lc_name += "UTF-8";
66                     }
67                 }
68                 else {
69                     lc_name += ".UTF-8";
70                 }
71                 return lc_name;
72 
73                 #endif
74             }
75         } // impl
76     } // locale
77 } // boost
78 
79 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
80 
81