From: Nick Bowler Date: Tue, 4 Oct 2011 22:14:05 +0000 (-0400) Subject: Add support for translated long options. X-Git-Tag: v1~85 X-Git-Url: https://git.draconx.ca/gitweb/cdecl99.git/commitdiff_plain/87278972ae64ef4c5eff110af1565b1988160fb4 Add support for translated long options. --- diff --git a/Makefile.am b/Makefile.am index 0632878..c802b2d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -77,7 +77,7 @@ XGETTEXT_OPTS = -D $(builddir) -D $(srcdir) --from-code=utf-8 \ --add-comments=TRANSLATORS: --foreign-user \ --package-name=$(PACKAGE) --package-version=$(PACKAGE_VERSION) \ --msgid-bugs-address=$(PACKAGE_BUGREPORT) \ - --keyword=_ --keyword=N_ + --keyword=_ --keyword=N_ --keyword=PN_:1c,2 po/$(PACKAGE).pot: $(SOURCES) src/errors.lst $(AM_V_at) $(MKDIR_P) $(@D) diff --git a/cfgtail.h b/cfgtail.h new file mode 100644 index 0000000..6368d76 --- /dev/null +++ b/cfgtail.h @@ -0,0 +1,4 @@ +/* Allow ENABLE_NLS to be used in C conditionals. */ +#ifndef ENABLE_NLS +# define ENABLE_NLS 0 +#endif diff --git a/configure.ac b/configure.ac index 8576628..dcb5504 100644 --- a/configure.ac +++ b/configure.ac @@ -39,6 +39,8 @@ BISON_I18N GLOBAL_SYMBOL_PIPE=$lt_cv_sys_global_symbol_pipe AC_SUBST([GLOBAL_SYMBOL_PIPE]) +AH_BOTTOM([#include ]) + AC_CONFIG_FILES([ exported.sh Makefile diff --git a/src/cdecl99.c b/src/cdecl99.c index c5f913b..c70834f 100644 --- a/src/cdecl99.c +++ b/src/cdecl99.c @@ -27,18 +27,26 @@ #include "readline.h" #include "cdecl.h" +#define N_(x) x +#define PN_(c, x) x + static const char *progname = "cdecl99"; static const char sopts[] = "qbif:e:VH"; -static const struct option lopts[] = { - { "quiet", 0, NULL, 'q' }, - { "batch", 0, NULL, 'b' }, - { "interactive", 0, NULL, 'i' }, - { "file", 1, NULL, 'f' }, - { "execute", 1, NULL, 'e' }, - { "version", 0, NULL, 'V' }, - { "help", 0, NULL, 'H' }, - { 0 } -}; +#define RAW_LOPTS \ + { PN_("longopt", "quiet"), 0, NULL, 'q' }, \ + { PN_("longopt", "batch"), 0, NULL, 'b' }, \ + { PN_("longopt", "interactive"), 0, NULL, 'i' }, \ + { PN_("longopt", "file"), 1, NULL, 'f' }, \ + { PN_("longopt", "execute"), 1, NULL, 'e' }, \ + { PN_("longopt", "version"), 0, NULL, 'V' }, \ + { PN_("longopt", "help"), 0, NULL, 'H' } + +/* + * With NLS, we need a buffer big enough to store the translated options. + * The translations will be filled in at program startup. + */ +enum { NOPTS = sizeof (struct option[]){RAW_LOPTS} / sizeof (struct option) }; +static struct option lopts[NOPTS + !!ENABLE_NLS * NOPTS + 1] = { RAW_LOPTS }; static void print_version(void) { @@ -322,6 +330,22 @@ static int repl_noninteractive(void) return ret; } +/* Initialize gettext and setup translated long options. */ +static void init_i18n(void) +{ + setlocale(LC_ALL, ""); + bindtextdomain(PACKAGE, LOCALEDIR); + textdomain(PACKAGE); + + if (!ENABLE_NLS) + return; + + for (int i = 0; i < NOPTS; i++) { + lopts[i+NOPTS] = lopts[i]; + lopts[i+NOPTS].name = pgettext_expr("longopt", lopts[i].name); + } +} + int main(int argc, char **argv) { bool show_intro = true, interactive = true, execute = false; @@ -331,10 +355,7 @@ int main(int argc, char **argv) if (argc > 0) progname = argv[0]; - /* Initialize gettext. */ - setlocale(LC_ALL, ""); - bindtextdomain(PACKAGE, LOCALEDIR); - textdomain(PACKAGE); + init_i18n(); while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) { switch (opt) {