i18n: Shell script translation infrastructure
Subsystems (2)
cover i18n: Shell script translation infrastructure
Now that the C gettext infrastructure patches have made it to master I'm submitting a series to do the same for our shell scripts. These are the first two patches from:
git://github.com/avar/git.git ab/i18n-sh-only
The rest (to be submitted soon) are the patches marking up the shellscripts themselves for translation.
Ævar Arnfjörð Bjarmason (2):
git-sh-i18n--envsubst: our own envsubst(1) for eval_gettext() git-sh-i18n.sh: add no-op gettext() and eval_gettext() wrappers
.gitignore | 3 + Documentation/git-sh-i18n--envsubst.txt | 36 +++ Documentation/git-sh-i18n.txt | 57 ++++ Makefile | 2 + git-sh-i18n.sh | 17 ++ sh-i18n--envsubst.c | 444 +++++++++++++++++++++++++++++++ t/t0201-gettext-fallbacks.sh | 51 ++++ 7 files changed, 610 insertions(+), 0 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.1
PATCH 1/2 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, instead of using a clever (but broken) printf + eval + printf trick.
In a previous incarnation of the gettext series I implemented the eval_gettext() fallback like this:
eval_gettext() {
gettext_out=$(gettext "$1")
gettext_eval="printf '%s' \"$gettext_out\""
printf "%s" "`eval \"$gettext_eval\"`"
}This was clever, but would incorrectly handle cases where the variable being interpolated contained spaces. E.g.:
cmd="git foo"; eval_gettext "command: \$cmd"
Would emit "command: gitfoo", instead of the correct "command: git foo". This happened with a message in git-am.sh that used the $cmdline variable. An alternate version suggested by Junio is able to handle this case:
eval_gettext() {
gettext_out=$(gettext "$1") &&
gettext_eval="printf '%s' \"$gettext_out\"" &&
gettext_cmd=$(eval "$gettext_eval") &&
printf "%s" "$gettext_cmd"
}But it strips out double-quotes when they're included in the message being passed to eval_gettext. E.g. the message in git-am.sh:
"When you have resolved this problem run \"$cmdline --resolved\"."
Will be emitted as:
When you have resolved this problem run git am --resolved.
Instead of the correct:
When you have resolved this problem run "git am --resolved".
To work around this, and to improve our variable expansion behavior (eval has security issues) I've imported a stripped-down version of gettext's envsubst(1) program. With it eval_gettext() is implemented like this:
eval_gettext() {
printf "%s" "$1" | (
export PATH $(git sh-i18n--envsubst --variables "$1");
git sh-i18n--envsubst "$1"
)
}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 | 36 +++ Makefile | 1 + sh-i18n--envsubst.c | 444 +++++++++++++++++++++++++++++++ t/t0201-gettext-fallbacks.sh | 51 ++++ 5 files changed, 534 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
@@ -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 +36 -0
@@ -0,0 +1,36 @@ +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 +provide the `eval_gettext` fallback on Solaris and systems +that don't have a gettext implementation. + +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. + +Author +------ +Written by Ævar Arnfjörð Bjarmason <[email protected]> + +Documentation +-------------- +Documentation by Ævar Arnfjörð Bjarmason and the git-list +<[email protected]>. + +GIT +--- +Part of the linkgit:git[1] suite
Makefile +1 -0
@@ -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.1
Re: [PATCH 1/2] git-sh-i18n--envsubst: our own envsubst(1) for eval_gettext()
Quoted 8 lines · Ævar Arnfjörð Bjarmason
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, instead of using a clever (but broken) printf + eval + printf trick.In a previous incarnation of the gettext series I implemented the
eval_gettext()fallback like this:
Quoted 7 lines
eval_gettext() { gettext_out=$(gettext "$1") gettext_eval="printf '%s' \"$gettext_out\"" printf "%s" "`eval \"$gettext_eval\"`" }This was clever,...
The incoming string to this function is an arbitrary string and it is asked to interpolate variables $var in it, without any other restriction in the string. If we want to interpolate $var using shell eval, we have to somehow put it in the dq context, but all other interpolation done inside dq context would also be applied to the piece of the string by the eval, including backslashes, $(command substitution--horrors), etc., which we would have needed to add more quotes to selected places in the string (read: parse and find what the shell would interpolate ourselves) to prevent them from happening.
In hindsight, it should have been clear to all of us from day one that shell eval based approach would have never worked.
I suspect that the use of envsubst (especially the part that gives the string itself to envsubst) is not quite right. What if the message wanted to say something like this?
Look at $HOME/.gitconfig and find definition for $var variable.
where we want to show $HOME _literally_, while substituting $var for the name of the variable? The underlying envsubst allows you to do so by telling it which the variables are to be substituted.
s='Look at $HOME/.gitconfig and find definition of $var variable.'
HOME=/home/junio
var=core.i18n
(
export HOME var ;# no need for HOME, but just an illustration
echo "$s" | envsubst '$var'
)but eval_gettext interface doesn't seem to be expressive enough to do this; you would let it scan the whole "$s" and end up substituting $HOME, no?
Perhaps it is not an issue in real life; after all eval_gettext interface is not something you invented in 5 minutes without thinking deeply, but is what you plan to use from outside codebase that presumably has seen a wide use in the field, and it may be just that I am worried too much about an implausible corner case, but I thought I should bring it up.
Thanks.
Re: [PATCH 1/2] git-sh-i18n--envsubst: our own envsubst(1) for eval_gettext()
Quoted 5 lines · Junio C Hamano
Perhaps it is not an issue in real life; after all
eval_gettextinterface is not something you invented in 5 minutes without thinking deeply, but is what you plan to use from outside codebase that presumably has seen a wide use in the field, and it may be just that I am worried too much about an implausible corner case, but I thought I should bring it up.
It's a corner case, but if we run into it we can easily solve it like this:
#!/bin/sh
. gettext.sh
HOME=/home/junio
path='$HOME/.gitconfig'
var=core.i18n
(
export HOME var ;# no need for HOME, but just an illustration
# TRANSLATORS: $path will be "$HOME/.gitconfig"
eval_gettext 'Look at $path and find definition of $var variable.'; echo
)Which results in:
$ sh /tmp/foo.sh
Look at $HOME/.gitconfig and find definition of core.i18n variable.PATCH 2/2 git-sh-i18n.sh: add no-op gettext() and eval_gettext() wrappers
In reply to Ævar Arnfjörð BjarmasonAdd 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 | 57 +++++++++++++++++++++++++++++++++++++++++ Makefile | 1 + git-sh-i18n.sh | 17 ++++++++++++ 4 files changed, 76 insertions(+), 0 deletions(-) create mode 100644 Documentation/git-sh-i18n.txt create mode 100644 git-sh-i18n.sh
.gitignore +1 -0
@@ -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 +57 -0
57 lines not on this page · raw message
Makefile +1 -0
@@ -381,6 +381,7 @@ SCRIPT_SH += git-web--browse.sh SCRIPT_LIB += git-mergetool--lib SCRIPT_LIB += git-parse-remote 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
@@ -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.1