Search code examples
c++locale

Is it possible to create your own custom locale


Since Windows doesnt have a C++ locale with UTF8 support by default, i would like to construct a custom locale object which supports UTF8 (by creating it with a custom ctype facet).

How can i construct a locale object with a my own ctype implementation (i only found functions to construct a locale using an already existing locale as base..)

If C++ does not support construction of locales with a custom ctype facet at all, why is that so ?


Solution

  • It's possible to create custom facets by inheriting from std::locale::facet. Locales can use those custom facets as in following code:

    class custom_facet : public std::locale::facet {
    public:
        static std::locale::id id;
        custom_facet(int);  
        int custom_value() const; 
        };
    
    std::locale  custom_locale ( std::locale(), new custom_facet() );
    int s = std::use_facet<custom_facet>(custom_locale).custom_value();