mirror of
https://codeberg.org/dnkl/foot.git
synced 2026-03-18 05:34:02 -04:00
main: invalid locale != non-UTF-8 locale
When doing locale fallback, and printing user notifications and log warnings, better separate the case "locale is invalid" from "locale is valid but not UTF-8". Closes #1798
This commit is contained in:
parent
7e1894978f
commit
6d351ffc43
1 changed files with 35 additions and 18 deletions
53
main.c
53
main.c
|
|
@ -425,19 +425,18 @@ main(int argc, char *const *argv)
|
||||||
* that does not exist on this system, then the above call may return
|
* that does not exist on this system, then the above call may return
|
||||||
* NULL. We should just continue with the fallback method below.
|
* NULL. We should just continue with the fallback method below.
|
||||||
*/
|
*/
|
||||||
LOG_WARN("setlocale() failed");
|
LOG_ERR("setlocale() failed");
|
||||||
locale = "C";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_INFO("locale: %s", locale);
|
LOG_INFO("locale: %s", locale != NULL ? locale : "<invalid>");
|
||||||
|
|
||||||
bool bad_locale = !locale_is_utf8();
|
bool bad_locale = locale == NULL || !locale_is_utf8();
|
||||||
if (bad_locale) {
|
if (bad_locale) {
|
||||||
static const char fallback_locales[][12] = {
|
static const char fallback_locales[][12] = {
|
||||||
"C.UTF-8",
|
"C.UTF-8",
|
||||||
"en_US.UTF-8",
|
"en_US.UTF-8",
|
||||||
};
|
};
|
||||||
char *saved_locale = xstrdup(locale);
|
char *saved_locale = locale != NULL ? xstrdup(locale) : NULL;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Try to force an UTF-8 locale. If we succeed, launch the
|
* Try to force an UTF-8 locale. If we succeed, launch the
|
||||||
|
|
@ -448,13 +447,23 @@ main(int argc, char *const *argv)
|
||||||
const char *const fallback_locale = fallback_locales[i];
|
const char *const fallback_locale = fallback_locales[i];
|
||||||
|
|
||||||
if (setlocale(LC_CTYPE, fallback_locale) != NULL) {
|
if (setlocale(LC_CTYPE, fallback_locale) != NULL) {
|
||||||
LOG_WARN("'%s' is not a UTF-8 locale, using '%s' instead",
|
if (saved_locale != NULL) {
|
||||||
saved_locale, fallback_locale);
|
LOG_WARN(
|
||||||
|
"'%s' is not a UTF-8 locale, falling back to '%s'",
|
||||||
|
saved_locale, fallback_locale);
|
||||||
|
|
||||||
user_notification_add_fmt(
|
user_notification_add_fmt(
|
||||||
&user_notifications, USER_NOTIFICATION_WARNING,
|
&user_notifications, USER_NOTIFICATION_WARNING,
|
||||||
"'%s' is not a UTF-8 locale, using '%s' instead",
|
"'%s' is not a UTF-8 locale, falling back to '%s'",
|
||||||
saved_locale, fallback_locale);
|
saved_locale, fallback_locale);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
LOG_WARN(
|
||||||
|
"invalid locale, falling back to '%s'", fallback_locale);
|
||||||
|
user_notification_add_fmt(
|
||||||
|
&user_notifications, USER_NOTIFICATION_WARNING,
|
||||||
|
"invalid locale, falling back to '%s'", fallback_locale);
|
||||||
|
}
|
||||||
|
|
||||||
bad_locale = false;
|
bad_locale = false;
|
||||||
break;
|
break;
|
||||||
|
|
@ -462,14 +471,22 @@ main(int argc, char *const *argv)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bad_locale) {
|
if (bad_locale) {
|
||||||
LOG_ERR(
|
if (saved_locale != NULL) {
|
||||||
"'%s' is not a UTF-8 locale, and failed to find a fallback",
|
LOG_ERR(
|
||||||
saved_locale);
|
"'%s' is not a UTF-8 locale, and failed to find a fallback",
|
||||||
|
saved_locale);
|
||||||
|
|
||||||
user_notification_add_fmt(
|
user_notification_add_fmt(
|
||||||
&user_notifications, USER_NOTIFICATION_ERROR,
|
&user_notifications, USER_NOTIFICATION_ERROR,
|
||||||
"'%s' is not a UTF-8 locale, and failed to find a fallback",
|
"'%s' is not a UTF-8 locale, and failed to find a fallback",
|
||||||
saved_locale);
|
saved_locale);
|
||||||
|
} else {
|
||||||
|
LOG_ERR("invalid locale, and failed to find a fallback");
|
||||||
|
|
||||||
|
user_notification_add_fmt(
|
||||||
|
&user_notifications, USER_NOTIFICATION_ERROR,
|
||||||
|
"invalid locale, and failed to find a fallback");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
free(saved_locale);
|
free(saved_locale);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue