locale-util: add new helper locale_is_installed()
This new helper checks whether the specified locale is installed. It's distinct from locale_is_valid() which just superficially checks if a string looks like something that could be a valid locale. Heavily inspired by @jsynacek's #13964. Replaces: #13964
This commit is contained in:
@@ -254,6 +254,21 @@ bool locale_is_valid(const char *name) {
|
||||
return true;
|
||||
}
|
||||
|
||||
int locale_is_installed(const char *name) {
|
||||
if (!locale_is_valid(name))
|
||||
return false;
|
||||
|
||||
if (STR_IN_SET(name, "C", "POSIX")) /* These ones are always OK */
|
||||
return true;
|
||||
|
||||
_cleanup_(freelocalep) locale_t loc =
|
||||
newlocale(LC_ALL_MASK, name, 0);
|
||||
if (loc == (locale_t) 0)
|
||||
return errno == ENOMEM ? -ENOMEM : false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void init_gettext(void) {
|
||||
setlocale(LC_ALL, "");
|
||||
textdomain(GETTEXT_PACKAGE);
|
||||
|
||||
Reference in New Issue
Block a user