TinyGetText::DictionaryManager Class Reference

Manager class for dictionaries, you give it a bunch of directories with .po files and it will then automatically load the right file on demand depending on which language was set. More...

#include <tinygettext.hpp>

List of all members.

Public Member Functions

 DictionaryManager ()
Dictionaryget_dictionary ()
 Return the currently active dictionary, if none is set, an empty dictionary is returned.
Dictionaryget_dictionary (const std::string &langspec)
 Get dictionary for lang.
void set_language (const std::string &langspec)
 Set a language based on a four? letter country code.
const std::string & get_language () const
 returns the (normalized) country code of the currently used language
void set_charset (const std::string &charset)
 Set a charset that will be set on the returned dictionaries.
void set_language_alias (const std::string &alias, const std::string &lang)
 Define an alias for a language.
void add_directory (const std::string &pathname)
 Add a directory to the search path for dictionaries.
std::set< std::string > get_languages ()
 Return a set of the available languages in their country code.

Private Types

typedef std::map< std::string,
Dictionary
Dictionaries
typedef std::vector< std::string > SearchPath
typedef std::map< std::string,
std::string > 
Aliases

Private Member Functions

void parseLocaleAliases ()
std::string get_language_from_spec (const std::string &spec)
 returns the language part in a language spec (like de_DE.UTF-8 -> de)

Private Attributes

Dictionaries dictionaries
SearchPath search_path
Aliases language_aliases
std::string charset
std::string language
Dictionarycurrent_dict
Dictionary empty_dict


Detailed Description

Manager class for dictionaries, you give it a bunch of directories with .po files and it will then automatically load the right file on demand depending on which language was set.

Definition at line 101 of file tinygettext.hpp.


Member Typedef Documentation

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

Definition at line 104 of file tinygettext.hpp.

typedef std::vector<std::string> TinyGetText::DictionaryManager::SearchPath [private]

Definition at line 106 of file tinygettext.hpp.

typedef std::map<std::string, std::string> TinyGetText::DictionaryManager::Aliases [private]

Definition at line 108 of file tinygettext.hpp.


Constructor & Destructor Documentation

TinyGetText::DictionaryManager::DictionaryManager (  ) 

Definition at line 205 of file tinygettext.cpp.

References FL_Locale::country, FL_FindLocale(), FL_FreeLocale(), FL_MESSAGES, FL_Locale::lang, parseLocaleAliases(), and set_language().

00206   : current_dict(&empty_dict)
00207 {
00208   parseLocaleAliases();
00209   // Environment variable SUPERTUX_LANG overrides language settings.
00210   const char* lang = getenv( "SUPERTUX_LANG" );
00211   if( lang ){
00212     set_language( lang );
00213     return;
00214   }
00215   // use findlocale to setup language
00216   FL_Locale *locale;
00217   FL_FindLocale( &locale, FL_MESSAGES );
00218   if(locale->lang) {
00219     if (locale->country) {
00220       set_language( std::string(locale->lang)+"_"+std::string(locale->country) );
00221     } else {
00222       set_language( std::string(locale->lang) );
00223     }
00224   }
00225   FL_FreeLocale( &locale );
00226 }


Member Function Documentation

Dictionary& TinyGetText::DictionaryManager::get_dictionary (  )  [inline]

Return the currently active dictionary, if none is set, an empty dictionary is returned.

Definition at line 120 of file tinygettext.hpp.

References current_dict.

Referenced by set_language().

00121   { return *current_dict; }

Dictionary & TinyGetText::DictionaryManager::get_dictionary ( const std::string &  langspec  ) 

Get dictionary for lang.

Definition at line 265 of file tinygettext.cpp.

References charset, dictionaries, TinyGetText::get_language_def(), get_language_from_spec(), log_warning, TinyGetText::read_po_file(), search_path, TinyGetText::Dictionary::set_charset(), and TinyGetText::Dictionary::set_language().

00266 {
00267 
00268   //log_debug << "Dictionary for language \"" << spec << "\" requested" << std::endl;
00269 
00270   std::string lang = get_language_from_spec(spec);
00271 
00272   //log_debug << "...normalized as \"" << lang << "\"" << std::endl;
00273 
00274   Dictionaries::iterator i = dictionaries.find(get_language_from_spec(lang));
00275   if (i != dictionaries.end())
00276     {
00277       return i->second;
00278     }
00279   else // Dictionary for languages lang isn't loaded, so we load it
00280     {
00281       //log_debug << "get_dictionary: " << lang << std::endl;
00282       Dictionary& dict = dictionaries[lang];
00283 
00284       dict.set_language(get_language_def(lang));
00285       if(charset != "")
00286         dict.set_charset(charset);
00287 
00288       for (SearchPath::iterator p = search_path.begin(); p != search_path.end(); ++p)
00289         {
00290           char** files = PHYSFS_enumerateFiles(p->c_str());
00291           if(!files)
00292             {
00293               log_warning << "Error: enumerateFiles() failed on " << *p << std::endl;
00294             }
00295           else
00296             {
00297               for(const char* const* filename = files;
00298                       *filename != 0; filename++) {
00299 
00300                 // check if filename matches requested language
00301                 std::string fname = std::string(*filename);
00302                 std::string load_from_file = "";
00303                 if(fname == lang + ".po") {
00304                   load_from_file = fname;
00305                 } else {
00306                   std::string::size_type s = lang.find("_");
00307                   if(s != std::string::npos) {
00308                     std::string lang_short = std::string(lang, 0, s);
00309                     if (fname == lang_short + ".po") {
00310                       load_from_file = lang_short;
00311                     }
00312                   }
00313                 }
00314 
00315                 // if it matched, load dictionary
00316                 if (load_from_file != "") {
00317                   //log_debug << "Loading dictionary for language \"" << lang << "\" from \"" << filename << "\"" << std::endl;
00318                   std::string pofile = *p + "/" + *filename;
00319                   try {
00320                       IFileStream in(pofile);
00321                       read_po_file(dict, in);
00322                   } catch(std::exception& e) {
00323                       log_warning << "Error: Failure file opening: " << pofile << std::endl;
00324                       log_warning << e.what() << "" << std::endl;
00325                   }
00326                 }
00327 
00328               }
00329               PHYSFS_freeList(files);
00330             }
00331         }
00332 
00333       return dict;
00334     }
00335 }

void TinyGetText::DictionaryManager::set_language ( const std::string &  langspec  ) 

Set a language based on a four? letter country code.

Definition at line 364 of file tinygettext.cpp.

References current_dict, get_dictionary(), get_language_from_spec(), and language.

Referenced by add_directory(), DictionaryManager(), and set_charset().

00365 {
00366   //log_debug << "set_language \"" << lang << "\"" << std::endl;
00367   language = get_language_from_spec(lang);
00368   //log_debug << "==> \"" << language << "\"" << std::endl;
00369   current_dict = & (get_dictionary(language));
00370 }

const std::string & TinyGetText::DictionaryManager::get_language (  )  const

returns the (normalized) country code of the currently used language

Definition at line 373 of file tinygettext.cpp.

References language.

00374 {
00375   return language;
00376 }

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

Set a charset that will be set on the returned dictionaries.

Definition at line 379 of file tinygettext.cpp.

References dictionaries, language, and set_language().

00380 {
00381   dictionaries.clear(); // changing charset invalidates cache
00382   this->charset = charset;
00383   set_language(language);
00384 }

void TinyGetText::DictionaryManager::set_language_alias ( const std::string &  alias,
const std::string &  lang 
)

Define an alias for a language.

Definition at line 387 of file tinygettext.cpp.

References language_aliases.

Referenced by parseLocaleAliases().

00389 {
00390   language_aliases.insert(std::make_pair(alias, language));
00391 }

void TinyGetText::DictionaryManager::add_directory ( const std::string &  pathname  ) 

Add a directory to the search path for dictionaries.

Definition at line 419 of file tinygettext.cpp.

References dictionaries, language, search_path, and set_language().

00420 {
00421   dictionaries.clear(); // adding directories invalidates cache
00422   search_path.push_back(pathname);
00423   set_language(language);
00424 }

std::set< std::string > TinyGetText::DictionaryManager::get_languages (  ) 

Return a set of the available languages in their country code.

Definition at line 338 of file tinygettext.cpp.

References TinyGetText::has_suffix(), log_warning, and search_path.

00339 {
00340   std::set<std::string> languages;
00341 
00342   for (SearchPath::iterator p = search_path.begin(); p != search_path.end(); ++p)
00343     {
00344       char** files = PHYSFS_enumerateFiles(p->c_str());
00345       if (!files)
00346         {
00347           log_warning << "Error: opendir() failed on " << *p << std::endl;
00348         }
00349       else
00350         {
00351           for(const char* const* file = files; *file != 0; file++) {
00352               if(has_suffix(*file, ".po")) {
00353                   std::string filename = *file;
00354                   languages.insert(filename.substr(0, filename.length()-3));
00355               }
00356           }
00357           PHYSFS_freeList(files);
00358         }
00359     }
00360   return languages;
00361 }

void TinyGetText::DictionaryManager::parseLocaleAliases (  )  [private]

Definition at line 229 of file tinygettext.cpp.

References language, and set_language_alias().

Referenced by DictionaryManager().

00230 {
00231   // try to parse language alias list
00232   std::ifstream in("/usr/share/locale/locale.alias");
00233 
00234   char c = ' ';
00235   while(in.good() && !in.eof()) {
00236     while(isspace(static_cast<unsigned char>(c)) && !in.eof())
00237       in.get(c);
00238 
00239     if(c == '#') { // skip comments
00240       while(c != '\n' && !in.eof())
00241         in.get(c);
00242       continue;
00243     }
00244 
00245     std::string alias;
00246     while(!isspace(static_cast<unsigned char>(c)) && !in.eof()) {
00247       alias += c;
00248       in.get(c);
00249     }
00250     while(isspace(static_cast<unsigned char>(c)) && !in.eof())
00251       in.get(c);
00252     std::string language;
00253     while(!isspace(static_cast<unsigned char>(c)) && !in.eof()) {
00254       language += c;
00255       in.get(c);
00256     }
00257 
00258     if(in.eof())
00259       break;
00260     set_language_alias(alias, language);
00261   }
00262 }

std::string TinyGetText::DictionaryManager::get_language_from_spec ( const std::string &  spec  )  [private]

returns the language part in a language spec (like de_DE.UTF-8 -> de)

Definition at line 394 of file tinygettext.cpp.

References language_aliases.

Referenced by get_dictionary(), and set_language().

00395 {
00396   std::string lang = spec;
00397   Aliases::iterator i = language_aliases.find(lang);
00398   if(i != language_aliases.end()) {
00399     lang = i->second;
00400   }
00401 
00402   std::string::size_type s = lang.find(".");
00403   if(s != std::string::npos) {
00404     lang = std::string(lang, 0, s);
00405   }
00406 
00407   s = lang.find("_");
00408   if(s == std::string::npos) {
00409     std::string lang_big = lang;
00410     std::transform (lang_big.begin(), lang_big.end(), lang_big.begin(), toupper);
00411     lang += "_" + lang_big;
00412   }
00413 
00414   return lang;
00415 
00416 }


Member Data Documentation

Dictionaries TinyGetText::DictionaryManager::dictionaries [private]

Definition at line 105 of file tinygettext.hpp.

Referenced by add_directory(), get_dictionary(), and set_charset().

SearchPath TinyGetText::DictionaryManager::search_path [private]

Definition at line 107 of file tinygettext.hpp.

Referenced by add_directory(), get_dictionary(), and get_languages().

Aliases TinyGetText::DictionaryManager::language_aliases [private]

Definition at line 109 of file tinygettext.hpp.

Referenced by get_language_from_spec(), and set_language_alias().

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

Definition at line 110 of file tinygettext.hpp.

Referenced by get_dictionary().

std::string TinyGetText::DictionaryManager::language [private]

Definition at line 111 of file tinygettext.hpp.

Referenced by add_directory(), get_language(), parseLocaleAliases(), set_charset(), and set_language().

Dictionary* TinyGetText::DictionaryManager::current_dict [private]

Definition at line 112 of file tinygettext.hpp.

Referenced by get_dictionary(), and set_language().

Dictionary TinyGetText::DictionaryManager::empty_dict [private]

Definition at line 113 of file tinygettext.hpp.


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