If you wish to directly include the strings in the software, you could make use of the extern
keyword:
// someHeader.h
namespace messages
{
extern const std::string WelcomeOnBoard; // declaration
}
// someHeader.cpp
#include "myproject/someHeader.h"
namespace messages
{
const std::string WelcomeOnBoard = "Welcome on Board!"; // definition
}
The main advantage of namespaces is that you can split your messages into different files, to group them depending on some zone etc.
However, you usually don't do this like that.
Harcoding strings in the software causes issues of maintenance and internationalization.
- You wish to remove a comma or correct a mistake ? Recompile
- You wish to translate the game into Sweden because this girl your really want to impress is not very good in English ? Ouch... good luck.
Therefore, you usually use identifiers within the software, that are mapped to strings thanks to a framework. One famous such framework is gettext
The messages are tucked into a .po
file with a nice GUI to edit them and referred to with identifiers that are themselves sentences from within the software. For example:
printf (gettext ("The amount is %0" PRId64 "\n"), number);
The GUIs include ways to search for already existing messages and also include fuzzy translations features.