simplify i18n code (needs MUCH more...)

This commit is contained in:
Albert Cahalan 2009-11-21 23:07:48 +00:00
parent 1c980026f0
commit 57aa18e8fe

View file

@ -176,7 +176,6 @@ static int lang_y_nudge[][2] = {
}; };
static char *langstr;
int need_own_font; int need_own_font;
int need_right_to_left; int need_right_to_left;
int need_right_to_left_word; int need_right_to_left_word;
@ -327,13 +326,6 @@ static int search_int_array(int l, int *array)
return 0; return 0;
} }
static void set_langstr(const char *s)
{
if (langstr)
free(langstr);
langstr = strdup(s);
}
// This is to ensure that iswprint() works beyond ASCII, // This is to ensure that iswprint() works beyond ASCII,
// even if the locale wouldn't normally support that. // even if the locale wouldn't normally support that.
static void ctype_utf8(void) static void ctype_utf8(void)
@ -660,13 +652,12 @@ static void show_locale_usage(FILE * f, const char *const prg)
"\n", prg); "\n", prg);
} }
static int setup_language(const char *const prg) MUST_CHECK; static int setup_language(const char *const prg, const char *langstr)
static int setup_language(const char *const prg)
{ {
if (langstr) if (!langstr)
{ return;
int i =
sizeof language_to_locale_array / sizeof language_to_locale_array[0]; int i = sizeof language_to_locale_array / sizeof language_to_locale_array[0];
const char *locale = NULL; const char *locale = NULL;
while (i--) while (i--)
@ -682,14 +673,12 @@ static int setup_language(const char *const prg)
if (strcmp(langstr, "help") == 0 || strcmp(langstr, "list") == 0) if (strcmp(langstr, "help") == 0 || strcmp(langstr, "list") == 0)
{ {
show_lang_usage(stdout, prg); show_lang_usage(stdout, prg);
//free(langstr); // pointless
exit(0); exit(0);
} }
else else
{ {
fprintf(stderr, "%s is an invalid language\n", langstr); fprintf(stderr, "%s is an invalid language\n", langstr);
show_lang_usage(stderr, prg); show_lang_usage(stderr, prg);
//free(langstr); // pointless
exit(1); exit(1);
} }
} }
@ -709,14 +698,10 @@ static int setup_language(const char *const prg)
putenv(s); putenv(s);
} }
setlocale(LC_ALL, ""); // Specifies an implementation-dependent native environment. For XSI-conformant systems, this corresponds to the value of the associated environment variables, LC_* and LANG // Specifies an implementation-dependent native environment.
// For XSI-conformant systems, this corresponds to the value
ctype_utf8(); // of the associated environment variables, LC_* and LANG
free(langstr); setlocale(LC_ALL, "");
langstr = NULL;
}
return set_current_language();
} }
@ -736,18 +721,17 @@ static void do_locale_option(const char *const arg)
// of the environment. If it were local, the environment would // of the environment. If it were local, the environment would
// get corrupted. // get corrupted.
setlocale(LC_ALL, ""); /* use arg ? */ setlocale(LC_ALL, ""); /* use arg ? */
ctype_utf8();
} }
int setup_i18n(const char *restrict lang, const char *restrict locale) int setup_i18n(const char *restrict lang, const char *restrict locale)
{ {
printf("lang %p, locale %p\n", lang, locale); printf("lang %p, locale %p\n", lang, locale);
printf("lang \"%s\", locale \"%s\"\n", lang, locale); printf("lang \"%s\", locale \"%s\"\n", lang, locale);
if(lang)
set_langstr(lang);
if(locale) if(locale)
do_locale_option(locale); do_locale_option(locale);
int y_nudge = setup_language("tuxpaint"); setup_language("tuxpaint", lang);
ctype_utf8();
int y_nudge = set_current_language();
printf("lang_prefixes[%d] is \"%s\"\n", get_current_language(), lang_prefixes[get_current_language()]); printf("lang_prefixes[%d] is \"%s\"\n", get_current_language(), lang_prefixes[get_current_language()]);
return y_nudge; return y_nudge;
} }