Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When and how to use std::locale::messages?

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?

like image 568
xmllmx Avatar asked Sep 21 '13 10:09

xmllmx


People also ask

What does std:: locale do?

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, ... }

How to use locale in C++?

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("") .

What does locale mean in C++?

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.


1 Answers

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.

like image 115
Shervin Avatar answered Oct 15 '22 13:10

Shervin