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.
This commit is contained in:
Eric Blake 2010-11-16 12:01:37 -07:00
parent 0d5f54bb21
commit 981d2cdab4
7 changed files with 46 additions and 11 deletions

View 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) {

View 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

View File

@ -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)

View 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;

View 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) {

View File

@ -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;
}

View 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], '/')))