1. Write a message file
# greetings.msg
$MODULE greetings
$LANGUAGE en
$STRING ENABLE
$MESSAGE Welcome
@ user std::string const &
"Welcome, {$user}!"
[tr]
"Hoş geldin, {$user}!"
$MODULEnames the output:greetings.hhandgreetings.cpp.$LANGUAGE ensays the texts without a header are English.$STRING ENABLEasks for a string function per message.$MESSAGE Welcomehas one parameter,user, and a text in English and Turkish;{$user}puts the parameter into the text.
2. Generate
mstring greetings.msg
// greetings.hh (excerpt)
std::string WelcomeStr( std::string const & user, std::locale const &loc );
std::string WelcomeStr( std::string const & user );
3. Use it
#include "greetings.hh"
#include <iostream>
int main( )
{
std::cout << WelcomeStr( "Ada" ) << '\n'; // Welcome, Ada!
try
{
std::locale tr( "tr_TR.UTF-8" );
std::cout << WelcomeStr( "Ada", tr ) << '\n'; // Hoş geldin, Ada!
}
catch ( std::runtime_error const & ) // locale not installed
{
}
}
g++ -std=c++17 main.cpp greetings.cpp -o hello
WelcomeStr( 42 ) does not compile — the parameter is a string.
4. Let the build do it
In CMake, mstring_generate() runs mstring whenever the message file changes:
find_package(mstring REQUIRED)
mstring_generate(GREETINGS greetings.msg)
add_executable(hello main.cpp ${GREETINGS})
target_include_directories(hello PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
Next
- more languages and how one is chosen: Languages & Locales
- exceptions, fatal errors, syslog: Error & Exception Messages
- the whole language: Language

