[libvirt] [PATCH 1/3] maint: improve i18n on non-Linux

Eric Blake eblake at redhat.com
Tue Nov 16 22:35:16 UTC 2010


Per the gettext developer:
http://lists.gnu.org/archive/html/bug-gnu-utils/2010-10/msg00019.html
http://lists.gnu.org/archive/html/bug-gnu-utils/2010-10/msg00021.html

gettext() doesn't work correctly on all platforms unless you have
called setlocale().  Furthermore, gnulib's gettext.h has provisions
for setting up a default locale, which is the preferred method for
libraries to use gettext without having to call textdomain() and
override the main program's default domain (virInitialize already
calls bindtextdomain(), but this is insufficient without the
setlocale() added in this patch; and a redundant bindtextdomain()
in this patch doesn't hurt, but serves as a good example for other
packages that need to bind a second translation domain).

This patch is needed to silence a new gnulib 'make syntax-check'
rule in the next patch.

* daemon/libvirtd.c (main): Setup locale and gettext.
* src/lxc/lxc_controller.c (main): Likewise.
* src/security/virt-aa-helper.c (main): Likewise.
* src/storage/parthelper.c (main): Likewise.
* tools/virsh.c (main): Fix exit status.
* src/internal.h (DEFAULT_TEXT_DOMAIN): Define, for gettext.h.
(_): Simplify definition accordingly.
* po/POTFILES.in: Add src/storage/parthelper.c.
---

 daemon/libvirtd.c                |   10 +++++++---
 po/POTFILES.in                   |    1 +
 src/internal.h                   |   11 ++++++++---
 src/lxc/lxc_controller.c         |    9 +++++++++
 src/security/security_apparmor.c |    1 +
 src/security/virt-aa-helper.c    |    7 +++++++
 src/storage/parthelper.c         |   15 ++++++++++++---
 tools/virsh.c                    |    4 ++--
 8 files changed, 47 insertions(+), 11 deletions(-)

diff --git a/daemon/libvirtd.c b/daemon/libvirtd.c
index aab7667..c43fc67 100644
--- a/daemon/libvirtd.c
+++ b/daemon/libvirtd.c
@@ -47,6 +47,7 @@
 #include <grp.h>
 #include <signal.h>
 #include <netdb.h>
+#include <locale.h>

 #include "libvirt_internal.h"
 #include "virterror_internal.h"
@@ -3076,9 +3077,12 @@ int main(int argc, char **argv) {
         {0, 0, 0, 0}
     };

-    if (virInitialize() < 0) {
-        fprintf (stderr, _("%s: initialization failed\n"), argv0);
-        exit (EXIT_FAILURE);
+    if (setlocale (LC_ALL, "") == NULL ||
+        bindtextdomain (PACKAGE, LOCALEDIR) == NULL ||
+        textdomain(PACKAGE) == NULL ||
+        virInitialize() < 0) {
+        fprintf(stderr, _("%s: initialization failed\n"), argv0);
+        exit(EXIT_FAILURE);
     }

     while (1) {
diff --git a/po/POTFILES.in b/po/POTFILES.in
index ed3a151..2820ac1 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -63,6 +63,7 @@ src/security/security_apparmor.c
 src/security/security_driver.c
 src/security/security_selinux.c
 src/security/virt-aa-helper.c
+src/storage/parthelper.c
 src/storage/storage_backend.c
 src/storage/storage_backend_disk.c
 src/storage/storage_backend_fs.c
diff --git a/src/internal.h b/src/internal.h
index a98daa3..8473c3c 100644
--- a/src/internal.h
+++ b/src/internal.h
@@ -27,7 +27,15 @@
  */
 # define VIR_DEPRECATED /*empty*/

+/* All uses of _() within the library should pick up translations from
+ * libvirt's message files, rather than from the package that is
+ * linking in the library.  Setting this macro before including
+ * "gettext.h" means that gettext() (and _()) will properly expand to
+ * dgettext.  */
+# define DEFAULT_TEXT_DOMAIN PACKAGE
 # include "gettext.h"
+# define _(str) gettext(str)
+# define N_(str) str

 # include "libvirt/libvirt.h"
 # include "libvirt/virterror.h"
@@ -52,9 +60,6 @@
 #  define INET_ADDRSTRLEN 16
 # endif

-# define _(str) dgettext(PACKAGE, (str))
-# define N_(str) str
-
 /* String equality tests, suggested by Jim Meyering. */
 # define STREQ(a,b) (strcmp(a,b) == 0)
 # define STRCASEEQ(a,b) (strcasecmp(a,b) == 0)
diff --git a/src/lxc/lxc_controller.c b/src/lxc/lxc_controller.c
index 478f0d1..af0b70c 100644
--- a/src/lxc/lxc_controller.c
+++ b/src/lxc/lxc_controller.c
@@ -1,4 +1,5 @@
 /*
+ * Copyright (C) 2010 Red Hat, Inc.
  * Copyright IBM Corp. 2008
  *
  * lxc_controller.c: linux container process controller
@@ -34,6 +35,7 @@
 #include <signal.h>
 #include <getopt.h>
 #include <sys/mount.h>
+#include <locale.h>

 #if HAVE_CAPNG
 # include <cap-ng.h>
@@ -717,6 +719,13 @@ int main(int argc, char *argv[])
         { 0, 0, 0, 0 },
     };

+    if (setlocale(LC_ALL, "") == NULL ||
+        bindtextdomain(PACKAGE, LOCALEDIR) == NULL ||
+        textdomain(PACKAGE) == NULL) {
+        fprintf(stderr, _("%s: initialization failed\n"), argv[0]);
+        exit(EXIT_FAILURE);
+    }
+
     while (1) {
         int c;

diff --git a/src/security/security_apparmor.c b/src/security/security_apparmor.c
index b43c4ac..f172cfd 100644
--- a/src/security/security_apparmor.c
+++ b/src/security/security_apparmor.c
@@ -25,6 +25,7 @@
 #include <unistd.h>
 #include <wait.h>
 #include <stdbool.h>
+#include <locale.h>

 #include "internal.h"

diff --git a/src/security/virt-aa-helper.c b/src/security/virt-aa-helper.c
index 0f94fe4..3b13298 100644
--- a/src/security/virt-aa-helper.c
+++ b/src/security/virt-aa-helper.c
@@ -1134,6 +1134,13 @@ main(int argc, char **argv)
     char profile[PATH_MAX];
     char include_file[PATH_MAX];

+    if (setlocale(LC_ALL, "") == NULL ||
+        bindtextdomain(PACKAGE, LOCALEDIR) == NULL ||
+        textdomain(PACKAGE) == NULL) {
+        fprintf(stderr, _("%s: initialization failed\n"), argv0);
+        exit(EXIT_FAILURE);
+    }
+
     /* clear the environment */
     environ = NULL;
     if (setenv("PATH", "/sbin:/usr/sbin", 1) != 0) {
diff --git a/src/storage/parthelper.c b/src/storage/parthelper.c
index 2a70250..6ef413d 100644
--- a/src/storage/parthelper.c
+++ b/src/storage/parthelper.c
@@ -39,9 +39,11 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
+#include <locale.h>

 #include "util.h"
 #include "c-ctype.h"
+#include "configmake.h"

 /* we don't need to include the full internal.h just for this */
 #define STREQ(a,b) (strcmp(a,b) == 0)
@@ -79,10 +81,17 @@ int main(int argc, char **argv)
     char *canonical_path;
     const char *partsep;

+    if (setlocale(LC_ALL, "") == NULL ||
+        bindtextdomain(PACKAGE, LOCALEDIR) == NULL ||
+        textdomain(PACKAGE) == NULL) {
+        fprintf(stderr, _("%s: initialization failed\n"), argv[0]);
+        exit(EXIT_FAILURE);
+    }
+
     if (argc == 3 && STREQ(argv[2], "-g")) {
         cmd = DISK_GEOMETRY;
     } else if (argc != 2) {
-        fprintf(stderr, "syntax: %s DEVICE [-g]\n", argv[0]);
+        fprintf(stderr, _("syntax: %s DEVICE [-g]\n"), argv[0]);
         return 1;
     }

@@ -103,7 +112,7 @@ int main(int argc, char **argv)
     }

     if ((dev = ped_device_get(path)) == NULL) {
-        fprintf(stderr, "unable to access device %s\n", path);
+        fprintf(stderr, _("unable to access device %s\n"), path);
         return 2;
     }

@@ -117,7 +126,7 @@ int main(int argc, char **argv)
     }

     if ((disk = ped_disk_new(dev)) == NULL) {
-        fprintf(stderr, "unable to access disk %s\n", argv[1]);
+        fprintf(stderr, _("unable to access disk %s\n"), argv[1]);
         return 2;
     }

diff --git a/tools/virsh.c b/tools/virsh.c
index 743d5a1..36e74e5 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -11789,11 +11789,11 @@ main(int argc, char **argv)
     }
     if (!bindtextdomain(PACKAGE, LOCALEDIR)) {
         perror("bindtextdomain");
-        return -1;
+        return EXIT_FAILURE;
     }
     if (!textdomain(PACKAGE)) {
         perror("textdomain");
-        return -1;
+        return EXIT_FAILURE;
     }

     if (!(progname = strrchr(argv[0], '/')))
-- 
1.7.3.2




More information about the libvir-list mailing list