i18n: Add shell script translation infrastructure
Subsystems (2)
cover i18n: Add shell script translation infrastructure
As threatened this is the updated shell script translation infrastructure that I'm submitting now that 1.7.5 has been out for a bit.
This adds skeleton no-op functions to git-sh-i18n.sh analogous to the gettext.c skeleton functions for C, adds *.sh scripts to the "pot" target for message extraction, and updates the git-sh-i18n--envsubst tests to use the new test_i18ncmp function.
Æ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 | 36 +++ Documentation/git-sh-i18n.txt | 57 ++++ Makefile | 8 +- git-sh-i18n.sh | 29 ++ sh-i18n--envsubst.c | 444 +++++++++++++++++++++++++++++++ t/t0201-gettext-fallbacks.sh | 51 ++++ 7 files changed, 627 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
PATCH 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, 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.4
Re: [PATCH 1/4] git-sh-i18n--envsubst: our own envsubst(1) for eval_gettext()
Quoted 3 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
Ok up to this point.
Quoted 11 lines
... 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:
...
This was clever, but ...
...
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.
I do not think the lengthy history of failed experiments above is worth explaining. If you really want to say something to justify a new helper, I think it is sufficient to just explain that it is unsolvable in shell. I tried that in the first 9-line paragraph in:
http://thread.gmane.org/gmane.comp.version-control.git/170703/focus=170770
In other words, "we tried X that didn't work and we tried Y that didn't either, we cannot think of any better solution, so we are doing something else" is not a good justificiation for doing that "something else".
"Anything based on shell is an unpractical solution for this and that reasons, so we use this instead" explains that the earlier failures were not because we did not try hard enough. Unlike "tried X and Y but didn't work", dismissing "anything based on shell" as a whole class with clear explanation why it would not work would prevent people from pursuing that dead-end approach. It also avoids giving quibbling people an excuse to argue against importing envsubst implementation saying "you didn't try hard enough".
Quoted 5 lines
With it
eval_gettext()is implemented
like this:
...Reported-by Johannes Sixt <[email protected]>Signed-off-by Ævar Arnfjörð Bjarmason <[email protected]>
Ok.
Quoted 6 lines
Documentation/git-sh-i18n--envsubst.txt new file +0
-0
Documentation/git-sh-i18n--envsubst.txt line 32
Quoted 9 lines
Documentation/git-sh-i18n--envsubst.txt new file +8
-0@@ -0,0 +1,36 @@ +Author +------ +Written by Ævar Arnfjörð Bjarmason <[email protected]> + +Documentation +-------------- +Documentation by Ævar Arnfjörð Bjarmason and the git-list +<[email protected]>.
I do not think we do these individual credits these days in the doc.
Re: [PATCH 1/4] git-sh-i18n--envsubst: our own envsubst(1) for eval_gettext()
Quoted 31 lines · Junio C Hamano
Quoted 3 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()fallbackOk up to this point.
Quoted 11 lines
... 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:
...
This was clever, but ...
...
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.I do not think the lengthy history of failed experiments above is worth explaining. If you really want to say something to justify a new helper, I think it is sufficient to just explain that it is unsolvable in shell. I tried that in the first 9-line paragraph in:
http://thread.gmane.org/gmane.comp.version-control.git/170703/focus=170770
In other words, "we tried X that didn't work and we tried Y that didn't either, we cannot think of any better solution, so we are doing something else" is not a good justificiation for doing that "something else".
Quoted 8 lines
"Anything based on shell is an unpractical solution for this and that reasons, so we use this instead" explains that the earlier failures were not because we did not try hard enough. Unlike "tried X and Y but didn't work", dismissing "anything based on shell" as a whole class with clear explanation why it would not work would prevent people from pursuing that dead-end approach. It also avoids giving quibbling people an excuse to argue against importing envsubst implementation saying "you didn't try hard enough".
I included the failed experiments because I know for sure that those don't work, but I'm not sure that this is impossible in shell, and it might be possible in some non-POSIX shell (which we could optionally take advantage of on certain platforms in the future).
So having the failed examples that we know don't work is valuable for anyone digging into this in the future.
Or are we sure that this can't work in POSIX (or non-POSIX shells)?
Quoted 17 lines
Quoted 5 lines
With it
eval_gettext()is implemented
like this:
...Reported-by Johannes Sixt <[email protected]>Signed-off-by Ævar Arnfjörð Bjarmason <[email protected]>Ok.
Quoted 9 lines
Documentation/git-sh-i18n--envsubst.txt new file +2
-0@@ -0,0 +1,36 @@ +Author +------
Quoted 8 lines
Documentation/git-sh-i18n--envsubst.txt line 32
Quoted 6 lines
Documentation/git-sh-i18n--envsubst.txt new file +6
-0+Written by Ævar Arnfjörð Bjarmason <[email protected]> + +Documentation +-------------- +Documentation by Ævar Arnfjörð Bjarmason and the git-list +<[email protected]>.I do not think we do these individual credits these days in the doc.
I wrote this when that was the convention, but evidently we've changed that since then.
I'll remove it in the next iteration.
Re: [PATCH 1/4] git-sh-i18n--envsubst: our own envsubst(1) for eval_gettext()
Quoted 1 line · Ævar Arnfjörð Bjarmason
Or are we sure that this can't work in POSIX (or non-POSIX shells)?
You need to implement the same logic to tokenize enough to understand the expansion the shell does inside dq. Yes, shell is general programming language and you can theoretically implement it, but would the result be a practical solution? I doubt it.
Re: [PATCH 1/4] git-sh-i18n--envsubst: our own envsubst(1) for eval_gettext()
Quoted 8 lines · Junio C Hamano
Quoted 1 line · Ævar Arnfjörð Bjarmason
Or are we sure that this can't work in POSIX (or non-POSIX shells)?
You need to implement the same logic to tokenize enough to understand the expansion the shell does inside dq. Yes, shell is general programming language and you can theoretically implement it, but would the result be a practical solution? I doubt it.
I'll write a better commit message and re-send this soon (after some other comments have trickled in).
PATCH 2/4 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_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
@@ -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
PATCH 3/4 git-sh-i18n.sh: add GIT_GETTEXT_POISON support
In reply to Ævar Arnfjörð BjarmasonChange 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
@@ -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
PATCH 4/4 Makefile: add xgettext target for *.sh files
In reply to Ævar Arnfjörð BjarmasonChange 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
@@ -2065,10 +2065,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
Heya,
Quoted 4 lines · Ævar Arnfjörð Bjarmason
This adds skeleton no-op functions to
git-sh-i18n.shanalogous to thegettext.cskeleton functions for C, adds *.sh scripts to the "pot" target for message extraction, and updates the git-sh-i18n--envsubst tests to use the newtest_i18ncmpfunction.
I seem to remember there were some concerns about performance with a previous version of this series. Have you done any before/after timings on, say, the git test suite? Or am I remembering incorrectly?
Signature · 3 lines
Cheers, Sverre Rabbelier
Quoted 11 lines · Sverre Rabbelier
Heya,
Quoted 4 lines · Ævar Arnfjörð Bjarmason
This adds skeleton no-op functions to
git-sh-i18n.shanalogous to thegettext.cskeleton functions for C, adds *.sh scripts to the "pot" target for message extraction, and updates the git-sh-i18n--envsubst tests to use the newtest_i18ncmpfunction.I seem to remember there were some concerns about performance with a previous version of this series. Have you done any before/after timings on, say, the git test suite? Or am I remembering incorrectly?
When I benchmark this on my (Linux) system it runs at 0% difference between the current test suite and the one with this series.
The concern was about Windows, where forks are more expensive, so e.g. every time we shell out to sed/awk/perl/grep or git-sh-i18n--envsubst we incur a larger speed hit than on Unix systems.
Heya,
[+msysgit people]
Quoted 11 lines · Ævar Arnfjörð Bjarmason
Quoted 9 lines · Sverre Rabbelier
Quoted 4 lines · Ævar Arnfjörð Bjarmason
This adds skeleton no-op functions to
git-sh-i18n.shanalogous to thegettext.cskeleton functions for C, adds *.sh scripts to the "pot" target for message extraction, and updates the git-sh-i18n--envsubst tests to use the newtest_i18ncmpfunction.I seem to remember there were some concerns about performance with a previous version of this series. Have you done any before/after timings on, say, the git test suite? Or am I remembering incorrectly?
Quoted 7 lines
When I benchmark this on my (Linux) system it runs at 0% difference between the current test suite and the one with this series.
The concern was about Windows, where forks are more expensive, so e.g. every time we shell out to sed/awk/perl/grep or git-sh-i18n--envsubst we incur a larger speed hit than on Unix systems.
Perhaps one of the windows devs can benchmark this then? Ævar, where do you have a pullable version of this series? Your github fork has quite a few branches.
Signature · 3 lines
Cheers, Sverre Rabbelier
Quoted 19 lines · Sverre Rabbelier
Heya,
[+msysgit people]
Quoted 14 lines · Ævar Arnfjörð Bjarmason
Quoted 9 lines · Sverre Rabbelier
Quoted 4 lines · Ævar Arnfjörð Bjarmason
This adds skeleton no-op functions to
git-sh-i18n.shanalogous to thegettext.cskeleton functions for C, adds *.sh scripts to the "pot" target for message extraction, and updates the git-sh-i18n--envsubst tests to use the newtest_i18ncmpfunction.I seem to remember there were some concerns about performance with a previous version of this series. Have you done any before/after timings on, say, the git test suite? Or am I remembering incorrectly?
When I benchmark this on my (Linux) system it runs at 0% difference between the current test suite and the one with this series.
Quoted 8 lines
Quoted 4 lines
The concern was about Windows, where forks are more expensive, so e.g. every time we shell out to sed/awk/perl/grep or git-sh-i18n--envsubst we incur a larger speed hit than on Unix systems.
Perhaps one of the windows devs can benchmark this then? Ævar, where do you have a pullable version of this series? Your github fork has quite a few branches.
It's ab/i18n-sh-only in git://github.com/avar/git.git
There was some discussion about this a while ago, not sure what came out of that, but there were some msysgit patches involved IIRC.