]> git.draconx.ca Git - cdecl99.git/commitdiff
Add support for translated long options.
authorNick Bowler <nbowler@draconx.ca>
Tue, 4 Oct 2011 22:14:05 +0000 (18:14 -0400)
committerNick Bowler <nbowler@draconx.ca>
Sun, 23 Oct 2011 00:20:58 +0000 (20:20 -0400)
Makefile.am
cfgtail.h [new file with mode: 0644]
configure.ac
src/cdecl99.c

index 0632878dbf2f01225159fb41df0f26816bd35567..c802b2d040ce8abe16aafdeffe6c9d027243e203 100644 (file)
@@ -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 (file)
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
index 85766287b461b6b27b994c1e0a455dee90d04678..dcb55043595271729efff47c697121f8d311ef3b 100644 (file)
@@ -39,6 +39,8 @@ BISON_I18N
 GLOBAL_SYMBOL_PIPE=$lt_cv_sys_global_symbol_pipe
 AC_SUBST([GLOBAL_SYMBOL_PIPE])
 
+AH_BOTTOM([#include <cfgtail.h>])
+
 AC_CONFIG_FILES([
        exported.sh
        Makefile
index c5f913bd12d1fd0a442bdc0f2aa82def63d3a5cf..c70834fbb98eeaa015bdff0caa73adda63793fbf 100644 (file)
 #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) {