API proposal: Glib::compose()



Hey all,

here's another API suggestion to ponder:  Glib::compose(), a small set
of overloaded utility functions to aid with message translation.  People
have been using custom-made variations of this for a while now.  If I
remember correctly, someone on this list even made a small library for
that purpose.  The following is taken verbatim from the source code of
regexxer:

        Glib::ustring compose_argv(const char* format, int argc,
                                   const Glib::ustring *const * argv);
        
        /*
         * The compose functions substitute placeholders in a format string with
         * the referenced arguments.  The template string should be in qt-format,
         * that is "%1", "%2", ..., "%9" are used as placeholders and "%%" denotes
         * a literal "%".  Substitutions may be reordered.
         *
         * Note that the format argument is of type const char* because the format
         * should generally not be computed.  It should be either a literal string
         * or the result of a call to Util::translate().
         */
        inline
        Glib::ustring compose(const char* format, const Glib::ustring& s1)
        {
          const Glib::ustring *const argv[] = { &s1 };
          return compose_argv(format, G_N_ELEMENTS(argv), argv);
        }
        
        inline
        Glib::ustring compose(const char* format, const Glib::ustring& s1,
                              const Glib::ustring& s2)
        {
          const Glib::ustring *const argv[] = { &s1, &s2 };
          return compose_argv(format, G_N_ELEMENTS(argv), argv);
        }
        
        inline
        Glib::ustring compose(const char* format, const Glib::ustring& s1,
                              const Glib::ustring& s2, const Glib::ustring& s3)
        {
          const Glib::ustring *const argv[] = { &s1, &s2, &s3 };
          return compose_argv(format, G_N_ELEMENTS(argv), argv);
        }

The implementation can be found here:
http://svn.gnome.org/viewcvs/regexxer/trunk/src/translation.cc?view=markup

You can't get any simpler.  My compose() functions do the bare minimum
that is necessary for proper use of gettext.  As only string arguments
are supported, compose() needs to be augmented by utility functions like
int_to_string() and similar.  The usage would look like this:

        message = Util::compose(_("Failed to save file \"%1\": %2"),
                                Glib::filename_display_basename(fullname),
                                error.what()));

Note that the qt-format used here for message IDs is recognized by
xgettext if invoked with the --qt flag.  So it's pretty standard.

So, what's the verdict?  Should this go into glibmm?  Would you prefer
something more sophisticated, similar to the compose mini-libraries
already floating around the net?

Happy composing,
--Daniel





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]