Complete C++ i18n gettext() hello world example
cat >hellogt.cxx <<EOF
// hellogt.cxx
#include <libintl.h>
#include <locale.h>
#include <iostream>
int main (){
setlocale(LC_ALL, "");
bindtextdomain("hellogt", ".");
textdomain( "hellogt");
std::cout << gettext("hello, world!") << std::endl;
}
EOF
g++ -o hellogt hellogt.cxx
xgettext --package-name hellogt --package-version 1.2 --default-domain hellogt --output hellogt.pot hellogt.cxx
msginit --no-translator --locale es_MX --output-file hellogt_spanish.po --input hellogt.pot
sed --in-place hellogt_spanish.po --expression='/"hello, world!"/,/#: / s/""/"hola mundo"/'
mkdir --parents ./es_MX.utf8/LC_MESSAGES
msgfmt --check --verbose --output-file ./es_MX.utf8/LC_MESSAGES/hellogt.mo hellogt_spanish.po
LANGUAGE=es_MX.utf8 ./hellogt
Here is a description of the files created by the above:
hellogt.cxx C++ source file
hellogt Executable image
hellogt.pot Extracted text from C++ source file (portable object template)
hellogt_spanish.po Modified text for Spanish with translations added (using sed)
es_MX.utf8/
LC_MESSAGES/
hellogt.mo Binary translated text for Spanish used at run-time
I18n C++ hello world with plurals
I'm not sure what you want. If it is slight modification of your example that give your wanted output, just replace the printf line by
printf(ngettext("Hello world with %d moon\n", "Hello world with %d moons\n", ii), ii);
but as it is a trivial modification of unwind's answer and the gettext documentation has the very similar example,
printf (ngettext ("%d file removed", "%d files removed", n), n);
I wonder if it is really what you wanted. If you want to use gettext with a more C++ syntax, you'll have to look for libraries like Boost::Format.
How to initialize a static char* with gettetxt() using local operating system environment?
You need to split gettext usage into two parts. First, you just mark the string with a macro, such as gettext_noop, so that xgettext will extract it. Then, when you refer to the the global variable, you wrap the access with the true gettext call.
See Special Cases in the gettext manual.
N.B. Your variable hws is not a static variable (no "static keyword"); it is a global variable.
Proper Hello World in C
I believe this is a standard Hello World program in C:
#include <stdio.h>
int main(void)
{
printf("Hello World\n");
return 0;
}
Related Topics
Decltype as a Return Type in Class Member Function
Capture Output of Spawned Process to String
When Do Compilers Inline C++ Code
"Constexpr If" VS "If" with Optimizations - Why Is "Constexpr" Needed
Compile Time Triggered Range Check for Std::Vector
Why Does A%B Produce Sigfpe When B Is Zero
How to Have Static Data Members in a Header-Only Library
Enumerating All Available Drive Letters in Windows
How to Directly Bind a Member Function to an Std::Function in Visual Studio 11
Disallowing Creation of the Temporary Objects
Why Does the Expression a = a + B - ( B = a ) Give a Sequence Point Warning in C++
Setting the Cursor Position in a Win32 Console Application
Get Index of a Tuple Element's Type
How to Store Arrays in an Stl List
C++ - Getusername() When Process Is Run as Administrator
What Is the Behavior of "Delete" with Stack Objects
Reading Line from Text File and Putting the Strings into a Vector