|
|
|
|
|
|
|
|
|
|
|
|
ISO C++
|
ISO C++
|
|
|
|
|
messages
|
messages
|
|
|
|
|
|
|
|
|
messages
|
messages
|
|
|
|
|
The std::messages facet implements message retrieval functionality
|
The std::messages facet implements message retrieval functionality
|
equivalent to Java's java.text.MessageFormat .using either GNU gettext
|
equivalent to Java's java.text.MessageFormat .using either GNU gettext
|
or IEEE 1003.1-200 functions.
|
or IEEE 1003.1-200 functions.
|
|
|
|
|
|
|
Requirements
|
Requirements
|
|
|
|
|
The std::messages facet is probably the most vaguely defined facet in
|
The std::messages facet is probably the most vaguely defined facet in
|
the standard library. It's assumed that this facility was built into
|
the standard library. It's assumed that this facility was built into
|
the standard library in order to convert string literals from one
|
the standard library in order to convert string literals from one
|
locale to the other. For instance, converting the "C" locale's
|
locale to the other. For instance, converting the "C" locale's
|
const char* c = "please" to a German-localized "bitte"
|
const char* c = "please" to a German-localized "bitte"
|
during program execution.
|
during program execution.
|
|
|
|
|
|
|
|
|
22.2.7.1 - Template class messages [lib.locale.messages]
|
22.2.7.1 - Template class messages [lib.locale.messages]
|
|
|
|
|
|
|
|
|
This class has three public member functions, which directly
|
This class has three public member functions, which directly
|
correspond to three protected virtual member functions.
|
correspond to three protected virtual member functions.
|
|
|
|
|
|
|
The public member functions are:
|
The public member functions are:
|
|
|
|
|
|
|
catalog open(const string&, const locale&) const
|
catalog open(const string&, const locale&) const
|
|
|
|
|
|
|
string_type get(catalog, int, int, const string_type&) const
|
string_type get(catalog, int, int, const string_type&) const
|
|
|
|
|
|
|
void close(catalog) const
|
void close(catalog) const
|
|
|
|
|
|
|
While the virtual functions are:
|
While the virtual functions are:
|
|
|
|
|
|
|
catalog do_open(const string&, const locale&) const
|
catalog do_open(const string&, const locale&) const
|
|
|
|
|
|
|
|
|
-1- Returns: A value that may be passed to get() to retrieve a
|
-1- Returns: A value that may be passed to get() to retrieve a
|
message, from the message catalog identified by the string name
|
message, from the message catalog identified by the string name
|
according to an implementation-defined mapping. The result can be used
|
according to an implementation-defined mapping. The result can be used
|
until it is passed to close(). Returns a value less than 0 if no such
|
until it is passed to close(). Returns a value less than 0 if no such
|
catalog can be opened.
|
catalog can be opened.
|
|
|
|
|
|
|
|
|
|
|
string_type do_get(catalog, int, int, const string_type&) const
|
string_type do_get(catalog, int, int, const string_type&) const
|
|
|
|
|
|
|
|
|
-3- Requires: A catalog cat obtained from open() and not yet closed.
|
-3- Requires: A catalog cat obtained from open() and not yet closed.
|
-4- Returns: A message identified by arguments set, msgid, and dfault,
|
-4- Returns: A message identified by arguments set, msgid, and dfault,
|
according to an implementation-defined mapping. If no such message can
|
according to an implementation-defined mapping. If no such message can
|
be found, returns dfault.
|
be found, returns dfault.
|
|
|
|
|
|
|
|
|
|
|
void do_close(catalog) const
|
void do_close(catalog) const
|
|
|
|
|
|
|
|
|
-5- Requires: A catalog cat obtained from open() and not yet closed.
|
-5- Requires: A catalog cat obtained from open() and not yet closed.
|
-6- Effects: Releases unspecified resources associated with cat.
|
-6- Effects: Releases unspecified resources associated with cat.
|
-7- Notes: The limit on such resources, if any, is implementation-defined.
|
-7- Notes: The limit on such resources, if any, is implementation-defined.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Design
|
Design
|
|
|
|
|
A couple of notes on the standard.
|
A couple of notes on the standard.
|
|
|
|
|
|
|
First, why is messages_base::catalog specified as a typedef
|
First, why is messages_base::catalog specified as a typedef
|
to int? This makes sense for implementations that use
|
to int? This makes sense for implementations that use
|
catopen , but not for others. Fortunately, it's not heavily
|
catopen , but not for others. Fortunately, it's not heavily
|
used and so only a minor irritant.
|
used and so only a minor irritant.
|
|
|
|
|
|
|
Second, by making the member functions const , it is
|
Second, by making the member functions const , it is
|
impossible to save state in them. Thus, storing away information used
|
impossible to save state in them. Thus, storing away information used
|
in the 'open' member function for use in 'get' is impossible. This is
|
in the 'open' member function for use in 'get' is impossible. This is
|
unfortunate.
|
unfortunate.
|
|
|
|
|
|
|
The 'open' member function in particular seems to be oddly
|
The 'open' member function in particular seems to be oddly
|
designed. The signature seems quite peculiar. Why specify a const
|
designed. The signature seems quite peculiar. Why specify a const
|
string& argument, for instance, instead of just const
|
string& argument, for instance, instead of just const
|
char* ? Or, why specify a const locale& argument that is
|
char* ? Or, why specify a const locale& argument that is
|
to be used in the 'get' member function? How, exactly, is this locale
|
to be used in the 'get' member function? How, exactly, is this locale
|
argument useful? What was the intent? It might make sense if a locale
|
argument useful? What was the intent? It might make sense if a locale
|
argument was associated with a given default message string in the
|
argument was associated with a given default message string in the
|
'open' member function, for instance. Quite murky and unclear, on
|
'open' member function, for instance. Quite murky and unclear, on
|
reflection.
|
reflection.
|
|
|
|
|
|
|
Lastly, it seems odd that messages, which explicitly require code
|
Lastly, it seems odd that messages, which explicitly require code
|
conversion, don't use the codecvt facet. Because the messages facet
|
conversion, don't use the codecvt facet. Because the messages facet
|
has only one template parameter, it is assumed that ctype, and not
|
has only one template parameter, it is assumed that ctype, and not
|
codecvt, is to be used to convert between character sets.
|
codecvt, is to be used to convert between character sets.
|
|
|
|
|
|
|
It is implicitly assumed that the locale for the default message
|
It is implicitly assumed that the locale for the default message
|
string in 'get' is in the "C" locale. Thus, all source code is assumed
|
string in 'get' is in the "C" locale. Thus, all source code is assumed
|
to be written in English, so translations are always from "en_US" to
|
to be written in English, so translations are always from "en_US" to
|
other, explicitly named locales.
|
other, explicitly named locales.
|
|
|
|
|
|
|
|
|
|
|
Implementation
|
Implementation
|
|
|
|
|
Models
|
Models
|
|
|
This is a relatively simple class, on the face of it. The standard
|
This is a relatively simple class, on the face of it. The standard
|
specifies very little in concrete terms, so generic
|
specifies very little in concrete terms, so generic
|
implementations that are conforming yet do very little are the
|
implementations that are conforming yet do very little are the
|
norm. Adding functionality that would be useful to programmers and
|
norm. Adding functionality that would be useful to programmers and
|
comparable to Java's java.text.MessageFormat takes a bit of work,
|
comparable to Java's java.text.MessageFormat takes a bit of work,
|
and is highly dependent on the capabilities of the underlying
|
and is highly dependent on the capabilities of the underlying
|
operating system.
|
operating system.
|
|
|
|
|
|
|
Three different mechanisms have been provided, selectable via
|
Three different mechanisms have been provided, selectable via
|
configure flags:
|
configure flags:
|
|
|
|
|
|
|
|
|
|
|
generic
|
generic
|
|
|
|
|
This model does very little, and is what is used by default.
|
This model does very little, and is what is used by default.
|
|
|
|
|
|
|
|
|
|
|
gnu
|
gnu
|
|
|
|
|
The gnu model is complete and fully tested. It's based on the
|
The gnu model is complete and fully tested. It's based on the
|
GNU gettext package, which is part of glibc. It uses the
|
GNU gettext package, which is part of glibc. It uses the
|
functions textdomain, bindtextdomain, gettext to
|
functions textdomain, bindtextdomain, gettext to
|
implement full functionality. Creating message catalogs is a
|
implement full functionality. Creating message catalogs is a
|
relatively straight-forward process and is lightly documented
|
relatively straight-forward process and is lightly documented
|
below, and fully documented in gettext's distributed
|
below, and fully documented in gettext's distributed
|
documentation.
|
documentation.
|
|
|
|
|
|
|
|
|
|
|
ieee_1003.1-200x
|
ieee_1003.1-200x
|
|
|
|
|
This is a complete, though untested, implementation based on
|
This is a complete, though untested, implementation based on
|
the IEEE standard. The functions catopen, catgets,
|
the IEEE standard. The functions catopen, catgets,
|
catclose are used to retrieve locale-specific messages
|
catclose are used to retrieve locale-specific messages
|
given the appropriate message catalogs that have been
|
given the appropriate message catalogs that have been
|
constructed for their use. Note, the script
|
constructed for their use. Note, the script
|
po2msg.sed that is part of the gettext distribution can
|
po2msg.sed that is part of the gettext distribution can
|
convert gettext catalogs into catalogs that
|
convert gettext catalogs into catalogs that
|
catopen can use.
|
catopen can use.
|
|
|
|
|
|
|
|
|
|
|
A new, standards-conformant non-virtual member function signature was
|
A new, standards-conformant non-virtual member function signature was
|
added for 'open' so that a directory could be specified with a given
|
added for 'open' so that a directory could be specified with a given
|
message catalog. This simplifies calling conventions for the gnu
|
message catalog. This simplifies calling conventions for the gnu
|
model.
|
model.
|
|
|
|
|
|
|
|
|
|
|
The GNU Model
|
The GNU Model
|
|
|
|
|
The messages facet, because it is retrieving and converting
|
The messages facet, because it is retrieving and converting
|
between characters sets, depends on the ctype and perhaps the
|
between characters sets, depends on the ctype and perhaps the
|
codecvt facet in a given locale. In addition, underlying "C"
|
codecvt facet in a given locale. In addition, underlying "C"
|
library locale support is necessary for more than just the
|
library locale support is necessary for more than just the
|
LC_MESSAGES mask: LC_CTYPE is also
|
LC_MESSAGES mask: LC_CTYPE is also
|
necessary. To avoid any unpleasantness, all bits of the "C" mask
|
necessary. To avoid any unpleasantness, all bits of the "C" mask
|
(i.e. LC_ALL ) are set before retrieving messages.
|
(i.e. LC_ALL ) are set before retrieving messages.
|
|
|
|
|
|
|
Making the message catalogs can be initially tricky, but become
|
Making the message catalogs can be initially tricky, but become
|
quite simple with practice. For complete info, see the gettext
|
quite simple with practice. For complete info, see the gettext
|
documentation. Here's an idea of what is required:
|
documentation. Here's an idea of what is required:
|
|
|
|
|
|
|
|
|
|
|
Make a source file with the required string literals that need
|
Make a source file with the required string literals that need
|
to be translated. See intl/string_literals.cc for
|
to be translated. See intl/string_literals.cc for
|
an example.
|
an example.
|
|
|
|
|
|
|
|
|
|
|
Make initial catalog (see "4 Making the PO Template File" from
|
Make initial catalog (see "4 Making the PO Template File" from
|
the gettext docs).
|
the gettext docs).
|
|
|
xgettext --c++ --debug string_literals.cc -o libstdc++.pot
|
xgettext --c++ --debug string_literals.cc -o libstdc++.pot
|
|
|
|
|
|
|
|
|
Make language and country-specific locale catalogs.
|
Make language and country-specific locale catalogs.
|
|
|
cp libstdc++.pot fr_FR.po
|
cp libstdc++.pot fr_FR.po
|
|
|
|
|
cp libstdc++.pot de_DE.po
|
cp libstdc++.pot de_DE.po
|
|
|
|
|
|
|
|
|
|
|
Edit localized catalogs in emacs so that strings are
|
Edit localized catalogs in emacs so that strings are
|
translated.
|
translated.
|
|
|
|
|
emacs fr_FR.po
|
emacs fr_FR.po
|
|
|
|
|
|
|
|
|
Make the binary mo files.
|
Make the binary mo files.
|
|
|
msgfmt fr_FR.po -o fr_FR.mo
|
msgfmt fr_FR.po -o fr_FR.mo
|
|
|
|
|
msgfmt de_DE.po -o de_DE.mo
|
msgfmt de_DE.po -o de_DE.mo
|
|
|
|
|
|
|
|
|
Copy the binary files into the correct directory structure.
|
Copy the binary files into the correct directory structure.
|
|
|
cp fr_FR.mo (dir)/fr_FR/LC_MESSAGES/libstdc++.mo
|
cp fr_FR.mo (dir)/fr_FR/LC_MESSAGES/libstdc++.mo
|
|
|
|
|
cp de_DE.mo (dir)/de_DE/LC_MESSAGES/libstdc++.mo
|
cp de_DE.mo (dir)/de_DE/LC_MESSAGES/libstdc++.mo
|
|
|
|
|
|
|
|
|
Use the new message catalogs.
|
Use the new message catalogs.
|
|
|
locale loc_de("de_DE");
|
locale loc_de("de_DE");
|
|
|
|
|
|
|
use_facet<messages<char> >(loc_de).open("libstdc++", locale(), dir);
|
use_facet<messages<char> >(loc_de).open("libstdc++", locale(), dir);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Use
|
Use
|
|
|
A simple example using the GNU model of message conversion.
|
A simple example using the GNU model of message conversion.
|
|
|
|
|
|
|
#include <iostream>
|
#include <iostream>
|
#include <locale>
|
#include <locale>
|
using namespace std;
|
using namespace std;
|
|
|
void test01()
|
void test01()
|
{
|
{
|
typedef messages<char>::catalog catalog;
|
typedef messages<char>::catalog catalog;
|
const char* dir =
|
const char* dir =
|
"/mnt/egcs/build/i686-pc-linux-gnu/libstdc++/po/share/locale";
|
"/mnt/egcs/build/i686-pc-linux-gnu/libstdc++/po/share/locale";
|
const locale loc_de("de_DE");
|
const locale loc_de("de_DE");
|
const messages<char>& mssg_de = use_facet<messages<char> >(loc_de);
|
const messages<char>& mssg_de = use_facet<messages<char> >(loc_de);
|
|
|
catalog cat_de = mssg_de.open("libstdc++", loc_de, dir);
|
catalog cat_de = mssg_de.open("libstdc++", loc_de, dir);
|
string s01 = mssg_de.get(cat_de, 0, 0, "please");
|
string s01 = mssg_de.get(cat_de, 0, 0, "please");
|
string s02 = mssg_de.get(cat_de, 0, 0, "thank you");
|
string s02 = mssg_de.get(cat_de, 0, 0, "thank you");
|
cout << "please in german:" << s01 << '\n';
|
cout << "please in german:" << s01 << '\n';
|
cout << "thank you in german:" << s02 << '\n';
|
cout << "thank you in german:" << s02 << '\n';
|
mssg_de.close(cat_de);
|
mssg_de.close(cat_de);
|
}
|
}
|
|
|
|
|
|
|
|
|
|
|
Future
|
Future
|
|
|
|
|
|
|
|
|
Things that are sketchy, or remain unimplemented:
|
Things that are sketchy, or remain unimplemented:
|
|
|
|
|
|
|
|
|
_M_convert_from_char, _M_convert_to_char are in flux,
|
_M_convert_from_char, _M_convert_to_char are in flux,
|
depending on how the library ends up doing character set
|
depending on how the library ends up doing character set
|
conversions. It might not be possible to do a real character
|
conversions. It might not be possible to do a real character
|
set based conversion, due to the fact that the template
|
set based conversion, due to the fact that the template
|
parameter for messages is not enough to instantiate the
|
parameter for messages is not enough to instantiate the
|
codecvt facet (1 supplied, need at least 2 but would prefer
|
codecvt facet (1 supplied, need at least 2 but would prefer
|
3).
|
3).
|
|
|
|
|
|
|
|
|
|
|
There are issues with gettext needing the global locale set
|
There are issues with gettext needing the global locale set
|
to extract a message. This dependence on the global locale
|
to extract a message. This dependence on the global locale
|
makes the current "gnu" model non MT-safe. Future versions
|
makes the current "gnu" model non MT-safe. Future versions
|
of glibc, i.e. glibc 2.3.x will fix this, and the C++ library
|
of glibc, i.e. glibc 2.3.x will fix this, and the C++ library
|
bits are already in place.
|
bits are already in place.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Development versions of the GNU "C" library, glibc 2.3 will allow
|
Development versions of the GNU "C" library, glibc 2.3 will allow
|
a more efficient, MT implementation of std::messages, and will
|
a more efficient, MT implementation of std::messages, and will
|
allow the removal of the _M_name_messages data member. If this is
|
allow the removal of the _M_name_messages data member. If this is
|
done, it will change the library ABI. The C++ parts to support
|
done, it will change the library ABI. The C++ parts to support
|
glibc 2.3 have already been coded, but are not in use: once this
|
glibc 2.3 have already been coded, but are not in use: once this
|
version of the "C" library is released, the marked parts of the
|
version of the "C" library is released, the marked parts of the
|
messages implementation can be switched over to the new "C"
|
messages implementation can be switched over to the new "C"
|
library functionality.
|
library functionality.
|
|
|
|
|
|
|
|
|
At some point in the near future, std::numpunct will probably use
|
At some point in the near future, std::numpunct will probably use
|
std::messages facilities to implement truename/falsename
|
std::messages facilities to implement truename/falsename
|
correctly. This is currently not done, but entries in
|
correctly. This is currently not done, but entries in
|
libstdc++.pot have already been made for "true" and "false" string
|
libstdc++.pot have already been made for "true" and "false" string
|
literals, so all that remains is the std::numpunct coding and the
|
literals, so all that remains is the std::numpunct coding and the
|
configure/make hassles to make the installed library search its
|
configure/make hassles to make the installed library search its
|
own catalog. Currently the libstdc++.mo catalog is only searched
|
own catalog. Currently the libstdc++.mo catalog is only searched
|
for the testsuite cases involving messages members.
|
for the testsuite cases involving messages members.
|
|
|
|
|
|
|
|
|
The following member functions:
|
The following member functions:
|
|
|
|
|
|
|
catalog
|
catalog
|
open(const basic_string<char>& __s, const locale& __loc) const
|
open(const basic_string<char>& __s, const locale& __loc) const
|
|
|
|
|
|
|
|
|
|
|
catalog
|
catalog
|
open(const basic_string<char>&, const locale&, const char*) const;
|
open(const basic_string<char>&, const locale&, const char*) const;
|
|
|
|
|
|
|
|
|
Don't actually return a "value less than 0 if no such catalog
|
Don't actually return a "value less than 0 if no such catalog
|
can be opened" as required by the standard in the "gnu"
|
can be opened" as required by the standard in the "gnu"
|
model. As of this writing, it is unknown how to query to see
|
model. As of this writing, it is unknown how to query to see
|
if a specified message catalog exists using the gettext
|
if a specified message catalog exists using the gettext
|
package.
|
package.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Bibliography
|
Bibliography
|
|
|
|
|
|
|
The GNU C Library
|
The GNU C Library
|
|
|
|
|
McGrath
|
McGrath
|
Roland
|
Roland
|
|
|
|
|
Drepper
|
Drepper
|
Ulrich
|
Ulrich
|
|
|
|
|
2007
|
2007
|
FSF
|
FSF
|
|
|
Chapters 6 Character Set Handling, and 7 Locales and Internationalization
|
Chapters 6 Character Set Handling, and 7 Locales and Internationalization
|
|
|
|
|
|
|
|
|
|
|
Correspondence
|
Correspondence
|
|
|
|
|
Drepper
|
Drepper
|
Ulrich
|
Ulrich
|
|
|
|
|
2002
|
2002
|
|
|
|
|
|
|
|
|
|
|
|
|
ISO/IEC 14882:1998 Programming languages - C++
|
ISO/IEC 14882:1998 Programming languages - C++
|
|
|
|
|
1998
|
1998
|
ISO
|
ISO
|
|
|
|
|
|
|
|
|
|
|
ISO/IEC 9899:1999 Programming languages - C
|
ISO/IEC 9899:1999 Programming languages - C
|
|
|
|
|
|
|
1999
|
1999
|
ISO
|
ISO
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
System Interface Definitions, Issue 7 (IEEE Std. 1003.1-2008)
|
System Interface Definitions, Issue 7 (IEEE Std. 1003.1-2008)
|
|
|
|
|
|
|
|
|
2008
|
2008
|
|
|
The Open Group/The Institute of Electrical and Electronics
|
The Open Group/The Institute of Electrical and Electronics
|
Engineers, Inc.
|
Engineers, Inc.
|
|
|
|
|
|
|
|
|
|
|
|
|
The C++ Programming Language, Special Edition
|
The C++ Programming Language, Special Edition
|
|
|
|
|
Stroustrup
|
Stroustrup
|
Bjarne
|
Bjarne
|
|
|
|
|
2000
|
2000
|
Addison Wesley, Inc.
|
Addison Wesley, Inc.
|
|
|
Appendix D
|
Appendix D
|
|
|
|
|
Addison Wesley
|
Addison Wesley
|
|
|
|
|
|
|
|
|
|
|
|
|
Standard C++ IOStreams and Locales
|
Standard C++ IOStreams and Locales
|
|
|
|
|
Advanced Programmer's Guide and Reference
|
Advanced Programmer's Guide and Reference
|
|
|
|
|
Langer
|
Langer
|
Angelika
|
Angelika
|
|
|
|
|
Kreft
|
Kreft
|
Klaus
|
Klaus
|
|
|
|
|
2000
|
2000
|
Addison Wesley Longman, Inc.
|
Addison Wesley Longman, Inc.
|
|
|
|
|
|
|
Addison Wesley Longman
|
Addison Wesley Longman
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
API Specifications, Java Platform
|
API Specifications, Java Platform
|
|
|
|
|
|
|
java.util.Properties, java.text.MessageFormat,
|
java.util.Properties, java.text.MessageFormat,
|
java.util.Locale, java.util.ResourceBundle
|
java.util.Locale, java.util.ResourceBundle
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GNU gettext tools, version 0.10.38, Native Language Support
|
GNU gettext tools, version 0.10.38, Native Language Support
|
Library and Tools.
|
Library and Tools.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|