std::locale与boost::locale的学习

1. 什么是facet, locale

facet ['fæsɪt]的原意,是宝石切割出来的一个平面。

locale[ləʊˈkæl],表示本地化,

locale

the container that holds all the required information about a specific culture, such as number formatting patterns, date and time formatting, currency, case conversion etc.

而facet代表着locale中存放的某一种具体信息,比如字符转换、货币等等。

可以通过use_facet得到某个locale中的一个facet

   1: std::ctype<char> const &ctype_facet = std::use_facet<std::ctype<char> >(some_locale);
   2: char upper_a = ctype_facet.toupper('a');

也可以将某个locale浸透(imbue [ɪmˈbjuː])到某个stream中,使该stream按照该locale规定的本地化格式进行输出。

   1: cout.imbue(std::locale("en_US.UTF-8"));
   2: cout << 1345.45 << endl;
   3: cout.imbue(std::locale("ru_RU.UTF-8"));
   4: cout << 1345.45 << endl;

你也可以创建自己的std::locale::facet的派生类,用来建立自定义的本地化规则,然后将其安装到某个locale对象中。

boost::locale库就是通过这种方式扩展了std::locale库的内容。

原文地址:https://www.cnblogs.com/long123king/p/3520275.html