From c4477870d918ecfea32fa59cbed0066f9cba0d0d Mon Sep 17 00:00:00 2001
From: Daiki Ueno <ueno@gnu.org>
Date: Fri, 5 Mar 2021 08:26:02 +0100
Subject: [PATCH] common: Enable message translation in p11_tool_main

---
 common/tool.c | 45 ++++++++++++++++++++++++++++++++++++---------
 1 file changed, 36 insertions(+), 9 deletions(-)

diff --git a/common/tool.c b/common/tool.c
index 414f852d..5bd2a956 100644
--- a/common/tool.c
+++ b/common/tool.c
@@ -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
@@ -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
@@ -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 ();
 
@@ -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;
@@ -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;
 			}
 
@@ -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;
@@ -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;
 		}
 	}
@@ -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;
 }
