How to get current locale of my environment?

前端 未结 5 604
挽巷
挽巷 2020-12-28 13:06

Had tried following code in Linux, but always return \'C\' under different LANG settings.

#include 
#include 
#i         


        
5条回答
  •  再見小時候
    2020-12-28 13:58

    From man 3 setlocale (New maxim: "When in doubt, read the entire manpage."):

    If locale is "", each part of the locale that should be modified is set according to the environment variables.

    So, we can read the environment variables by calling setlocale at the beginning of the program, as follows:

    #include 
    #include 
    using namespace std;
    
    int main()
    {
        setlocale(LC_ALL, "");
        cout << "LC_ALL: " << setlocale(LC_ALL, NULL) << endl;
        cout << "LC_CTYPE: " << setlocale(LC_CTYPE, NULL) << endl;
        return 0;
    }
    

    My system does not support the zh_CN locale, as the following output reveals:

    $ ./a.out 
    LC_ALL: en_US.utf8
    LC_CTYPE: en_US.utf8
    $ export LANG=zh_CN.UTF-8
    $ ./a.out 
    LC_ALL: C
    LC_CTYPE: C
    

    Windows: I have no idea about Windows locales. I suggest starting with an MSDN search, and then opening a separate Stack Overflow question if you still have questions.

提交回复
热议问题