refactor(lang): reduce scope of HAVE_WORKING_LIBINTL #ifdefs

A lot of code inside HAVE_WORKING_LIBINTL doesn't really depend on a
"working libintl". For instance ex_language is also used for ":lang collate"
and ":lang time".

Also ":lang C" should not fail just because translations aren't available (it
just means use the default text).

References:

https://github.com/neovim/neovim/pull/1 2d00ead2e5
separate ifdefs for locale and gettext got merged together.

8253e29971
Unmotivated switcharoo of get_mess_env() logic. If available, get_locale_val(LC_MESSAGES) is the correct implementation.
This commit is contained in:
bfredl 2023-10-11 22:16:25 +02:00
parent 0818d65528
commit 3a44db510b
4 changed files with 31 additions and 48 deletions

View File

@ -2130,10 +2130,9 @@ static const char *set_context_by_cmdname(const char *cmd, cmdidx_T cmdidx, expa
set_context_in_runtime_cmd(xp, arg); set_context_in_runtime_cmd(xp, arg);
break; break;
#ifdef HAVE_WORKING_LIBINTL
case CMD_language: case CMD_language:
return set_context_in_lang_cmd(xp, arg); return set_context_in_lang_cmd(xp, arg);
#endif
case CMD_profile: case CMD_profile:
set_context_in_profile_cmd(xp, arg); set_context_in_profile_cmd(xp, arg);
break; break;
@ -2644,10 +2643,8 @@ static int ExpandOther(char *pat, expand_T *xp, regmatch_T *rmp, char ***matches
{ EXPAND_AUGROUP, expand_get_augroup_name, true, false }, { EXPAND_AUGROUP, expand_get_augroup_name, true, false },
{ EXPAND_SIGN, get_sign_name, true, true }, { EXPAND_SIGN, get_sign_name, true, true },
{ EXPAND_PROFILE, get_profile_name, true, true }, { EXPAND_PROFILE, get_profile_name, true, true },
#ifdef HAVE_WORKING_LIBINTL
{ EXPAND_LANGUAGE, get_lang_arg, true, false }, { EXPAND_LANGUAGE, get_lang_arg, true, false },
{ EXPAND_LOCALES, get_locales, true, false }, { EXPAND_LOCALES, get_locales, true, false },
#endif
{ EXPAND_ENV_VARS, get_env_name, true, true }, { EXPAND_ENV_VARS, get_env_name, true, true },
{ EXPAND_USER, get_users, true, false }, { EXPAND_USER, get_users, true, false },
{ EXPAND_ARGLIST, get_arglist_name, true, false }, { EXPAND_ARGLIST, get_arglist_name, true, false },

View File

@ -150,10 +150,6 @@ struct dbg_stuff {
# include "ex_docmd.c.generated.h" # include "ex_docmd.c.generated.h"
#endif #endif
#ifndef HAVE_WORKING_LIBINTL
# define ex_language ex_ni
#endif
// Declare cmdnames[]. // Declare cmdnames[].
#ifdef INCLUDE_GENERATED_DECLARATIONS #ifdef INCLUDE_GENERATED_DECLARATIONS
# include "ex_cmds_defs.generated.h" # include "ex_cmds_defs.generated.h"

View File

@ -17,7 +17,7 @@
# undef setlocale # undef setlocale
# endif # endif
#else #else
# define _(x) (x) # define _(x) ((char *)(x))
# define N_(x) x # define N_(x) x
# define NGETTEXT(x, xs, n) ((n) == 1 ? (x) : (xs)) # define NGETTEXT(x, xs, n) ((n) == 1 ? (x) : (xs))
# define bindtextdomain(x, y) // empty # define bindtextdomain(x, y) // empty

View File

@ -71,20 +71,23 @@ char *get_mess_lang(void)
return is_valid_mess_lang(p) ? p : NULL; return is_valid_mess_lang(p) ? p : NULL;
} }
// Complicated #if; matches with where get_mess_env() is used below.
#ifdef HAVE_WORKING_LIBINTL
/// Get the language used for messages from the environment. /// Get the language used for messages from the environment.
///
/// This uses LC_MESSAGES when available, which it is for most systems we build for
/// except for windows. Then fallback to get the value from the envirionment
/// ourselves, and use LC_CTYPE as a last resort.
static char *get_mess_env(void) static char *get_mess_env(void)
{ {
char *p; #ifdef LC_MESSAGES
return get_locale_val(LC_MESSAGES);
p = (char *)os_getenv("LC_ALL"); #else
char *p = (char *)os_getenv("LC_ALL");
if (p == NULL) { if (p == NULL) {
p = (char *)os_getenv("LC_MESSAGES"); p = (char *)os_getenv("LC_MESSAGES");
if (p == NULL) { if (p == NULL) {
p = (char *)os_getenv("LANG"); p = (char *)os_getenv("LANG");
if (p != NULL && ascii_isdigit(*p)) { if (p != NULL && ascii_isdigit(*p)) {
p = NULL; // ignore something like "1043" p = NULL; // ignore something like "1043"
} }
if (p == NULL) { if (p == NULL) {
p = get_locale_val(LC_CTYPE); p = get_locale_val(LC_CTYPE);
@ -92,8 +95,8 @@ static char *get_mess_env(void)
} }
} }
return p; return p;
}
#endif #endif
}
/// Set the "v:lang" variable according to the current locale setting. /// Set the "v:lang" variable according to the current locale setting.
/// Also do "v:lc_time"and "v:ctype". /// Also do "v:lc_time"and "v:ctype".
@ -104,16 +107,7 @@ void set_lang_var(void)
loc = get_locale_val(LC_CTYPE); loc = get_locale_val(LC_CTYPE);
set_vim_var_string(VV_CTYPE, loc, -1); set_vim_var_string(VV_CTYPE, loc, -1);
// When LC_MESSAGES isn't defined use the value from $LC_MESSAGES, fall
// back to LC_CTYPE if it's empty.
#ifdef HAVE_WORKING_LIBINTL
loc = get_mess_env(); loc = get_mess_env();
#elif defined(LC_MESSAGES)
loc = get_locale_val(LC_MESSAGES);
#else
// In Windows LC_MESSAGES is not defined fallback to LC_CTYPE
loc = get_locale_val(LC_CTYPE);
#endif
set_vim_var_string(VV_LANG, loc, -1); set_vim_var_string(VV_LANG, loc, -1);
loc = get_locale_val(LC_TIME); loc = get_locale_val(LC_TIME);
@ -145,8 +139,6 @@ void init_locale(void)
TIME_MSG("locale set"); TIME_MSG("locale set");
} }
#ifdef HAVE_WORKING_LIBINTL
/// ":language": Set the language (locale). /// ":language": Set the language (locale).
/// ///
/// @param eap /// @param eap
@ -157,11 +149,11 @@ void ex_language(exarg_T *eap)
char *name; char *name;
int what = LC_ALL; int what = LC_ALL;
char *whatstr = ""; char *whatstr = "";
# ifdef LC_MESSAGES #ifdef LC_MESSAGES
# define VIM_LC_MESSAGES LC_MESSAGES # define VIM_LC_MESSAGES LC_MESSAGES
# else #else
# define VIM_LC_MESSAGES 6789 # define VIM_LC_MESSAGES 6789
# endif #endif
name = eap->arg; name = eap->arg;
@ -200,29 +192,29 @@ void ex_language(exarg_T *eap)
} }
smsg(0, _("Current %slanguage: \"%s\""), whatstr, p); smsg(0, _("Current %slanguage: \"%s\""), whatstr, p);
} else { } else {
# ifndef LC_MESSAGES #ifndef LC_MESSAGES
if (what == VIM_LC_MESSAGES) { if (what == VIM_LC_MESSAGES) {
loc = ""; loc = "";
} else { } else {
# endif #endif
loc = setlocale(what, name); loc = setlocale(what, name);
# ifdef LC_NUMERIC #ifdef LC_NUMERIC
// Make sure strtod() uses a decimal point, not a comma. // Make sure strtod() uses a decimal point, not a comma.
setlocale(LC_NUMERIC, "C"); setlocale(LC_NUMERIC, "C");
# endif #endif
# ifndef LC_MESSAGES #ifndef LC_MESSAGES
} }
# endif #endif
if (loc == NULL) { if (loc == NULL) {
semsg(_("E197: Cannot set language to \"%s\""), name); semsg(_("E197: Cannot set language to \"%s\""), name);
} else { } else {
# ifdef HAVE_NL_MSG_CAT_CNTR #ifdef HAVE_NL_MSG_CAT_CNTR
// Need to do this for GNU gettext, otherwise cached translations // Need to do this for GNU gettext, otherwise cached translations
// will be used again. // will be used again.
extern int _nl_msg_cat_cntr; extern int _nl_msg_cat_cntr;
_nl_msg_cat_cntr++; _nl_msg_cat_cntr++;
# endif #endif
// Reset $LC_ALL, otherwise it would overrule everything. // Reset $LC_ALL, otherwise it would overrule everything.
os_setenv("LC_ALL", "", 1); os_setenv("LC_ALL", "", 1);
@ -250,7 +242,7 @@ void ex_language(exarg_T *eap)
static char **locales = NULL; // Array of all available locales static char **locales = NULL; // Array of all available locales
# ifndef MSWIN #ifndef MSWIN
static bool did_init_locales = false; static bool did_init_locales = false;
/// @return an array of strings for all available locales + NULL for the /// @return an array of strings for all available locales + NULL for the
@ -285,22 +277,22 @@ static char **find_locales(void)
((char **)locales_ga.ga_data)[locales_ga.ga_len] = NULL; ((char **)locales_ga.ga_data)[locales_ga.ga_len] = NULL;
return locales_ga.ga_data; return locales_ga.ga_data;
} }
# endif #endif
/// Lazy initialization of all available locales. /// Lazy initialization of all available locales.
static void init_locales(void) static void init_locales(void)
{ {
# ifndef MSWIN #ifndef MSWIN
if (did_init_locales) { if (did_init_locales) {
return; return;
} }
did_init_locales = true; did_init_locales = true;
locales = find_locales(); locales = find_locales();
# endif #endif
} }
# if defined(EXITFREE) #if defined(EXITFREE)
void free_locales(void) void free_locales(void)
{ {
if (locales == NULL) { if (locales == NULL) {
@ -312,7 +304,7 @@ void free_locales(void)
} }
XFREE_CLEAR(locales); XFREE_CLEAR(locales);
} }
# endif #endif
/// Function given to ExpandGeneric() to obtain the possible arguments of the /// Function given to ExpandGeneric() to obtain the possible arguments of the
/// ":language" command. /// ":language" command.
@ -348,8 +340,6 @@ char *get_locales(expand_T *xp, int idx)
return locales[idx]; return locales[idx];
} }
#endif
void lang_init(void) void lang_init(void)
{ {
#ifdef __APPLE__ #ifdef __APPLE__