aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/restricted/boost/libs/locale/src/util/default_locale.cpp
blob: b0aab03c7119daa93c7643efd7d6b5de14dcef9e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//
//  Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
//
//  Distributed under the Boost Software License, Version 1.0. (See
//  accompanying file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)
//

#define BOOST_LOCALE_SOURCE
#include <boost/locale/util.hpp>
#include <boost/config.hpp>
#include <stdlib.h>

#ifdef BOOST_MSVC
#  pragma warning(disable : 4996)
#endif

#if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#define BOOST_LOCALE_USE_WIN32_API
#endif

namespace boost {
    namespace locale {
        namespace util {
            std::string get_system_locale(bool use_utf8)
            {
                char const *lang = 0;
                if(!lang || !*lang)
                    lang = getenv("LC_CTYPE");
                if(!lang || !*lang)
                    lang = getenv("LC_ALL");
                if(!lang || !*lang)
                    lang = getenv("LANG");
                #ifndef BOOST_LOCALE_USE_WIN32_API
                (void)use_utf8; // not relevant for non-windows
                if(!lang || !*lang)
                    lang = "C";
                return lang;
                #else
                if(lang && *lang) {
                    return lang;
                }
                char buf[10];
                if(GetLocaleInfoA(LOCALE_USER_DEFAULT,LOCALE_SISO639LANGNAME,buf,sizeof(buf))==0)
                    return "C";
                std::string lc_name = buf;
                if(GetLocaleInfoA(LOCALE_USER_DEFAULT,LOCALE_SISO3166CTRYNAME,buf,sizeof(buf))!=0) {
                    lc_name += "_";
                    lc_name += buf;
                }
                if(!use_utf8) {
                    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";
                }
                return lc_name;
                
                #endif
            }
        } // impl
    } // locale
} // boost

// vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4