TinyGetText::Dictionary Class Reference

A simple dictionary class that mimics gettext() behaviour. More...

#include <tinygettext.hpp>

List of all members.

Public Member Functions

 Dictionary (const LanguageDef &language_, const std::string &charset="")
 Dictionary ()
std::string get_charset () const
 Return the charset used for this dictionary.
void set_charset (const std::string &charset)
 Set a charset for this dictionary, this will NOT convert stuff, it is for information only, you have to convert stuff yourself when you add it with add_translation().
void set_language (const LanguageDef &lang)
 Set the language that is used for this dictionary, this is mainly needed to evaluate plural forms.
std::string translate (const std::string &msgid, const std::string &msgid2, int num)
 Translate the string msgid to its correct plural form, based on the number of items given by num.
std::string translate (const std::string &msgid)
 Translate the string msgid.
const char * translate (const char *msgid)
 Translate the string msgid.
void add_translation (const std::string &msgid, const std::string &msgid2, const std::map< int, std::string > &msgstrs)
 Add a translation from msgid to msgstr to the dictionary, where msgid is the singular form of the message, msgid2 the plural form and msgstrs a table of translations.
void add_translation (const std::string &msgid, const std::string &msgstr)
 Add a translation from msgid to msgstr to the dictionary.

Private Types

typedef std::map< std::string,
std::string > 
Entries
typedef std::map< std::string,
std::map< int, std::string > > 
PluralEntries

Private Attributes

Entries entries
PluralEntries plural_entries
LanguageDef language
std::string charset


Detailed Description

A simple dictionary class that mimics gettext() behaviour.

Each Dictionary only works for a single language, for managing multiple languages and .po files at once use the DictionaryManager.

Definition at line 46 of file tinygettext.hpp.


Member Typedef Documentation

typedef std::map<std::string, std::string> TinyGetText::Dictionary::Entries [private]

Definition at line 49 of file tinygettext.hpp.

typedef std::map<std::string, std::map<int, std::string> > TinyGetText::Dictionary::PluralEntries [private]

Definition at line 52 of file tinygettext.hpp.


Constructor & Destructor Documentation

TinyGetText::Dictionary::Dictionary ( const LanguageDef language_,
const std::string &  charset = "" 
)

Definition at line 428 of file tinygettext.cpp.

00429   : language(language_), charset(charset_)
00430 {
00431 }

TinyGetText::Dictionary::Dictionary (  ) 

Definition at line 433 of file tinygettext.cpp.

00434   : language(lang_en)
00435 {
00436 }


Member Function Documentation

std::string TinyGetText::Dictionary::get_charset (  )  const

Return the charset used for this dictionary.

Definition at line 439 of file tinygettext.cpp.

References charset.

Referenced by TinyGetText::POFileReader::parse_header().

00440 {
00441   return charset;
00442 }

void TinyGetText::Dictionary::set_charset ( const std::string &  charset  ) 

Set a charset for this dictionary, this will NOT convert stuff, it is for information only, you have to convert stuff yourself when you add it with add_translation().

Definition at line 445 of file tinygettext.cpp.

References charset.

Referenced by TinyGetText::DictionaryManager::get_dictionary(), and TinyGetText::POFileReader::parse_header().

00446 {
00447   charset = charset_;
00448 }

void TinyGetText::Dictionary::set_language ( const LanguageDef lang  ) 

Set the language that is used for this dictionary, this is mainly needed to evaluate plural forms.

Definition at line 451 of file tinygettext.cpp.

References language.

Referenced by TinyGetText::DictionaryManager::get_dictionary().

00452 {
00453   language = lang;
00454 }

std::string TinyGetText::Dictionary::translate ( const std::string &  msgid,
const std::string &  msgid2,
int  num 
)

Translate the string msgid to its correct plural form, based on the number of items given by num.

msgid2 is msgid in plural form.

Definition at line 457 of file tinygettext.cpp.

References language, log_debug, log_warning, TinyGetText::LanguageDef::plural, TinyGetText::plural2_1(), and plural_entries.

00458 {
00459   PluralEntries::iterator i = plural_entries.find(msgid);
00460   std::map<int, std::string>& msgstrs = i->second;
00461 
00462   if (i != plural_entries.end() && !msgstrs.empty())
00463     {
00464       int g = language.plural(num);
00465       std::map<int, std::string>::iterator j = msgstrs.find(g);
00466       if (j != msgstrs.end())
00467         {
00468           return j->second;
00469         }
00470       else
00471         {
00472           // Return the first translation, in case we can't translate the specific number
00473           return msgstrs.begin()->second;
00474         }
00475     }
00476   else
00477     {
00478 #ifdef TRANSLATION_DEBUG
00479       log_warning << "Couldn't translate: " << msgid << std::endl;
00480       log_warning << "Candidates: " << std::endl;
00481       for (PluralEntries::iterator i = plural_entries.begin(); i != plural_entries.end(); ++i)
00482         log_debug << "'" << i->first << "'" << std::endl;
00483 #endif
00484 
00485       if (plural2_1(num)) // default to english rules
00486         return msgid2;
00487       else
00488         return msgid;
00489     }
00490 }

std::string TinyGetText::Dictionary::translate ( const std::string &  msgid  ) 

Translate the string msgid.

Definition at line 510 of file tinygettext.cpp.

References entries, and log_warning.

00511 {
00512   Entries::iterator i = entries.find(msgid);
00513   if (i != entries.end() && !i->second.empty())
00514     {
00515       return i->second;
00516     }
00517   else
00518     {
00519 #ifdef TRANSLATION_DEBUG
00520       log_warning << "Couldn't translate: " << msgid << std::endl;
00521 #endif
00522       return msgid;
00523     }
00524 }

const char * TinyGetText::Dictionary::translate ( const char *  msgid  ) 

Translate the string msgid.

Definition at line 493 of file tinygettext.cpp.

References entries, and log_warning.

00494 {
00495   Entries::iterator i = entries.find(msgid);
00496   if (i != entries.end() && !i->second.empty())
00497     {
00498       return i->second.c_str();
00499     }
00500   else
00501     {
00502 #ifdef TRANSLATION_DEBUG
00503       log_warning << "Couldn't translate: " << msgid << std::endl;
00504 #endif
00505       return msgid;
00506     }
00507 }

void TinyGetText::Dictionary::add_translation ( const std::string &  msgid,
const std::string &  msgid2,
const std::map< int, std::string > &  msgstrs 
)

Add a translation from msgid to msgstr to the dictionary, where msgid is the singular form of the message, msgid2 the plural form and msgstrs a table of translations.

The right translation will be calculated based on the num argument to translate().

Definition at line 527 of file tinygettext.cpp.

References plural_entries.

Referenced by TinyGetText::POFileReader::tokenize_po().

00529 {
00530   // Do we need msgid2 for anything? its after all supplied to the
00531   // translate call, so we just throw it away
00532   plural_entries[msgid] = msgstrs;
00533 }

void TinyGetText::Dictionary::add_translation ( const std::string &  msgid,
const std::string &  msgstr 
)

Add a translation from msgid to msgstr to the dictionary.

Definition at line 536 of file tinygettext.cpp.

References entries.

00537 {
00538   entries[msgid] = msgstr;
00539 }


Member Data Documentation

Entries TinyGetText::Dictionary::entries [private]

Definition at line 50 of file tinygettext.hpp.

Referenced by add_translation(), and translate().

PluralEntries TinyGetText::Dictionary::plural_entries [private]

Definition at line 53 of file tinygettext.hpp.

Referenced by add_translation(), and translate().

LanguageDef TinyGetText::Dictionary::language [private]

Definition at line 55 of file tinygettext.hpp.

Referenced by set_language(), and translate().

std::string TinyGetText::Dictionary::charset [private]

Definition at line 56 of file tinygettext.hpp.

Referenced by get_charset(), and set_charset().


The documentation for this class was generated from the following files:
Generated on Mon Jun 9 03:38:41 2014 for SuperTux by  doxygen 1.5.1