#include <iostream> #include <locale> int main(int argc, char** argv) { using namespace std; locale::global(locale("")); // set the global locale // imbue it on all the std streams cin.imbue(locale()); cout.imbue(locale()); cerr.imbue(locale()); wcin.imbue(locale()); wcout.imbue(locale()); wcerr.imbue(locale()); return MyObject(argc, argv).doit(); }
#include <iostream>
#include <locale>
int main() {
using namespace std;
cin.imbue(locale("")); // the user's preferred locale
cout.imbue(locale::classic());
double f;
while (cin >> f) cout << f << endl;
return (cin.fail() != 0);
}
// file: Date.h
#include <iosfwd>
#include <string>
#include <locale>
class Date {
public:
Date(unsigned day, unsigned month, unsigned year);
std::string asString(const std::locale& = std::locale());
};
std::istream& operator>>(std::istream& s, Date& d);
std::ostream& operator<<(std::ostream& s, Date d);
// file: Date.C #include "Date" // includes <ctime> #include <sstream> std::string Date::asString(const std::locale& l) { using namespace std; ostringstream s; s.imbue(l); s << *this; return s.str(); } std::istream& operator>>(std::istream& s, Date& d) { using namespace std; istream::sentry cerberos(s); if (cerberos) { ios_base::iostate err = goodbit; struct tm t; use_facet<time_get<char>>(s.getloc()).get_date(s, 0, s, err, &t); if (!err) d = Date(t.tm_day, t.tm_mon + 1, t.tm_year + 1900); s.setstate(err); } return s; }
// file: <jctype> #include <locale> namespace My { using namespace std; class JCtype : public locale::facet { public: static locale::id id; // required for use as a new locale facet bool is_kanji (wchar_t c) const; JCtype() { } protected: ~JCtype() { } }; } // file: filt.C #include <iostream> #include <locale> #include "jctype" // above std::locale::id My::JCtype::id; // the static JCtype member declared above. int main() { using namespace std; using wctype = ctype<wchar_t>; locale loc(locale(""), // the user's preferred locale ... new My::JCtype); // and a new feature ... wchar_t c = use_facet<wctype>(loc).widen('!'); if (!use_facet<My::JCtype>(loc).is_kanji(c)) cout << "no it isn't!" << endl; }
// file: my_bool.C #include <iostream> #include <locale> #include <string> namespace My { using namespace std; using cnumpunct = numpunct_byname<char>; class BoolNames : public cnumpunct { protected: string do_truename() const { return "Oui Oui!"; } string do_falsename() const { return "Mais Non!"; } ~BoolNames() { } public: BoolNames(const char* name) : cnumpunct(name) { } }; } int main(int argc, char** argv) { using namespace std; // make the user's preferred locale, except for... locale loc(locale(""), new My::BoolNames("")); cout.imbue(loc); cout << boolalpha << "Any arguments today? " << (argc > 1) << endl; }