Add Localisation Support
Posted: Tue Mar 27, 2018 4:15 am
Adding Localisation support to your application isn't to hard on Linux systems. The tool of choice is gettext, which is installed on default on nearly all Linux distributions.
At first you need to include the following headers:
next step is to define a macro to reduce the needed work.
Now you need to init the gettext system in your main call like this:
were the macro GETTEXT_PACKAGE is your application name, you could define it in your config.h file, or put it in cleartext here.
Well, that's all, you've added localisation support in your app already.
But, that's just the support, you could build and run your app, but no magic translation will happen. A bit more work is needed.
You need to define in your source, which strings may be translated, to do so, we've defined the MACRO on top.
Now, put the strings you would translate in the clamps _() like this:
Do that for all strings you would add translation support for.
When done, you could produce the .pot file for your app with the following command:
here replace $(NAME) and $(VER) with your app name and version.
You may need to add the /po directory beforehand.
now you could generate the .po from the .pot file which then could be used to hold the translation.
This command will produce a po file for your locale settings, which you could open with, for example Gtranslator.
You properly wont to automate the po file generation so that supporters could easily generate the po file for there language, so here are some targets for your makefile, you could easily adapt them for your build system, if you use a other one:
EDIT// remove utf8 suffix from po file name for better integration.
the po target must be declared as .PHONY
with this targets you could produce the po file by:
or, produce it for a other language then your locale, use
check for available locales with
At least you need to add it to your installation target, you could do so by adding
where you clearly needs to define SHARE_DIR
That's it, now translation teams or supporters could easily add there language support to your application.
https://github.com/brummer10/jalv_selec ... ISATION.md
At first you need to include the following headers:
Code: Select all
#include <libintl.h>
#include <locale.h>
Code: Select all
#define _(S) gettext(S)
Code: Select all
int main (int argc , char ** argv) {
bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
textdomain(GETTEXT_PACKAGE);
Well, that's all, you've added localisation support in your app already.
But, that's just the support, you could build and run your app, but no magic translation will happen. A bit more work is needed.
You need to define in your source, which strings may be translated, to do so, we've defined the MACRO on top.
Now, put the strings you would translate in the clamps _() like this:
Code: Select all
set_title(_("This string will be to translate"));
When done, you could produce the .pot file for your app with the following command:
Code: Select all
xgettext --keyword=_ --language=C++ --add-comments --sort-output --package-name=$(NAME) --package-version=$(VER) -o po/$(NAME).pot sourcefilename.cpp sourcefilename1.cpp etc.cpp
You may need to add the /po directory beforehand.
now you could generate the .po from the .pot file which then could be used to hold the translation.
Code: Select all
msginit --input=po/$(NAME).pot --locale=$(LANG) --output=po/$(LANG).po
You properly wont to automate the po file generation so that supporters could easily generate the po file for there language, so here are some targets for your makefile, you could easily adapt them for your build system, if you use a other one:
Code: Select all
#@localisation
MSGLANGS=$(notdir $(wildcard po/*po))
MSGOBJS=$(addprefix locale/,$(MSGLANGS:.po=/LC_MESSAGES/$(NAME).mo))
gettext: $(MSGOBJS)
locale/%/LC_MESSAGES/$(NAME).mo: po/%.po
mkdir -p $(dir $@)
msgfmt -c -o $@ po/$*.po
updatepot:
xgettext --keyword=_ --language=C++ --add-comments --sort-output --package-name=$(NAME) --package-version=$(VER) -o po/$(NAME).pot $(NAME).cpp
for POFILE in $(MSGLANGS) ; do msgmerge --update po/$$POFILE po/$(NAME).pot ; done
po:
msginit --input=po/$(NAME).pot --locale=$(LANG) --output=po/$(basename $(LANG)).po
the po target must be declared as .PHONY
with this targets you could produce the po file by:
Code: Select all
make po
Code: Select all
make po LANG=your_choice
Code: Select all
locale -a
Code: Select all
cp --parents $(MSGOBJS) $(DESTDIR)$(SHARE_DIR)
That's it, now translation teams or supporters could easily add there language support to your application.
https://github.com/brummer10/jalv_selec ... ISATION.md