The C++ standard defines six categories of facets: collate
, ctype
, monetary
, numeric
, time
, and messages
.
I have known the usage of the first five, but I don't know when and how to use the last one: std::locale::messages
.
Any illustrative examples?
The std::locale class keeps reference counters on installed facets and can be efficiently copied. You can also create your own facets and install them into existing locale objects. For example: class measure : public std::locale::facet { public: typedef enum { inches, ... }
You can create a C++ locale object for the native locale by calling the constructor std::locale("") , that is, by requesting a named locale using an empty string as the name. The empty string tells the system to get the locale name from the environment, in the same way as the C library function std::setlocale("") .
In C++, a locale is a class called locale provided by the Standard C++ Library. The C++ class locale differs from the C locale because it is more than a language table, or data representation of the various culture and language dependencies.
std::locale::messages
is used for opening message catalogues (most commonly GNU gettext
) including translated strings. Here is an example which opens an existing message catalogue using on Linux (for sed
) in German, retrieves (using get()
) and outputs the translations for the English strings:
#include <iostream>
#include <locale>
int main()
{
std::locale loc("de_DE.utf8");
std::cout.imbue(loc);
auto& facet = std::use_facet<std::messages<char>>(loc);
auto cat = facet.open("sed", loc);
if(cat < 0 )
std::cout << "Could not open german \"sed\" message catalog\n";
else
std::cout << "\"No match\" in German: "
<< facet.get(cat, 0, 0, "No match") << '\n'
<< "\"Memory exhausted\" in German: "
<< facet.get(cat, 0, 0, "Memory exhausted") << '\n';
facet.close(cat);
}
which outputs:
"No match" in German: Keine Übereinstimmung
"Memory exhausted" in German: Speicher erschöpft
Edit: Clarification according to this comment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With