libfdisk: fix NLS support

The current libfdisk code uses gettext() to translate strings. It
means it follows the default text domain (as set by textdomain(3)
usually in the main program). This is useless for public shared
library.

We have call private bindtextdomain() and use dgettext() with private
domain name to be independent on the main program. For this purpose
include/nls.h supports UL_TEXTDOMAIN_EXPLICIT to use dgettext().

Note that libfdisk will continue to use util-linux.po, rather than
keep the texts in the separate file.

The nls.h has to be included only from fdiskP.h to be sure that
nls.h works as expected for the library.

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2017-04-25 17:05:09 +02:00
parent be685b98c0
commit 7095232d21
9 changed files with 26 additions and 9 deletions

View File

@ -20,9 +20,19 @@ struct lconv
# define localeconv() NULL
#endif
#ifdef ENABLE_NLS
# include <libintl.h>
# define _(Text) gettext (Text)
/*
* For NLS support in the public shared libraries we have to specify text
* domain name to be independend on the main program. For this purpose define
* UL_TEXTDOMAIN_EXPLICIT before you include nls.h to your shared library code.
*/
# ifdef UL_TEXTDOMAIN_EXPLICIT
# define _(Text) dgettext (UL_TEXTDOMAIN_EXPLICIT, Text)
# else
# define _(Text) gettext (Text)
# endif
# ifdef gettext_noop
# define N_(String) gettext_noop (String)
# else
@ -37,7 +47,7 @@ struct lconv
# define _(Text) (Text)
# define N_(Text) (Text)
# define P_(Singular, Plural, n) ((n) == 1 ? (Singular) : (Plural))
#endif
#endif /* ENABLE_NLS */
#ifdef HAVE_LANGINFO_H
# include <langinfo.h>

View File

@ -16,7 +16,6 @@
#include <errno.h>
#include <sys/param.h>
#include "nls.h"
#include "blkdev.h"
#include "fdiskP.h"
#include "pt-mbr.h"

View File

@ -61,6 +61,8 @@ struct fdisk_context *fdisk_new_context(void)
cxt->labels[ cxt->nlabels++ ] = fdisk_new_sgi_label(cxt);
cxt->labels[ cxt->nlabels++ ] = fdisk_new_sun_label(cxt);
bindtextdomain(LIBFDISK_TEXTDOMAIN, LOCALEDIR);
return cxt;
}

View File

@ -7,7 +7,6 @@
* from util-linux fdisk.
*/
#include "c.h"
#include "nls.h"
#include "randutils.h"
#include "pt-mbr.h"
#include "strutils.h"

View File

@ -20,7 +20,6 @@
#include "c.h"
#include "libfdisk.h"
#include "nls.h" /* temporary before dialog API will be implemented */
#include "list.h"
#include "debug.h"
#include <stdio.h>
@ -47,6 +46,18 @@ UL_DEBUG_DECLARE_MASK(libfdisk);
#define ON_DBG(m, x) __UL_DBG_CALL(libfdisk, LIBFDISK_DEBUG_, m, x)
#define DBG_FLUSH __UL_DBG_FLUSH(libfdisk, LIBFDISK_DEBUG_)
/*
* NLS -- the library has to be independent on main program, so define
* UL_TEXTDOMAIN_EXPLICIT before you include nls.h.
*
* Now we use util-linux.po (=PACKAGE), rather than maintain the texts
* in the separate libfdisk.po file.
*/
#define LIBFDISK_TEXTDOMAIN PACKAGE
#define UL_TEXTDOMAIN_EXPLICIT LIBFDISK_TEXTDOMAIN
#include "nls.h"
#ifdef TEST_PROGRAM
struct fdisk_test {
const char *name;

View File

@ -21,7 +21,6 @@
#include "fdiskP.h"
#include "nls.h"
#include "crc32.h"
#include "blkdev.h"
#include "bitops.h"

View File

@ -1,7 +1,6 @@
#include <ctype.h>
#include "nls.h"
#include "fdiskP.h"
/**

View File

@ -12,7 +12,6 @@
*/
#include "c.h"
#include "nls.h"
#include "all-io.h"
#include "blkdev.h"

View File

@ -12,7 +12,6 @@
#include <unistd.h> /* write */
#include <sys/ioctl.h> /* ioctl */
#include "nls.h"
#include "blkdev.h"
#include "bitops.h"