Localizing option strings

#include "testoptions.h"

// ...

int main(int argc, char **argv)
{
    x::locale locale(x::locale::base::environment());

    x::messages msgcat(x::messages::create(locale, "testprog"));

    testoptions opts(msgcat);

    std::list<std::string> args(opts.parse(argc, argv, locale)->args);

The code generated by the optgen.xsl contains localizable strings. A localized output from the default --help and --usage options is produced by passing a message catalog to the option class constructor, and the locale object as the third parameter to parse().

The code generated by the optgen.xsl marks localizable strings by N_ and NN_. If using GNU gettext, add --keyword=N_ --keyword=NN_:1,2 to the XGETTEXT_OPTIONS setting in po/Makevars. The localizable strings are:

Using gettextmsg() with localizable strings

<descr>Specify up to 3 files</descr>

This literal string is localizable, but here's a better way:

<descr>
   <format>Specify up to %1% files</format>
   <param>nfiles</param>
</descr>

Instead of a literal string, the descr contains a format and one or more params. This example calls gettextmsg() to format a localizable string. format specifies the localized string passed as a first parameter to gettextmsg(), with any remaining param elements giving the additional parameters to gettextmsg.

The contents of param get inserted as is, into the generated code. Here, nfiles might be a member from the option parser object's superclass.

<descr>
   <format>
      <singular>Specify up to %1% file</singular>
      <plural>Specify up to %1% files</plural>
      <value>nfiles</value>
   </format>
   <param>nfiles</param>
</descr>

Instead of a literal string in the format, this is an example of GNU gettext's support for discrete singular and plural forms of localizable strings. format's singular and plural strings specify the singular and plural forms of a localizable string, selected by value. Note that value only selects the singular or the plural form of the localized string. Once selected, the localized string still has a parameter that gets replaces by the param.