예제 #1
0
파일: lang.c 프로젝트: MrBadge/enca
/**
 * enca_language_init:
 * @analyser: Analyzer state to be initialized for this language.
 * @langname: Two-letter ISO-639 language code.
 *
 * Initializes analyser for language @langname.
 *
 * Assumes @analyser is unitinialized, calling with an initialized @analyser
 * leads to memory leak.
 *
 * Returns: Nonzero on success, zero otherwise.
 **/
int
enca_language_init(EncaAnalyserState *analyser,
                   const char *langname)
{
  const EncaLanguageInfo *lang;

  assert(langname != NULL);

  analyser->lang = NULL;
  analyser->ncharsets = 0;
  analyser->charsets = NULL;
  analyser->lcbits = NULL;
  analyser->ucbits = NULL;

  lang = find_language(langname);
  if (lang == NULL)
    return 0;

  analyser->lang = lang;
  if (lang->ncharsets == 0)
    return 1;

  analyser->ncharsets = lang->ncharsets;
  analyser->charsets = language_charsets_ids(lang);

  return 1;
}
static bool
ReadResourceLanguageFile(const TCHAR *resource)
{
  if (!find_language(resource))
    /* refuse to load resources which are not in the language table */
    return false;

  LogStartUp(_T("Language: loading resource '%s'"), resource);

  // Load resource
  ResourceLoader::Data data = ResourceLoader::Load(resource, _T("MO"));
  if (data.first == NULL) {
    LogStartUp(_T("Language: resource '%s' not found"), resource);
    return false;
  }

  // Load MO file from resource
  delete mo_loader;
  mo_loader = new MOLoader(data.first, data.second);
  if (mo_loader->error()) {
    LogStartUp(_T("Language: could not load resource '%s'"), resource);
    delete mo_loader;
    mo_loader = NULL;
    return false;
  }

  LogStartUp(_T("Loaded translations from resource '%s'"), resource);

  mo_file = &mo_loader->get();
  return true;
}
예제 #3
0
파일: lang.c 프로젝트: MrBadge/enca
/**
 * enca_language_english_name:
 * @lang: A two-letter language code, such as obtained from
 *        enca_analyser_language() or enca_get_languages().
 *
 * Returns an English name of a language given its ISO-639 code.
 *
 * The returned string must be considered constant and must NOT be freed.
 *
 * Returns: The English language name.
 **/
const char*
enca_language_english_name(const char *lang)
{
  const EncaLanguageInfo *linfo;

  linfo = find_language(lang);
  if (!linfo)
    return NULL;

  return linfo->humanname;
}
예제 #4
0
파일: lang.c 프로젝트: MrBadge/enca
/**
 * enca_get_language_charsets:
 * @langname: Two-letter ISO-639 language code.
 * @n: The number of charsets will be stored here.
 *
 * Returns list of identifiers of charsets supported for language @language.
 *
 * The list of charset identifiers has to be freed by caller.
 *
 * Returns: The list of charsets, storing their number into *@n.  When language
 *          contains no charsets or @langname is invalid, #NULL is returned
 *          and zero stored into *@n.
 **/
int*
enca_get_language_charsets(const char *langname,
                           size_t *n)
{
  const EncaLanguageInfo *lang;

  assert(langname != NULL);

  lang = find_language(langname);
  if (lang == NULL) {
    *n = 0;
    return NULL;
  }

  *n = lang->ncharsets;
  return language_charsets_ids(lang);
}
예제 #5
0
파일: UnitsGlue.cpp 프로젝트: Mrdini/XCSoar
static unsigned
AutoDetect()
{
#ifndef HAVE_POSIX

#if defined(_WIN32_WCE)
  /* the GetUserDefaultUILanguage() prototype is missing on
     mingw32ce, we have to look it up dynamically */
  DynamicLibrary coreloc_dll(_T("coredll"));
  if (!coreloc_dll.defined()) {
    LogStartUp(_T("Units: coredll.dll not found"));
    return 0;
  }

  typedef LANGID WINAPI (*GetUserDefaultUILanguage_t)();
  GetUserDefaultUILanguage_t GetUserDefaultUILanguage =
    (GetUserDefaultUILanguage_t)
    coreloc_dll.lookup(_T("GetUserDefaultUILanguage"));
  if (GetUserDefaultUILanguage == NULL) {
    LogStartUp(_T("Units: GetUserDefaultUILanguage() not available"));
    return 0;
  }
#endif

  // Retrieve the default user language identifier from the OS
  LANGID lang_id = GetUserDefaultUILanguage();
  LogStartUp(_T("Units: GetUserDefaultUILanguage() = 0x%x"), (int)lang_id);
  if (lang_id == 0)
    return 0;

  return find_language(lang_id);

#elif defined(ANDROID)
  JNIEnv *env = Java::GetEnv();

  Java::Class cls(env, "java/util/Locale");

  // Call static function Locale.getDefault() that
  // returns the user's default Locale object

  jmethodID cid = env->GetStaticMethodID(cls, "getDefault",
                                         "()Ljava/util/Locale;");
  assert(cid != NULL);

  jobject _obj = env->CallStaticObjectMethod(cls, cid);
  if (_obj == NULL)
    return 0;

  Java::LocalObject obj(env, _obj);

  // Call function Locale.getLanguage() that
  // returns a two-letter language string

  cid = env->GetMethodID(cls, "toString", "()Ljava/lang/String;");
  assert(cid != NULL);

  jstring language = (jstring)env->CallObjectMethod(obj, cid);
  if (language == NULL)
    return 0;

  // Convert the jstring to a char string
  const char *language2 = env->GetStringUTFChars(language, NULL);
  if (language2 == NULL) {
    env->DeleteLocalRef(language);
    return 0;
  }

  unsigned id = find_language(language2);

  // Clean up the memory
  env->ReleaseStringUTFChars(language, language2);
  env->DeleteLocalRef(language);

  // Return e.g. "de.mo"
  return id;

#else
  // Metric default on Linux
  return 0;
#endif
}
static const TCHAR *
detect_language()
{
#ifdef ANDROID

  JNIEnv *env = Java::GetEnv();

  Java::Class cls(env, "java/util/Locale");

  // Call static function Locale.getDefault() that
  // returns the user's default Locale object

  jmethodID cid = env->GetStaticMethodID(cls, "getDefault",
                                         "()Ljava/util/Locale;");
  assert(cid != NULL);

  jobject _obj = env->CallStaticObjectMethod(cls, cid);
  if (_obj == NULL)
    return NULL;

  Java::LocalObject obj(env, _obj);

  // Call function Locale.getLanguage() that
  // returns a two-letter language string

  cid = env->GetMethodID(cls, "getLanguage", "()Ljava/lang/String;");
  assert(cid != NULL);

  jstring language = (jstring)env->CallObjectMethod(obj, cid);
  if (language == NULL)
    return NULL;

  // Convert the jstring to a char string
  const char *language2 = env->GetStringUTFChars(language, NULL);
  if (language2 == NULL) {
    env->DeleteLocalRef(language);
    return NULL;
  }

  /* generate the resource name */

  const char *language3 = language2;
  if (strcmp(language3, "pt") == 0)
    /* hack */
    language3 = "pt_BR";

  // Attach .mo to the language identifier
  static char language_buffer[16];
  snprintf(language_buffer, sizeof(language_buffer), "%s.mo", language3);

  // Clean up the memory
  env->ReleaseStringUTFChars(language, language2);
  env->DeleteLocalRef(language);

  // Return e.g. "de.mo"
  return language_buffer;

#else /* !ANDROID */

#if defined(_WIN32_WCE)
  /* the GetUserDefaultUILanguage() prototype is missing on
     mingw32ce, we have to look it up dynamically */
  DynamicLibrary coreloc_dll(_T("coredll"));
  if (!coreloc_dll.defined()) {
    LogStartUp(_T("Language: coredll.dll not found"));
    return NULL;
  }

  typedef LANGID WINAPI (*GetUserDefaultUILanguage_t)();
  GetUserDefaultUILanguage_t GetUserDefaultUILanguage =
    (GetUserDefaultUILanguage_t)
    coreloc_dll.lookup(_T("GetUserDefaultUILanguage"));
  if (GetUserDefaultUILanguage == NULL) {
    LogStartUp(_T("Language: GetUserDefaultUILanguage() not available"));
    return NULL;
  }
#endif

  // Retrieve the default user language identifier from the OS
  LANGID lang_id = GetUserDefaultUILanguage();
  LogStartUp(_T("Language: GetUserDefaultUILanguage()=0x%x"), (int)lang_id);
  if (lang_id == 0)
    return NULL;

  // Try to convert the primary language part of the language identifier
  // to a MO file name in the language table
  return find_language(PRIMARYLANGID(lang_id));

#endif /* !ANDROID */
}