Skip to contentSkip to conversation

i18n: Add shell script translation infrastructure

PATCH In review Ævar Arnfjörð Bjarmason 2 participants 6 messages Jun 15, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover i18n: Add shell script translation infrastructure

This is v2 of the "Add shell script translation infrastructure" series. Changes:

* The Author & Documentation sections from the documentation are now gone. These should no longer be included, but the patch series preceded that convention.

* Update the git-sh-i18n.txt and git-sh-i18n--envsubst.txt documentation to accurately reflect what the skeleton fall-through wrappers do.

* Updated the commit message for "git-sh-i18n--envsubst: our own envsubst(1) for eval_gettext()" to not include a lengthy description of failed eval_gettext experiments at Junio's suggestion.

Ævar Arnfjörð Bjarmason (4):
git-sh-i18n--envsubst: our own envsubst(1) for eval_gettext() git-sh-i18n.sh: add no-op gettext() and eval_gettext() wrappers git-sh-i18n.sh: add GIT_GETTEXT_POISON support
Makefile: add xgettext target for *.sh files

 .gitignore                              |    3 +
 Documentation/git-sh-i18n--envsubst.txt |   26 ++
 Documentation/git-sh-i18n.txt           |   42 +++
 Makefile                                |    8 +-
 git-sh-i18n.sh                          |   29 ++
 sh-i18n--envsubst.c                     |  444 +++++++++++++++++++++++++++++++
 t/t0201-gettext-fallbacks.sh            |   51 ++++
 7 files changed, 602 insertions(+), 1 deletions(-)
 create mode 100644 Documentation/git-sh-i18n--envsubst.txt
 create mode 100644 Documentation/git-sh-i18n.txt
 create mode 100644 git-sh-i18n.sh
 create mode 100644 sh-i18n--envsubst.c
 create mode 100755 t/t0201-gettext-fallbacks.sh
Signature · 1 line
1.7.4.4
#1 lore raw

PATCH v2 1/4 git-sh-i18n--envsubst: our own envsubst(1) for eval_gettext()

Add a git-sh-i18n--envsubst program which is a stripped-down version of the GNU envsubst(1) program that comes with GNU gettext for use in the eval_gettext() fallback.

We need a C helper program because implementing eval_gettext() purely in shell turned out to be unworkable. Digging through the Git mailing list archives will reveal two shell implementations of eval_gettext that are almost good enough, but fail on an edge case which is tested for in the tests which are part of this patch.

These are the modifications I made to envsubst.c as I turned it into sh-i18n--envsubst.c:

* Added our git-compat-util.h header for xrealloc() and friends.

* Removed inclusion of gettext-specific headers.

* Removed most of main() and replaced it with my own. The modified version only does option parsing for --variables. That's all it needs.

* Modified error() invocations to use our error() instead of error(3).

* Replaced the gettext XNMALLOC(n, size) macro with just xmalloc(n). Since XNMALLOC() only allocated char's.

* Removed the string_list_destroy function. It's redundant (also in the upstream code).

* Replaced the use of stdbool.h (a C99 header) by doing the following replacements on the code:

* s/bool/unsigned short int/g
* s/true/1/g
* s/false/0/g

---
 .gitignore                              |    2 +
 Documentation/git-sh-i18n--envsubst.txt |   26 ++
 Makefile                                |    1 +
 sh-i18n--envsubst.c                     |  444 +++++++++++++++++++++++++++++++
 t/t0201-gettext-fallbacks.sh            |   51 ++++
 5 files changed, 524 insertions(+), 0 deletions(-)
 create mode 100644 Documentation/git-sh-i18n--envsubst.txt
 create mode 100644 sh-i18n--envsubst.c
 create mode 100755 t/t0201-gettext-fallbacks.sh
.gitignore +2 -0
index 711fce7..1ccf797 100644
@@ -127,6 +127,8 @@
 /git-rm
 /git-send-email
 /git-send-pack
+/git-sh-i18n
+/git-sh-i18n--envsubst
 /git-sh-setup
 /git-shell
 /git-shortlog
Documentation/git-sh-i18n--envsubst.txt new file +26 -0
new file mode 100644
index 0000000..f5bbf77
@@ -0,0 +1,26 @@
+git-sh-i18n--envsubst(1)
+========================
+
+NAME
+----
+git-sh-i18n--envsubst - Git's own envsubst(1) for i18n fallbacks
+
+DESCRIPTION
+-----------
+
+This is not a command the end user would want to run.  Ever.
+This documentation is meant for people who are studying the
+plumbing scripts and/or are writing new ones.
+
+git-sh-i18n--envsubst is Git's stripped-down copy of the GNU
+`envsubst(1)` program that comes with the GNU gettext package. It's
+used internally by linkgit:git-sh-i18n[1] to interpolate the variables
+passed to the the `eval_gettext` function.
+
+No promises are made about the interface, or that this
+program won't disappear without warning in the next version
+of Git. Don't use it.
+
+GIT
+---
+Part of the linkgit:git[1] suite
Makefile +1 -0
index 320ccc7..56944c6 100644
@@ -414,6 +414,7 @@ PROGRAM_OBJS += shell.o
 PROGRAM_OBJS += show-index.o
 PROGRAM_OBJS += upload-pack.o
 PROGRAM_OBJS += http-backend.o
+PROGRAM_OBJS += sh-i18n--envsubst.o
 
 PROGRAMS += $(patsubst %.o,git-%$X,$(PROGRAM_OBJS))
 
sh-i18n--envsubst.c new file +444 -0

444 lines not on this page · raw message

t/t0201-gettext-fallbacks.sh new file +51 -0

51 lines not on this page · raw message

Signature · 1 line
1.7.4.4
#2 lore raw

Re: [PATCH v2 1/4] git-sh-i18n--envsubst: our own envsubst(1) for eval_gettext()

Review

Quoted 2 lines · Ævar Arnfjörð Bjarmason
(quoted diff) +2 -0
+static int
+do_getc ()

do_getc (void)

sh-i18n--envsubst.c line 333

Quoted 3 lines
sh-i18n--envsubst.c new file +3 -0
+/* Copies stdin to stdout, performing substitutions.  */
+static void
+subst_from_stdin ()

subst_from_stdin (void)

Will update these again and queue.
Thanks.

#3 lore raw

PATCH v2 2/4 git-sh-i18n.sh: add no-op gettext() and eval_gettext() wrappers

In reply to Ævar Arnfjörð Bjarmason

Add a no-op wrapper library for Git's shell scripts. To split up the gettext series I'm first submitting patches to gettextize the source tree before I add any of the Makefile and Shell library changes needed to actually use them.

---
 .gitignore                    |    1 +
 Documentation/git-sh-i18n.txt |   42 +++++++++++++++++++++++++++++++++++++++++
 Makefile                      |    1 +
 git-sh-i18n.sh                |   17 ++++++++++++++++
 4 files changed, 61 insertions(+), 0 deletions(-)
 create mode 100644 Documentation/git-sh-i18n.txt
 create mode 100644 git-sh-i18n.sh
.gitignore +1 -0
index 1ccf797..4bdb839 100644
@@ -130,6 +130,7 @@
 /git-sh-i18n
 /git-sh-i18n--envsubst
 /git-sh-setup
+/git-sh-i18n
 /git-shell
 /git-shortlog
 /git-show
Documentation/git-sh-i18n.txt new file +42 -0

42 lines not on this page · raw message

Makefile +1 -0
index 56944c6..854fe69 100644
@@ -381,6 +381,7 @@ SCRIPT_LIB += git-rebase--am
 SCRIPT_LIB += git-rebase--interactive
 SCRIPT_LIB += git-rebase--merge
 SCRIPT_LIB += git-sh-setup
+SCRIPT_LIB += git-sh-i18n
 
 SCRIPT_PERL += git-add--interactive.perl
 SCRIPT_PERL += git-difftool.perl
git-sh-i18n.sh new file +17 -0
new file mode 100644
index 0000000..ea05e16
@@ -0,0 +1,17 @@
+#!/bin/sh
+#
+# Copyright (c) 2010 Ævar Arnfjörð Bjarmason
+#
+# This is a skeleton no-op implementation of gettext for Git. It'll be
+# replaced by something that uses gettext.sh in a future patch series.
+
+gettext () {
+	printf "%s" "$1"
+}
+
+eval_gettext () {
+	printf "%s" "$1" | (
+		export PATH $(git sh-i18n--envsubst --variables "$1");
+		git sh-i18n--envsubst "$1"
+	)
+}
Signature · 1 line
1.7.4.4
#4 lore raw

PATCH v2 3/4 git-sh-i18n.sh: add GIT_GETTEXT_POISON support

In reply to Ævar Arnfjörð Bjarmason

Change git-sh-i18n.sh to support the GIT_GETTEXT_POISON environment variable like gettext.c does, this ensures that tests that use git-sh-i18n.sh will fail under GETTEXT_POISON=YesPlease if they rely on Git's C locale messages without declaring that they do.

---
 git-sh-i18n.sh |   30 +++++++++++++++++++++---------
 1 files changed, 21 insertions(+), 9 deletions(-)
git-sh-i18n.sh +21 -9
index ea05e16..32ca59d 100644
@@ -5,13 +5,25 @@
 # This is a skeleton no-op implementation of gettext for Git. It'll be
 # replaced by something that uses gettext.sh in a future patch series.
 
-gettext () {
-	printf "%s" "$1"
-}
+if test -z "$GIT_GETTEXT_POISON"
+then
+	gettext () {
+		printf "%s" "$1"
+	}
+
+	eval_gettext () {
+		printf "%s" "$1" | (
+			export PATH $(git sh-i18n--envsubst --variables "$1");
+			git sh-i18n--envsubst "$1"
+		)
+	}
+else
+	gettext () {
+		printf "%s" "# GETTEXT POISON #"
+	}
+
+	eval_gettext () {
+		printf "%s" "# GETTEXT POISON #"
+	}
+fi
 
-eval_gettext () {
-	printf "%s" "$1" | (
-		export PATH $(git sh-i18n--envsubst --variables "$1");
-		git sh-i18n--envsubst "$1"
-	)
-}
Signature · 1 line
1.7.4.4
#5 lore raw

PATCH v2 4/4 Makefile: add xgettext target for *.sh files

In reply to Ævar Arnfjörð Bjarmason

Change the "pot" target to also extract strings from our $(SCRIPT_SH) files with xgettext(1).

Note that due to Jonathan Nieder's trick of doing "mv $@+ $@" at the end of the target the "pot" target will now warn:

    $ make pot
        XGETTEXT po/git.pot
    po/git.pot+: warning: Charset "CHARSET" is not a portable encoding name.
                          Message conversion to user's charset might not work.

This warnings is emitted because xgettext is writing into a non-*.pot file, it's harmless however. The content that's written out is equivalent to what it would be if we were writing directly into an existing POT file with --join-existing.

As part of this change I've eliminated the && chain between xgettext calls, this is incompatible with $(QUIET_XGETTEXT), if the && is left in it'll emit:

/bin/sh: @echo: not found

Since it's redundant (the Makefile will stop if there's an error) I've removed it altogether.

---
 Makefile |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
Makefile +5 -1
index 854fe69..1b1a7e8 100644
@@ -2039,10 +2039,14 @@ XGETTEXT_FLAGS = \
 	--from-code=UTF-8
 XGETTEXT_FLAGS_C = $(XGETTEXT_FLAGS) --language=C \
 	--keyword=_ --keyword=N_ --keyword="Q_:1,2"
+XGETTEXT_FLAGS_SH = $(XGETTEXT_FLAGS) --language=Shell
 LOCALIZED_C := $(C_OBJ:o=c)
+LOCALIZED_SH := $(SCRIPT_SH)
 
 po/git.pot: $(LOCALIZED_C)
-	$(QUIET_XGETTEXT)$(XGETTEXT) -o$@+ $(XGETTEXT_FLAGS_C) $(LOCALIZED_C) && \
+	$(QUIET_XGETTEXT)$(XGETTEXT) -o$@+ $(XGETTEXT_FLAGS_C) $(LOCALIZED_C)
+	$(QUIET_XGETTEXT)$(XGETTEXT) -o$@+ --join-existing $(XGETTEXT_FLAGS_SH) \
+		$(LOCALIZED_SH)
 	mv $@+ $@
 
 pot: po/git.pot
Signature · 1 line
1.7.4.4