src/tinygettext/findlocale.cpp File Reference

#include <config.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "findlocale.hpp"

Go to the source code of this file.

Functions

static int is_lcchar (const int c)
static void lang_country_variant_from_envstring (const char *str, char **lang, char **country, char **variant)
static int accumulate_locstring (const char *str, FL_Locale *l)
static int accumulate_env (const char *name, FL_Locale *l)
static void canonize_fl (FL_Locale *l)
FL_Success FL_FindLocale (FL_Locale **locale, FL_Domain)
void FL_FreeLocale (FL_Locale **locale)


Function Documentation

static int accumulate_env ( const char *  name,
FL_Locale l 
) [static]

Definition at line 144 of file findlocale.cpp.

References accumulate_locstring().

Referenced by FL_FindLocale().

00144                                                {
00145   char *env;
00146   char *lang = NULL;
00147   char *country = NULL;
00148   char *variant = NULL;
00149   env = getenv(name);
00150   if (env) {
00151     return accumulate_locstring(env, l);
00152   }
00153   free(lang); free(country); free(variant);
00154   return 0;
00155 }

static int accumulate_locstring ( const char *  str,
FL_Locale l 
) [static]

Definition at line 124 of file findlocale.cpp.

References FL_Locale::country, FL_Locale::lang, lang_country_variant_from_envstring(), and FL_Locale::variant.

Referenced by accumulate_env(), and FL_FindLocale().

00124                                                     {
00125   char *lang = NULL;
00126   char *country = NULL;
00127   char *variant = NULL;
00128   if (str) {
00129     lang_country_variant_from_envstring(str, &lang, &country, &variant);
00130     if (lang) {
00131       l->lang = lang;
00132       l->country = country;
00133       l->variant = variant;
00134       return 1;
00135     }
00136   }
00137   free(lang); free(country); free(variant);
00138   return 0;
00139 }

static void canonize_fl ( FL_Locale l  )  [static]

Definition at line 159 of file findlocale.cpp.

References FL_Locale::country, and FL_Locale::lang.

Referenced by FL_FindLocale().

00159                           {
00160   /* this function fixes some common locale-specifying mistakes */
00161   /* en_UK -> en_GB */
00162   if (l->lang && 0 == strcmp(l->lang, "en")) {
00163     if (l->country && 0 == strcmp(l->country, "UK")) {
00164       free((void*)l->country);
00165       l->country = strdup("GB");
00166     }
00167   }
00168   /* ja_JA -> ja_JP */
00169   if (l->lang && 0 == strcmp(l->lang, "ja")) {
00170     if (l->country && 0 == strcmp(l->country, "JA")) {
00171       free((void*)l->country);
00172       l->country = strdup("JP");
00173     }
00174   }
00175 }

FL_Success FL_FindLocale ( FL_Locale **  locale,
FL_Domain   
)

Definition at line 453 of file findlocale.cpp.

References accumulate_env(), accumulate_locstring(), canonize_fl(), FL_Locale::country, FL_CONFIDENT, FL_DEFAULT_GUESS, FL_FAILED, FL_Locale::lang, and FL_Locale::variant.

Referenced by TinyGetText::DictionaryManager::DictionaryManager(), Main::init_tinygettext(), and LanguageMenu::menu_action().

00453                                                         {
00454   FL_Success success = FL_FAILED;
00455   FL_Locale *rtn = (FL_Locale*) malloc(sizeof(FL_Locale));
00456   rtn->lang = NULL;
00457   rtn->country = NULL;
00458   rtn->variant = NULL;
00459 
00460 #ifdef WIN32
00461   /* win32 >= mswindows95 */
00462   {
00463     LCID lcid = GetThreadLocale();
00464     if (lcid_to_fl(lcid, rtn)) {
00465       success = FL_CONFIDENT;
00466     }
00467     if (success == FL_FAILED) {
00468       /* assume US English on mswindows systems unless we know otherwise */
00469       if (accumulate_locstring("en_US.ISO_8859-1", rtn)) {
00470         success = FL_DEFAULT_GUESS;
00471       }
00472     }
00473   }
00474 #else
00475   /* assume unixoid */
00476   {
00477 #ifdef MACOSX
00478     CFIndex sz;
00479     CFArrayRef languages;
00480     CFStringRef uxstylelangs;
00481     char *uxsl;
00482 
00483     /* get the languages from the user's presets */
00484     languages = (CFArrayRef)CFPreferencesCopyValue(CFSTR("AppleLanguages"),
00485       kCFPreferencesAnyApplication, kCFPreferencesCurrentUser,
00486       kCFPreferencesAnyHost);
00487 
00488     /* join the returned string array into a string separated by colons */
00489     uxstylelangs = CFStringCreateByCombiningStrings(kCFAllocatorDefault,
00490       languages, CFSTR(":"));
00491 
00492     /* convert this string into a C string */
00493     sz = CFStringGetLength(uxstylelangs) + 1;
00494     uxsl = (char*)malloc(sz);
00495     CFStringGetCString(uxstylelangs, uxsl, sz, kCFStringEncodingISOLatin1);
00496 
00497     /* add it to the list */
00498     if (accumulate_locstring(uxsl, rtn)) {
00499       success = FL_CONFIDENT;
00500     }
00501     /* continue the UNIX method */
00502 #endif
00503     /* examples: */
00504     /* sv_SE.ISO_8859-1 */
00505     /* fr_FR.ISO8859-1 */
00506     /* no_NO_NB */
00507     /* no_NO_NY */
00508     /* no_NO */
00509     /* de_DE */
00510     /* try the various vars in decreasing order of authority */
00511     if (accumulate_env("LC_ALL", rtn) ||
00512         accumulate_env("LC_MESSAGES", rtn) ||
00513         accumulate_env("LANG", rtn) ||
00514         accumulate_env("LANGUAGE", rtn)) {
00515       success = FL_CONFIDENT;
00516     }
00517     if (success == FL_FAILED) {
00518       /* assume US English on unixoid systems unless we know otherwise */
00519       if (accumulate_locstring("en_US.ISO_8859-1", rtn)) {
00520         success = FL_DEFAULT_GUESS;
00521       }
00522     }
00523   }
00524 #endif
00525 
00526   if (success != FL_FAILED) {
00527     canonize_fl(rtn);
00528   }
00529 
00530   *locale = rtn;
00531   return success;
00532 }

void FL_FreeLocale ( FL_Locale **  locale  ) 

Definition at line 536 of file findlocale.cpp.

References FL_Locale::country, FL_Locale::lang, and FL_Locale::variant.

Referenced by TinyGetText::DictionaryManager::DictionaryManager(), Main::init_tinygettext(), and LanguageMenu::menu_action().

00536                                   {
00537   if (locale) {
00538     FL_Locale *l = *locale;
00539     if (l) {
00540       if (l->lang) {
00541         free((void*)l->lang);
00542       }
00543       if (l->country) {
00544         free((void*)l->country);
00545       }
00546       if (l->variant) {
00547         free((void*)l->variant);
00548       }
00549       free(l);
00550       *locale = NULL;
00551     }
00552   }
00553 }

static int is_lcchar ( const int  c  )  [static]

Definition at line 47 of file findlocale.cpp.

Referenced by lang_country_variant_from_envstring().

00047                        {
00048   return isalnum(c);
00049 }

static void lang_country_variant_from_envstring ( const char *  str,
char **  lang,
char **  country,
char **  variant 
) [static]

Definition at line 52 of file findlocale.cpp.

References is_lcchar().

Referenced by accumulate_locstring().

00055                                                     {
00056   int end = 0;
00057   int start;
00058 
00059   /* get lang, if any */
00060   start = end;
00061   while (is_lcchar(str[end])) {
00062     ++end;
00063   }
00064   if (start != end) {
00065     int i;
00066     int len = end - start;
00067     char *s = (char*) malloc(len + 1);
00068     for (i=0; i<len; ++i) {
00069       s[i] = tolower(str[start + i]);
00070     }
00071     s[i] = '\0';
00072     *lang = s;
00073   } else {
00074     *lang = NULL;
00075   }
00076 
00077   if (str[end] && str[end]!=':') { /* not at end of str */
00078     ++end;
00079   }
00080 
00081   /* get country, if any */
00082   start = end;
00083   while (is_lcchar(str[end])) {
00084     ++end;
00085   }
00086   if (start != end) {
00087     int i;
00088     int len = end - start;
00089     char *s = (char*) malloc(len + 1);
00090     for (i=0; i<len; ++i) {
00091       s[i] = toupper(str[start + i]);
00092     }
00093     s[i] = '\0';
00094     *country = s;
00095   } else {
00096     *country = NULL;
00097   }
00098 
00099   if (str[end] && str[end]!=':') { /* not at end of str */
00100     ++end;
00101   }
00102 
00103   /* get variant, if any */
00104   start = end;
00105   while (str[end] && str[end]!=':') {
00106     ++end;
00107   }
00108   if (start != end) {
00109     int i;
00110     int len = end - start;
00111     char *s = (char*) malloc(len + 1);
00112     for (i=0; i<len; ++i) {
00113       s[i] = str[start + i];
00114     }
00115     s[i] = '\0';
00116     *variant = s;
00117   } else {
00118     *variant = NULL;
00119   }
00120 }


Generated on Mon Jun 9 03:38:27 2014 for SuperTux by  doxygen 1.5.1