Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix gettext integration with tools #358

Merged
merged 4 commits into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 36 additions & 9 deletions common/tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@
#include <stdlib.h>
#include <unistd.h>

#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif

#ifdef ENABLE_NLS
#include <libintl.h>
#define _(x) dgettext(PACKAGE_NAME, x)
#else
#define _(x) (x)
#endif

#include "tool.h"

static char
Expand Down Expand Up @@ -170,13 +181,21 @@ command_usage (const p11_tool_command *commands)
int i;

progname = getprogname ();
printf ("usage: %s command <args>...\n", progname);
printf ("\nCommon %s commands are:\n", progname);
printf (_("usage: %s command <args>...\n"), progname);
printf (_("\nCommon %s commands are:\n"), progname);
for (i = 0; commands[i].name != NULL; i++) {
if (strcmp (commands[i].name, P11_TOOL_FALLBACK) != 0)
printf (" %-15s %s\n", commands[i].name, commands[i].text);
#ifdef ENABLE_NLS
printf (" %-15s %s\n",
commands[i].name,
dgettext (PACKAGE_NAME, commands[i].text));
#else
printf (" %-15s %s\n",
commands[i].name,
commands[i].text);
#endif
}
printf ("\nSee '%s <command> --help' for more information\n", progname);
printf (_("\nSee '%s <command> --help' for more information\n"), progname);
}

static void
Expand Down Expand Up @@ -205,6 +224,14 @@ p11_tool_main (int argc,
int in, out;
int i;

#ifdef HAVE_LOCALE_H
setlocale (LC_ALL, "");
#endif

#ifdef ENABLE_NLS
textdomain (PACKAGE_NAME);
#endif

/* Print messages by default. */
p11_message_loud ();

Expand All @@ -231,7 +258,7 @@ p11_tool_main (int argc,

if (strcmp (argv[in], "--") == 0) {
if (!command) {
p11_message ("no command specified");
p11_message (_("no command specified"));
return 2;
} else {
break;
Expand All @@ -247,7 +274,7 @@ p11_tool_main (int argc,
want_help = true;

} else if (!command) {
p11_message ("unknown global option: %s", argv[in]);
p11_message (_("unknown global option: %s"), argv[in]);
return 2;
}

Expand Down Expand Up @@ -276,7 +303,7 @@ p11_tool_main (int argc,

default:
if (!command) {
p11_message ("unknown global option: -%c", (int)argv[in][i]);
p11_message (_("unknown global option: -%c"), (int)argv[in][i]);
return 2;
}
break;
Expand All @@ -303,7 +330,7 @@ p11_tool_main (int argc,
command_usage (commands);
return 0;
} else {
p11_message ("no command specified");
p11_message (_("no command specified"));
return 2;
}
}
Expand All @@ -328,7 +355,7 @@ p11_tool_main (int argc,
}

/* At this point we have no command */
p11_message ("'%s' is not a valid command. See '%s --help'",
p11_message (_("'%s' is not a valid command. See '%s --help'"),
command, getprogname ());
return 2;
}
5 changes: 5 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ add_project_arguments(['-D_GNU_SOURCE', '-DP11_KIT_FUTURE_UNSTABLE_API'],

conf = configuration_data()

conf.set_quoted('PACKAGE_NAME', meson.project_name())
conf.set('PACKAGE_MAJOR', major_version)
conf.set('PACKAGE_MINOR', minor_version)

Expand Down Expand Up @@ -58,6 +59,10 @@ endif

conf.set10('WITH_STRICT', get_option('strict'))

if get_option('nls') and cc.has_header('libintl.h')
conf.set('ENABLE_NLS', 1)
endif

prefix = get_option('prefix')
datadir = get_option('datadir')
bindir = get_option('bindir')
Expand Down
21 changes: 14 additions & 7 deletions p11-kit/conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@
#include <stdlib.h>
#include <string.h>

#ifdef ENABLE_NLS
#include <libintl.h>
#define _(x) dgettext(PACKAGE_NAME, x)
#else
#define _(x) (x)
#endif

/* For testing, when the tests assuming user config are run as root. */
bool p11_conf_force_user_config = false;

Expand Down Expand Up @@ -151,11 +158,11 @@ _p11_conf_parse_file (const char* filename,
lexer.tok.field.value = NULL;
break;
case TOK_PEM:
p11_message ("%s: unexpected pem block", filename);
p11_message (_("%s: unexpected pem block"), filename);
failed = true;
break;
case TOK_SECTION:
p11_message ("%s: unexpected section header", filename);
p11_message (_("%s: unexpected section header"), filename);
failed = true;
break;
case TOK_EOF:
Expand Down Expand Up @@ -198,7 +205,7 @@ user_config_mode (p11_dict *config,
} else if (strequal (mode, "override")) {
return CONF_USER_ONLY;
} else {
p11_message ("invalid mode for 'user-config': %s", mode);
p11_message (_("invalid mode for 'user-config': %s"), mode);
return CONF_USER_INVALID;
}
}
Expand Down Expand Up @@ -357,7 +364,7 @@ load_config_from_file (const char *configfile,

key = calc_name_from_filename (name);
if (key == NULL) {
p11_message ("invalid config filename, will be ignored in the future: %s", configfile);
p11_message (_("invalid config filename, will be ignored in the future: %s"), configfile);
key = strdup (name);
return_val_if_fail (key != NULL, false);
}
Expand Down Expand Up @@ -418,7 +425,7 @@ load_configs_from_directory (const char *directory,
p11_debug ("couldn't list inacessible module configs");
return true;
}
p11_message_err (error, "couldn't list directory: %s", directory);
p11_message_err (error, _("couldn't list directory: %s"), directory);
errno = error;
return false;
}
Expand All @@ -429,7 +436,7 @@ load_configs_from_directory (const char *directory,

if (stat (path, &st) < 0) {
error = errno;
p11_message_err (error, "couldn't stat path: %s", path);
p11_message_err (error, _("couldn't stat path: %s"), path);
free (path);
break;
}
Expand Down Expand Up @@ -518,7 +525,7 @@ _p11_conf_parse_boolean (const char *string,
} else if (strcmp (string, "no") == 0) {
return false;
} else {
p11_message ("invalid setting '%s' defaulting to '%s'",
p11_message (_("invalid setting '%s' defaulting to '%s'"),
string, default_value ? "yes" : "no");
return default_value;
}
Expand Down
9 changes: 8 additions & 1 deletion p11-kit/filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@
#include <errno.h>
#include <stdarg.h>

#ifdef ENABLE_NLS
#include <libintl.h>
#define _(x) dgettext(PACKAGE_NAME, x)
#else
#define _(x) (x)
#endif

typedef struct {
CK_SLOT_ID slot;
const CK_TOKEN_INFO *token;
Expand Down Expand Up @@ -173,7 +180,7 @@ filter_reinit (FilterData *filter)
filter->initialized = true;
else {
filter->initialized = false;
p11_message ("filter cannot be initialized");
p11_message (_("filter cannot be initialized"));
}
}

Expand Down
15 changes: 11 additions & 4 deletions p11-kit/lists.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@
#include "tool.h"
#include "uri.h"

#ifdef ENABLE_NLS
#include <libintl.h>
#define _(x) dgettext(PACKAGE_NAME, x)
#else
#define _(x) (x)
#endif

int p11_kit_list_modules (int argc,
char *argv[]);

Expand Down Expand Up @@ -106,7 +113,7 @@ print_token_info (CK_FUNCTION_LIST_PTR module, CK_SLOT_ID slot_id)

rv = (module->C_GetTokenInfo) (slot_id, &info);
if (rv != CKR_OK) {
p11_message ("couldn't load module info: %s", p11_kit_strerror (rv));
p11_message (_("couldn't load module info: %s"), p11_kit_strerror (rv));
return;
}

Expand Down Expand Up @@ -173,7 +180,7 @@ print_module_info (CK_FUNCTION_LIST_PTR module)

rv = (module->C_GetInfo) (&info);
if (rv != CKR_OK) {
p11_message ("couldn't load module info: %s", p11_kit_strerror (rv));
p11_message (_("couldn't load module info: %s"), p11_kit_strerror (rv));
return;
}

Expand All @@ -194,7 +201,7 @@ print_module_info (CK_FUNCTION_LIST_PTR module)
count = sizeof (slot_list) / sizeof (slot_list[0]);
rv = (module->C_GetSlotList) (CK_TRUE, slot_list, &count);
if (rv != CKR_OK) {
p11_message ("couldn't load module info: %s", p11_kit_strerror (rv));
p11_message (_("couldn't load module info: %s"), p11_kit_strerror (rv));
return;
}

Expand Down Expand Up @@ -285,7 +292,7 @@ p11_kit_list_modules (int argc,
}

if (argc - optind != 0) {
p11_message ("extra arguments specified");
p11_message (_("extra arguments specified"));
return 2;
}

Expand Down
2 changes: 1 addition & 1 deletion p11-kit/messages.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
#include <libintl.h>
#define _(x) dgettext(PACKAGE_NAME, x)
#else
#define _(x) x
#define _(x) (x)
#endif

/**
Expand Down
Loading