te::core::Translator Class Reference

This singleton is designed to deal with multi-language text translation in TerraLib. More...

#include <Translator.h>

Public Member Functions

void addTextDomain (const std::string &textDomain, const std::string &dir)
 It adds a new text domain (text catalog). More...
 
bool exist (const std::string &textDomain)
 It returns true if the text domain (text catalog) exists and false otherwise. More...
 
void setLocale (const std::string &locale)
 It sets the locale for the Translator. More...
 
std::string translate (const std::string &message)
 It tries to translate the specified text string. More...
 
std::string translate (const char *message)
 It tries to translate the specified text string. More...
 
std::string translate (const std::string &msg1, const std::string &msg2, unsigned int n)
 It tries to translate the specified text string accounting for plural forms. More...
 
std::string translate (const char *msg1, const char *msg2, unsigned int n)
 It tries to translate the specified text string accounting for plural forms. More...
 

Static Public Member Functions

static Translatorinstance ()
 

Private Member Functions

Translatoroperator= (Translator const &)
 Singleton copy assignment operator must be private or protected. More...
 
 Translator ()
 Singleton constructor must be private or protected. More...
 
 Translator (Translator const &)
 Singleton copy constructor must be private or protected. More...
 
 ~Translator ()
 Singleton destructor must be private or protected. More...
 

Private Attributes

std::string m_locale
 If not empty, it is the current locale. More...
 
std::map< std::string, std::vector< std::string > > m_textDomainMap
 A vector from text domains to base directory for the message catalog. More...
 

Detailed Description

This singleton is designed to deal with multi-language text translation in TerraLib.

The Translator job is to manage "Internationalization" of the TerraLib messages. This class is all about Native Language Support. For each string you want to have a translation, you have to use the special macro TR_TR("string") or TE_TR_PLURAL("string1","string2", int). This macro does nothing with your code; its job is just to mark the code fragment that you want to translate and it does all the job of calling the translation for you. The translations are created with the GNU Gettext Utilities.

We provide a cmake module that searchs for all the necessary GNU Gettext tools, and adds a macro that creates the files for your translations. This macro searchs in a given directory for all ".cpp" files and for the given keywords inside the found files, then automatically generates the ".po", ".pot" and ".mo" files when you build your project. The macro implementation can be found in the file build/cmake/modules/FindGettext.cmake.

...
TE_ADD_TEXT_DOMAIN("terralib_mod_core");
...
std::string msg = TE_TR("My Message");
...
or
...
std::cout << TE_TR_PLURAL("One message", "Two messages", 1);
...

Note that all marked messages will be part of a .pot file from where you can create several po documents, one for each idiom. These .po files can be used to generate a .mo binary file with the translation.

Warning
It is supposed that PO files are UTF-8 encoded.

Definition at line 86 of file Translator.h.

Constructor & Destructor Documentation

te::core::Translator::Translator ( )
inlineprivate

Singleton constructor must be private or protected.

Definition at line 186 of file Translator.h.

te::core::Translator::~Translator ( )
inlineprivate

Singleton destructor must be private or protected.

Definition at line 189 of file Translator.h.

te::core::Translator::Translator ( Translator const &  )
private

Singleton copy constructor must be private or protected.

Member Function Documentation

void te::core::Translator::addTextDomain ( const std::string &  textDomain,
const std::string &  dir 
)

It adds a new text domain (text catalog).

Parameters
textDomainA given message domain (just a name). A text domain is the name of the catalog used to translate the message.
dirWhere the text domain is located.
Exceptions
ExceptionIf the text domain already exists it raises an exception. If you are not sure about the existence of a text domain, use the exist() method.
bool te::core::Translator::exist ( const std::string &  textDomain)

It returns true if the text domain (text catalog) exists and false otherwise.

Parameters
textDomainA given message domain (just a name).
static Translator& te::core::Translator::instance ( )
static
Translator& te::core::Translator::operator= ( Translator const &  )
private

Singleton copy assignment operator must be private or protected.

void te::core::Translator::setLocale ( const std::string &  locale)

It sets the locale for the Translator.

Parameters
localeA string of the new locale.
std::string te::core::Translator::translate ( const std::string &  message)

It tries to translate the specified text string.

If no translation is available it will return the original message (always in English).

Parameters
messageThe text to be translated.
Returns
A string of the translated message. You must not delete the memory pointed by the returned pointer.
Note
The returned message is UTF-8 encoded.
std::string te::core::Translator::translate ( const char *  message)

It tries to translate the specified text string.

If no translation is available it will return the original message (always in English).

Parameters
messageThe text to be translated.
Returns
A string of the translated message. You must not delete the memory pointed by the returned pointer.
Note
The returned message is UTF-8 encoded.
std::string te::core::Translator::translate ( const std::string &  msg1,
const std::string &  msg2,
unsigned int  n 
)

It tries to translate the specified text string accounting for plural forms.

If no translation is available it will return the original message (always in English).

Parameters
msg1The singular form of the text to be translated.
msg2The plural form of the text to be translated.
nThis parameter is used to determine the plural form.
Returns
A string of the translated message. You must not delete the memory pointed by the returned pointer.
Note
The returned message is UTF-8 encoded.
std::string te::core::Translator::translate ( const char *  msg1,
const char *  msg2,
unsigned int  n 
)

It tries to translate the specified text string accounting for plural forms.

If no translation is available it will return the original message (always in English).

Parameters
msg1The singular form of the text to be translated.
msg2The plural form of the text to be translated.
nThis parameter is used to determine the plural form.
Returns
A string of the translated message. You must not delete the memory pointed by the returned pointer.
Note
The returned message is UTF-8 encoded.

Member Data Documentation

std::string te::core::Translator::m_locale
private

If not empty, it is the current locale.

Definition at line 199 of file Translator.h.

std::map<std::string, std::vector<std::string> > te::core::Translator::m_textDomainMap
private

A vector from text domains to base directory for the message catalog.

Definition at line 200 of file Translator.h.


The documentation for this class was generated from the following file: