]> xenbits.xensource.com Git - libvirt.git/commitdiff
maint: improve i18n on non-Linux
authorEric Blake <eblake@redhat.com>
Tue, 16 Nov 2010 19:01:37 +0000 (12:01 -0700)
committerEric Blake <eblake@redhat.com>
Wed, 17 Nov 2010 17:12:57 +0000 (10:12 -0700)
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
po/POTFILES.in
src/internal.h
src/lxc/lxc_controller.c
src/security/virt-aa-helper.c
src/storage/parthelper.c
tools/virsh.c

index dcd9f247541545a76a095fc1bf3c1279c4f48134..1673e91d6720eae488a42af4f8f8fb9d89c64d6f 100644 (file)
@@ -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) {
index ed3a151c4e76551e9f6531e1f4e37cf3326815dd..2820ac1871b42afec4d3241ca71d7dce028e4d6c 100644 (file)
@@ -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
index a98daa3180ecc976e55aa19ac85a41ee8b16e69a..8473c3c2a29ee8004bcb50ea8c3f03e7b86113b5 100644 (file)
  */
 # 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)
index 478f0d1ff5510bd8b9a9112938d54d3f3fef7aed..af0b70ceb788eb4497a94282b97e1d956c79f47d 100644 (file)
@@ -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;
 
index 0f94fe4eca3b7068569b131e2c4311c4a320a76c..3b13298b9803c75553af5e4d06a8e90097abdb4a 100644 (file)
@@ -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) {
index 2a70250115990611408eb5fbad95917f085a1977..6ef413d963293b33b499cf0af4bf2add3b15c6a3 100644 (file)
 #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;
     }
 
index 3f64dd32bd8a319b743fff7e416ccfabe5727ac5..4ef556a70f9ad0fa296e163992c3623f25ca568f 100644 (file)
@@ -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], '/')))