From: Eric Blake Date: Tue, 16 Nov 2010 19:01:37 +0000 (-0700) Subject: maint: improve i18n on non-Linux X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=981d2cdab41edf2f4b7f6e1164336b85a02708cc;p=libvirt.git maint: improve i18n on non-Linux 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. --- diff --git a/daemon/libvirtd.c b/daemon/libvirtd.c index dcd9f24754..1673e91d67 100644 --- a/daemon/libvirtd.c +++ b/daemon/libvirtd.c @@ -47,6 +47,7 @@ #include #include #include +#include #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 ed3a151c4e..2820ac1871 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 a98daa3180..8473c3c2a2 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 478f0d1ff5..af0b70ceb7 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 #include #include +#include #if HAVE_CAPNG # include @@ -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/virt-aa-helper.c b/src/security/virt-aa-helper.c index 0f94fe4eca..3b13298b98 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 2a70250115..6ef413d963 100644 --- a/src/storage/parthelper.c +++ b/src/storage/parthelper.c @@ -39,9 +39,11 @@ #include #include #include +#include #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 3f64dd32bd..4ef556a70f 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], '/')))