| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include <libintl.h> /* IWYU pragma: export */ |
| 5 |
#include <locale.h> /* IWYU pragma: export */ |
| 6 |
|
| 7 |
#include "sd-dlopen.h" /* IWYU pragma: export */ |
| 8 |
|
| 9 |
#include "basic-forward.h" |
| 10 |
#include "dlfcn-util.h" |
| 11 |
|
| 12 |
/* format_arg(2) propagates the format-string nature of the second argument to the return value, so that |
| 13 |
* printf(_("Hello %s"), name) still gets checked. It survives both DLSYM_PROTOTYPE's typeof() and the |
| 14 |
* ternary in _() below — verified on gcc and clang. */ |
| 15 |
extern DLSYM_PROTOTYPE(dgettext) __attribute__((format_arg(2))); |
| 16 |
|
| 17 |
int dlopen_libintl(int log_level); |
| 18 |
|
| 19 |
#ifdef __GLIBC__ |
| 20 |
#define DLOPEN_LIBINTL(log_level, priority) dlopen_libintl(log_level) |
| 21 |
#else |
| 22 |
#define LIBINTL_NOTE(priority) \ |
| 23 |
SD_ELF_NOTE_DLOPEN("intl", \ |
| 24 |
"Support for message translation via gettext", \ |
| 25 |
priority, \ |
| 26 |
"libintl.so.8") |
| 27 |
|
| 28 |
#define DLOPEN_LIBINTL(log_level, priority) \ |
| 29 |
({ \ |
| 30 |
LIBINTL_NOTE(priority); \ |
| 31 |
dlopen_libintl(log_level); \ |
| 32 |
}) |
| 33 |
#endif |
| 34 |
|
| 35 |
typedef enum LocaleVariable { |
| 36 |
/* We don't list LC_ALL here on purpose. People should be |
| 37 |
* using LANG instead. */ |
| 38 |
|
| 39 |
VARIABLE_LANG, |
| 40 |
VARIABLE_LANGUAGE, |
| 41 |
VARIABLE_LC_CTYPE, |
| 42 |
VARIABLE_LC_NUMERIC, |
| 43 |
VARIABLE_LC_TIME, |
| 44 |
VARIABLE_LC_COLLATE, |
| 45 |
VARIABLE_LC_MONETARY, |
| 46 |
VARIABLE_LC_MESSAGES, |
| 47 |
VARIABLE_LC_PAPER, |
| 48 |
VARIABLE_LC_NAME, |
| 49 |
VARIABLE_LC_ADDRESS, |
| 50 |
VARIABLE_LC_TELEPHONE, |
| 51 |
VARIABLE_LC_MEASUREMENT, |
| 52 |
VARIABLE_LC_IDENTIFICATION, |
| 53 |
_VARIABLE_LC_MAX, |
| 54 |
_VARIABLE_LC_INVALID = -EINVAL, |
| 55 |
} LocaleVariable; |
| 56 |
|
| 57 |
int get_locales(char ***ret); |
| 58 |
bool locale_is_valid(const char *name); |
| 59 |
int locale_is_installed(const char *name); |
| 60 |
|
| 61 |
/* Falls back to the untranslated string if dlopen_libintl() hasn't run or has failed, so callers don't have |
| 62 |
* to gate every translatable message on a runtime check. */ |
| 63 |
#define _(String) (sym_dgettext ? sym_dgettext(GETTEXT_PACKAGE, (String)) : (String)) |
| 64 |
#define N_(String) String |
| 65 |
|
| 66 |
bool is_locale_utf8(void); |
| 67 |
|
| 68 |
DECLARE_STRING_TABLE_LOOKUP(locale_variable, LocaleVariable); |
| 69 |
|
| 70 |
static inline void freelocalep(locale_t *p) { |
| 71 |
if (*p == (locale_t) 0) |
| 72 |
return; |
| 73 |
|
| 74 |
freelocale(*p); |
| 75 |
} |
| 76 |
|
| 77 |
void locale_variables_free(char* l[_VARIABLE_LC_MAX]); |
| 78 |
static inline void locale_variables_freep(char*(*l)[_VARIABLE_LC_MAX]) { |
| 79 |
locale_variables_free(*l); |
| 80 |
} |
| 81 |
void locale_variables_simplify(char *l[_VARIABLE_LC_MAX]); |