LocaleMgr Class Reference

#include <localemgr.h>

Collaboration diagram for LocaleMgr:
Collaboration graph
[legend]

List of all members.

Public Member Functions

virtual StringList getAvailableLocales ()
virtual const char * getDefaultLocaleName ()
virtual SWLocalegetLocale (const char *name)
virtual void loadConfigDir (const char *ipath)
 LocaleMgr (const char *iConfigPath=0)
virtual void setDefaultLocaleName (const char *name)
virtual const char * translate (const char *text, const char *localeName=0)
virtual ~LocaleMgr ()

Static Public Member Functions

static LocaleMgrgetSystemLocaleMgr ()
static void setSystemLocaleMgr (LocaleMgr *newLocaleMgr)

Protected Attributes

LocaleMaplocales

Static Protected Attributes

static LocaleMgrsystemLocaleMgr = 0

Private Member Functions

void deleteLocales ()
 LocaleMgr (const LocaleMgr &)

Private Attributes

char * defaultLocaleName

Friends

class __staticsystemLocaleMgr

Detailed Description

The LocaleMgr class handles all the different locales of Sword. It provides functions to get a list of all available locales, to get the default locale name and to get it. The other functions are not interesting for frontend programmers.

To get the default locale name use

See also:
getDefaultLocaleName To set the default locale name use
setDefaultLocaleName To get the locale for a language name use
getLocale To get a list of availble locales use
getAvailableLocales

Definition at line 49 of file localemgr.h.


Constructor & Destructor Documentation

LocaleMgr::LocaleMgr ( const LocaleMgr  )  [private]
LocaleMgr::LocaleMgr ( const char *  iConfigPath = 0  ) 

Default constructor of LocaleMgr You do normally not need this constructor, use LocaleMgr::getSystemLocaleMgr() instead.

Definition at line 69 of file localemgr.cpp.

00069                                             {
00070     locales = new LocaleMap();
00071     char *prefixPath = 0;
00072     char *configPath = 0;
00073     SWConfig *sysConf = 0;
00074     char configType = 0;
00075     SWBuf path;
00076     std::list<SWBuf> augPaths;
00077     ConfigEntMap::iterator entry;
00078     
00079     defaultLocaleName = 0;
00080     
00081     if (!iConfigPath) {
00082         SWLog::getSystemLog()->logDebug("LOOKING UP LOCALE DIRECTORY...");
00083         SWMgr::findConfig(&configType, &prefixPath, &configPath, &augPaths, &sysConf);
00084         if (sysConf) {
00085             if ((entry = sysConf->Sections["Install"].find("LocalePath")) != sysConf->Sections["Install"].end()) {
00086                 configType = 9; // our own
00087                 stdstr(&prefixPath, (char *)entry->second.c_str());
00088                 SWLog::getSystemLog()->logDebug("LocalePath provided in sysConfig.");
00089             }
00090         }
00091         SWLog::getSystemLog()->logDebug("LOOKING UP LOCALE DIRECTORY COMPLETE.");
00092     }
00093     else {
00094         loadConfigDir(iConfigPath);
00095     }
00096     
00097     if (prefixPath) {
00098         switch (configType) {
00099         case 2:
00100             int i;
00101             for (i = strlen(configPath)-1; ((i) && (configPath[i] != '/') && (configPath[i] != '\\')); i--);
00102             configPath[i] = 0;
00103             path = configPath;
00104             path += "/";
00105             break;
00106         default:
00107             path = prefixPath;
00108             if ((prefixPath[strlen(prefixPath)-1] != '\\') && (prefixPath[strlen(prefixPath)-1] != '/'))
00109                 path += "/";
00110 
00111             break;
00112         }
00113         if (FileMgr::existsDir(path.c_str(), "locales.d")) {
00114             path += "locales.d";
00115             loadConfigDir(path.c_str());
00116         }
00117     }
00118     
00119     if (augPaths.size() && configType != 9) { //load locale files from all augmented paths
00120         std::list<SWBuf>::iterator it = augPaths.begin();
00121         std::list<SWBuf>::iterator end = augPaths.end();
00122         
00123         for (;it != end; ++it) {
00124             if (FileMgr::existsDir((*it).c_str(), "locales.d")) {
00125                 SWBuf path = (*it) + "locales.d";
00126                 loadConfigDir(path.c_str());
00127             }
00128         }
00129     }
00130 
00131     // Locales will be invalidated if you change the StringMgr
00132     // So only use the default hardcoded locale and let the
00133     // frontends change the locale if they want
00134     stdstr(&defaultLocaleName, SWLocale::DEFAULT_LOCALE_NAME);
00135 
00136     if (prefixPath)
00137         delete [] prefixPath;
00138 
00139     if (configPath)
00140         delete [] configPath;
00141 
00142     if (sysConf)
00143         delete sysConf;
00144 }

LocaleMgr::~LocaleMgr (  )  [virtual]

Default destructor of LocaleMgr

Definition at line 147 of file localemgr.cpp.

00147                       {
00148     if (defaultLocaleName)
00149         delete [] defaultLocaleName;
00150         deleteLocales();
00151     delete locales;
00152 }


Member Function Documentation

void LocaleMgr::deleteLocales (  )  [private]

Definition at line 201 of file localemgr.cpp.

00201                               {
00202 
00203     LocaleMap::iterator it;
00204 
00205     for (it = locales->begin(); it != locales->end(); it++)
00206         delete (*it).second;
00207 
00208     locales->erase(locales->begin(), locales->end());
00209 }

std::list< SWBuf > LocaleMgr::getAvailableLocales (  )  [virtual]

Get the list of available locales.

Returns:
Returns a list of strings, which contains the names of the available locales.

Definition at line 224 of file localemgr.cpp.

00224                                                {
00225     std::list <SWBuf> retVal;
00226     for (LocaleMap::iterator it = locales->begin(); it != locales->end(); it++) {
00227         if (strcmp(it->second->getName(), "locales")) {
00228             retVal.push_back((*it).second->getName());
00229         }
00230     }
00231 
00232     return retVal;
00233 }

const char * LocaleMgr::getDefaultLocaleName (  )  [virtual]

Get the default locale name. To set it use

See also:
setDefaultLocaleName
Returns:
Returns the default locale name

Definition at line 248 of file localemgr.cpp.

00248                                             {
00249     return defaultLocaleName;
00250 }

SWLocale * LocaleMgr::getLocale ( const char *  name  )  [virtual]

Get the locale connected with the name "name".

Parameters:
name The name of the locale you want to have. For example use getLocale("de") to get the locale for the German language.
Returns:
Returns the locale object if the locale with the name given as parameter was found. If it wasn't found return NULL.

Definition at line 212 of file localemgr.cpp.

00212                                                {
00213     LocaleMap::iterator it;
00214 
00215     it = locales->find(name);
00216     if (it != locales->end())
00217         return (*it).second;
00218 
00219     SWLog::getSystemLog()->logWarning("LocaleMgr::getLocale failed to find %s\n", name);
00220     return (*locales)[SWLocale::DEFAULT_LOCALE_NAME];
00221 }

LocaleMgr * LocaleMgr::getSystemLocaleMgr (  )  [static]

The LocaleMgr object used globally in the Sword world. Do not create your own LocaleMgr, use this static object instead.

Definition at line 51 of file localemgr.cpp.

00051                                          {
00052     if (!systemLocaleMgr) {
00053         setSystemLocaleMgr(new LocaleMgr());
00054     }
00055 
00056     return systemLocaleMgr;
00057 }

void LocaleMgr::loadConfigDir ( const char *  ipath  )  [virtual]

Augment this localmgr with all locale.conf files in a directory

Definition at line 155 of file localemgr.cpp.

00155                                                {
00156     DIR *dir;
00157     struct dirent *ent;
00158     SWBuf newmodfile;
00159     LocaleMap::iterator it;
00160     SWLog::getSystemLog()->logInformation("LocaleMgr::loadConfigDir loading %s", ipath);
00161  
00162     if ((dir = opendir(ipath))) {
00163         rewinddir(dir);
00164         while ((ent = readdir(dir))) {
00165             if ((strcmp(ent->d_name, ".")) && (strcmp(ent->d_name, ".."))) {
00166                 newmodfile = ipath;
00167                 if ((ipath[strlen(ipath)-1] != '\\') && (ipath[strlen(ipath)-1] != '/'))
00168                     newmodfile += "/";
00169                 newmodfile += ent->d_name;
00170                 SWLocale *locale = new SWLocale(newmodfile.c_str());
00171                 
00172                 if (locale->getName()) {                    
00173                     bool supported = false;
00174                     if (StringMgr::hasUTF8Support()) {
00175                         supported = (locale->getEncoding() && (!strcmp(locale->getEncoding(), "UTF-8") || !strcmp(locale->getEncoding(), "ASCII")) );
00176                     }
00177                     else {
00178                         supported = !locale->getEncoding() || (locale->getEncoding() && (strcmp(locale->getEncoding(), "UTF-8") != 0)); //exclude UTF-8 locales
00179                     }
00180                     
00181                     if (!supported) { //not supported
00182                         delete locale;
00183                         continue;
00184                     }
00185                 
00186                     it = locales->find(locale->getName());
00187                     if (it != locales->end()) { // already present
00188                         *((*it).second) += *locale;
00189                         delete locale;
00190                     }
00191                     else locales->insert(LocaleMap::value_type(locale->getName(), locale));
00192                 }
00193                 else    delete locale;
00194             }
00195         }
00196         closedir(dir);
00197     }
00198 }

void LocaleMgr::setDefaultLocaleName ( const char *  name  )  [virtual]

Set the new standard locale of Sword.

Parameters:
name The name of the new default locale

Definition at line 253 of file localemgr.cpp.

00253                                                      {
00254     char *tmplang=0;
00255     stdstr(&tmplang, name);
00256     // discard everything after '.' usually encoding e.g. .UTF-8
00257     strtok(tmplang, ".");
00258     // also discard after '@' so e.g. @euro locales are found
00259     strtok(tmplang, "@");
00260 
00261     stdstr(&defaultLocaleName, tmplang);
00262 
00263     // First check for what we ask for
00264     if (!getLocale(tmplang)) {
00265         // check for locale without country
00266         char *nocntry=0;
00267         stdstr(&nocntry, tmplang);
00268         strtok(nocntry, "_");
00269         if (getLocale(nocntry)) {
00270             stdstr(&defaultLocaleName, nocntry);
00271         }
00272         delete [] nocntry;
00273     }
00274     delete [] tmplang;
00275 }

void LocaleMgr::setSystemLocaleMgr ( LocaleMgr newLocaleMgr  )  [static]

Definition at line 60 of file localemgr.cpp.

00060                                                           {
00061     if (systemLocaleMgr)
00062         delete systemLocaleMgr;
00063     systemLocaleMgr = newLocaleMgr;
00064     SWLocale *locale = new SWLocale(0);
00065     systemLocaleMgr->locales->insert(LocaleMap::value_type(locale->getName(), locale));
00066 }

const char * LocaleMgr::translate ( const char *  text,
const char *  localeName = 0 
) [virtual]

Returns translated text. This function uses both parameters to return the translated version of the given text.

Parameters:
text The text to translate into the language given by the first parameter.
localeName The name of the locale Sword should use
Returns:
Returns the translated text.

Definition at line 236 of file localemgr.cpp.

00236                                                                          {
00237     SWLocale *target;
00238     if (!localeName) {
00239         localeName = getDefaultLocaleName();
00240     }
00241     target = getLocale(localeName);
00242     if (target)
00243         return target->translate(text);
00244     return text;
00245 }


Friends And Related Function Documentation

friend class __staticsystemLocaleMgr [friend]

Definition at line 54 of file localemgr.h.


Member Data Documentation

Definition at line 52 of file localemgr.h.

Definition at line 56 of file localemgr.h.

SWORD_NAMESPACE_START LocaleMgr * LocaleMgr::systemLocaleMgr = 0 [static, protected]

Definition at line 57 of file localemgr.h.


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

Generated on 18 Mar 2013 for The SWORD Project by  doxygen 1.6.1