Skip to content

Setting Up The Gettext Library For Qt

laurent22 edited this page Mar 28, 2013 · 1 revision

Introduction

Sample code on how to use Gettext with the Qt Framework

Details

Setting up the Gettext catalogue manager

In order to initialize the catalogue manager, the locale, catalogue name and directory must be provided. For instance, if we want to set the locale to traditional Chinese, and if the catalogues are named "catalogue.mo" and located in Data/Locales, we would use the following code:

#include <QtGettext.h>

QtGettext::instance()->setLocale("cn"); // Set the locale
QtGettext::instance()->setCatalogueName("catalogue"); // Set the name of the mo files
QtGettext::instance()->setCatalogueLocation("Data/Locales"); // Set the catalogue folder

QtGettext is now set to use the file in Data/Locales/cn_TW/catalogue.mo.

Also note that if this file is not available, QtGettext will try to load Data/Locales/cn/catalogue.mo (if available) as an alternative.

Marking strings for translation

To mark a string for translation, simply wrap it in a call to QtGettext::instance()->getTranslation("Some string to be translated").

Since this is rather long, a _() macro is also provided. That would shorten the code to just _("Some string to be translated")