Skip to content

Commit

Permalink
Switch to boost::format for non-trivial substitution of values into
Browse files Browse the repository at this point in the history
user-visible strings.

This makes it easier to translate the patterns.  See the comment added
to src/util/gettext.hpp .

SVN-Revision: 6634
  • Loading branch information
mattmccutchen committed Apr 17, 2010
1 parent 820ab34 commit dd857be
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion INSTALL
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/supertux/levelintro.cpp
Expand Up @@ -26,6 +26,7 @@
#include "util/gettext.hpp"

#include <sstream>
#include <boost/format.hpp>

LevelIntro::LevelIntro(const Level* level, const Statistics* best_level_statistics) :
level(level),
Expand Down Expand Up @@ -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<int>(Resources::small_font->get_height());
}
Expand Down
11 changes: 8 additions & 3 deletions src/supertux/main.cpp
Expand Up @@ -22,6 +22,7 @@
#include <iostream>
#include <binreloc.h>
#include <tinygettext/log.hpp>
#include <boost/format.hpp>

#include "supertux/main.hpp"

Expand Down Expand Up @@ -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"
Expand All @@ -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;
}

Expand Down
19 changes: 19 additions & 0 deletions src/util/gettext.hpp
Expand Up @@ -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 <boost/format.hpp>
* 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)
Expand Down

0 comments on commit dd857be

Please sign in to comment.