From: Matt McCutchen Date: Sat, 17 Apr 2010 01:44:50 +0000 (+0000) Subject: Switch to boost::format for non-trivial substitution of values into X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=dd857bedb627c2e3f9da431d51619afb06a707e4;p=supertux.git Switch to boost::format for non-trivial substitution of values into user-visible strings. This makes it easier to translate the patterns. See the comment added to src/util/gettext.hpp . SVN-Revision: 6634 --- diff --git a/INSTALL b/INSTALL index 9d195eb91..9dac70635 100644 --- a/INSTALL +++ b/INSTALL @@ -64,7 +64,7 @@ REQUIREMENTS * GLEW http://glew.sourceforge.net/ -* Boost's smart_ptr headers +* Boost's smart_ptr and format headers http://www.boost.org/ Note: We tried to write our code clean, portable and platform neutral, so it diff --git a/src/supertux/levelintro.cpp b/src/supertux/levelintro.cpp index 98c56234e..bbd3f215f 100644 --- a/src/supertux/levelintro.cpp +++ b/src/supertux/levelintro.cpp @@ -26,6 +26,7 @@ #include "util/gettext.hpp" #include +#include LevelIntro::LevelIntro(const Level* level, const Statistics* best_level_statistics) : level(level), @@ -90,7 +91,7 @@ LevelIntro::draw(DrawingContext& context) std::string author = level->get_author(); if ((author != "") && (author != "SuperTux Team")) { - std::string author_text = std::string(_("contributed by ")) + author; + std::string author_text = str(boost::format(_("contributed by %s")) % author); context.draw_center_text(Resources::small_font, author_text, Vector(0, py), LAYER_FOREGROUND1, LevelIntro::author_color); py += static_cast(Resources::small_font->get_height()); } diff --git a/src/supertux/main.cpp b/src/supertux/main.cpp index 390eb207c..27314d488 100644 --- a/src/supertux/main.cpp +++ b/src/supertux/main.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include "supertux/main.hpp" @@ -227,8 +228,10 @@ Main::init_physfs(const char* argv0) void Main::print_usage(const char* argv0) { - std::cerr << _("Usage: ") << argv0 << _(" [OPTIONS] [LEVELFILE]\n\n") - << _("Options:\n" + std::cerr << boost::format(_( + "\n" + "Usage: %s [OPTIONS] [LEVELFILE]\n\n" + "Options:\n" " -f, --fullscreen Run in fullscreen mode\n" " -w, --window Run in window mode\n" " -g, --geometry WIDTHxHEIGHT Run SuperTux in given resolution\n" @@ -246,7 +249,9 @@ Main::print_usage(const char* argv0) " --record-demo FILE LEVEL Record a demo to FILE\n" " --play-demo FILE LEVEL Play a recorded demo\n" " -s, --debug-scripts Enable script debugger.\n" - "\n") + "\n" + )) + % argv0 << std::flush; } diff --git a/src/util/gettext.hpp b/src/util/gettext.hpp index 4405c9993..9c761ab25 100644 --- a/src/util/gettext.hpp +++ b/src/util/gettext.hpp @@ -22,6 +22,25 @@ #include "supertux/globals.hpp" +/* + * If you need to do a nontrivial substitution of values into a pattern, use + * boost::format rather than an ad-hoc concatenation. That way, translators can + * translate the format string as a whole (and even rearrange the values if + * necessary with "%1$s"-style codes) instead of multiple pieces. Patterns like + * "Label: %s" with only one string piece are a borderline case where + * boost::format is not really necessary. + * + * http://www.mihai-nita.net/article.php?artID=20060430a + * + * Bad: + * std::string msg = _("You collected ") + num + _(" coins"); + * std::cout << _("You collected ") << num << _(" coins"); + * Good: + * #include + * std::string msg = str(boost::format(_("You collected %d coins")) % num); + * std::cout << boost::format(_("You collected %d coins")) % num; + */ + static inline std::string _(const std::string& message) { if (dictionary_manager)