Add infrastructure for translating Git with gettext
v6 cover Add infrastructure for translating Git with gettext
This is version 6 of the patch series to make Git optionally translatable with GNU gettext.
I had some time on the train so I polished it up since v5. Changes since then:
* Add a blurb to INSTALL about GNU libintl being a default requirement for building Git.
* There's now a Test::More test for Git::I18N. That module now has complete test coverage.
* git-sh-i18n.sh now has source code comments how to use it, and pointer to the GNU gettext manual for more.
* Eliminate the use of a wrapper function in the Perl library in favor of a symbol table alias.
* I tested the new Perl code I wrote on perl 5.6.2, it works.
* The tests now run successfully when Git isn't built with Perl support, or when the system doesn't support the locales we're trying to test.
* Improve the new tests by using variables defined in test-lib.sh to find out where the git root/po dir is instead of my own replacements.
* Make the tests easier to read by reducing redundancy and using e.g. "test_expect_failure grep" instead of calling test_cmp on the results of two grep invocations.
* Add new xgettext translator comment extraction tests.
* Moved t/t0200-gettext/* tests to t/t0200/*. This follows the convention of other tests that have their own subdirs in t/.
* Add a small (one-line) copyright notice to git-sh-i18n.sh and
Git::I18N.
Here's the difference between v5 and v6 in git diff --stat -M format:
INSTALL | 8 ++
Makefile | 6 +-
git-sh-i18n.sh | 18 +++-
perl/Git/I18N.pm | 28 ++++--
po/is.po | 2 -
t/t0200-gettext.sh | 52 ++++------
t/{t0200-gettext => t0200}/test.c | 0
t/{t0200-gettext => t0200}/test.perl | 3 +
t/{t0200-gettext => t0200}/test.sh | 0
...ell-fallbacks.sh => t0201-gettext-fallbacks.sh} | 12 ++-
t/t0202-gettext-perl.sh | 20 ++++
t/t0202/test.pl | 104 ++++++++++++++++++++
12 files changed, 203 insertions(+), 50 deletions(-)Ævar Arnfjörð Bjarmason (2):
Add infrastructure for translating Git with gettext Add initial C, Shell and Perl gettext translations
.gitignore | 2 + INSTALL | 8 +++ Makefile | 44 +++++++++++++- config.mak.in | 1 + configure.ac | 6 ++ gettext.c | 25 ++++++++ gettext.h | 13 ++++ git-pull.sh | 16 +++-- git-send-email.perl | 3 +- git-sh-i18n.sh | 47 ++++++++++++++ git.c | 3 + perl/Git/I18N.pm | 91 ++++++++++++++++++++++++++++ perl/Makefile | 3 +- perl/Makefile.PL | 14 ++++- po/.gitignore | 1 + po/is.po | 69 +++++++++++++++++++++ t/t0200-gettext.sh | 136 ++++++++++++++++++++++++++++++++++++++++++ t/t0200/test.c | 10 +++ t/t0200/test.perl | 14 ++++ t/t0200/test.sh | 14 ++++ t/t0201-gettext-fallbacks.sh | 42 +++++++++++++ t/t0202-gettext-perl.sh | 20 ++++++ t/t0202/test.pl | 104 ++++++++++++++++++++++++++++++++ t/test-lib.sh | 1 + wt-status.c | 107 +++++++++++++++++---------------- 25 files changed, 729 insertions(+), 65 deletions(-) create mode 100644 gettext.c create mode 100644 gettext.h create mode 100644 git-sh-i18n.sh create mode 100644 perl/Git/I18N.pm create mode 100644 po/.gitignore create mode 100644 po/is.po create mode 100755 t/t0200-gettext.sh create mode 100644 t/t0200/test.c create mode 100644 t/t0200/test.perl create mode 100644 t/t0200/test.sh create mode 100755 t/t0201-gettext-fallbacks.sh create mode 100755 t/t0202-gettext-perl.sh create mode 100644 t/t0202/test.pl
PATCH v6 1/2 Add infrastructure for translating Git with gettext
All of the interface messages in Git core are currently hardcoded in English. Change that by optionally enabling translation of the core C, Shell and Perl programs via GNU gettext. If you set the appropriate LC_* variables Git will speak your language, provided that someone has submitted a translation.
If gettext isn't available, or if Git is compiled with NO_GETTEXT=YesPlease, then Git fall back on its previous behavior of only speaking English.
With NO_GETTEXT=YesPlease gettext support will be #defined away for C programs. For Shell and Perl programs we rely on the git message catalog not being avalalable. That's a reasonable assumption since the *.po files won't be installed on the system during make install.
The gettext wrappers that are provided in the patch are only the bare minimum required to begin translation work. In particular I haven't added wrappers for the gettext functions that enable plural support, or those that provide message context (msgctxt). Those can be added later. The intent is to start with a small subset and see what we need later, not to start with something that's unnecessarily large right away.
Implementation and usage notes:
* General:
Gettext .mo files will be installed and looked for in the standard $(prefix)/share/locale path. GIT_TEXTDOMAINDIR can also be set to override that. But this is only intended to be used to test Git itself.
* Perl:
Perl code that wants to be localized should use the new Git::I18n module. It imports a __ function into the caller's package by default.
Instead of using the high level Locale::TextDomain interface I've opted to use the low-level (equivalent to the C interface) Locale::Messages module, which Locale::TextDomain itself uses.
Locale::TextDomain does a lot of redundant work we don't need, and some of it would potentially introduce bugs. It tries to set the $TEXTDOMAIN based on package of the caller, and has its own hardcoded paths where it'll search for messages.
I found it easier just to completely avoid it rather than try to circumvent its behavior. In any case, this is an issue wholly internal Git::I18N. Its guts can be changed later if that's deemed necessary.
* Shell:
Shell code that wants to be localized should use the new git-sh-i18n library. It's just a wrapper for the system's gettext.sh.
If gettext.sh isn't available we'll fall back on a dumb printf(1)-powered fall-through wrapper.
I originally tried to detect if the system supported `echo -n' but I found this to be a waste of time. My benchmarks on Linux, Solaris and FreeBSD reveal that printf(1) is fast enough, especially since we aren't calling gettext() from within any tight loops.
This patch is based on work by Jeff Epler <[email protected]> who did the initial Makefile / C work, and a lot of comments from the Git mailing list.
--- .gitignore | 2 + INSTALL | 8 +++ Makefile | 44 +++++++++++++- config.mak.in | 1 + configure.ac | 6 ++ gettext.c | 25 ++++++++ gettext.h | 13 ++++ git-sh-i18n.sh | 47 ++++++++++++++ git.c | 3 + perl/Git/I18N.pm | 91 ++++++++++++++++++++++++++++ perl/Makefile | 3 +- perl/Makefile.PL | 14 ++++- po/.gitignore | 1 + po/is.po | 46 ++++++++++++++ t/t0200-gettext.sh | 136 ++++++++++++++++++++++++++++++++++++++++++ t/t0200/test.c | 11 ++++ t/t0200/test.perl | 14 ++++ t/t0200/test.sh | 14 ++++ t/t0201-gettext-fallbacks.sh | 42 +++++++++++++ t/t0202-gettext-perl.sh | 20 ++++++ t/t0202/test.pl | 104 ++++++++++++++++++++++++++++++++ t/test-lib.sh | 1 + 22 files changed, 642 insertions(+), 4 deletions(-) create mode 100644 gettext.c create mode 100644 gettext.h create mode 100644 git-sh-i18n.sh create mode 100644 perl/Git/I18N.pm create mode 100644 po/.gitignore create mode 100644 po/is.po create mode 100755 t/t0200-gettext.sh create mode 100644 t/t0200/test.c create mode 100644 t/t0200/test.perl create mode 100644 t/t0200/test.sh create mode 100755 t/t0201-gettext-fallbacks.sh create mode 100755 t/t0202-gettext-perl.sh create mode 100644 t/t0202/test.pl
.gitignore +2 -0
@@ -125,6 +125,7 @@ /git-rm /git-send-email /git-send-pack +/git-sh-i18n /git-sh-setup /git-shell /git-shortlog
@@ -204,3 +205,4 @@ *.pdb /Debug/ /Release/ +/share/
INSTALL +8 -0
@@ -93,6 +93,14 @@ Issues of note: history graphically, and in git-gui. If you don't want gitk or git-gui, you can use NO_TCLTK. + - The GNU "libintl" library is used by default for localizing + Git. It needs a gettext.h on the system for C code, gettext.sh + for shell scripts, and libintl-perl for Perl programs. + + Set NO_GETTEXT to disable localization support and make Git only + use English. Under autoconf the configure script will do this + automatically if it can't find libintl on the system. + - Some platform specific issues are dealt with Makefile rules, but depending on your specific installation, you may not have all the libraries/tools needed, or you may have
Makefile +42 -2
129 lines not on this page · raw message
config.mak.in +1 -0
@@ -32,6 +32,7 @@ NO_CURL=@NO_CURL@ NO_EXPAT=@NO_EXPAT@ NO_LIBGEN_H=@NO_LIBGEN_H@ HAVE_PATHS_H=@HAVE_PATHS_H@ +NO_GETTEXT=@NO_GETTEXT@ NEEDS_LIBICONV=@NEEDS_LIBICONV@ NEEDS_SOCKET=@NEEDS_SOCKET@ NEEDS_RESOLV=@NEEDS_RESOLV@
configure.ac +6 -0
@@ -730,6 +730,12 @@ AC_CHECK_HEADER([paths.h], [HAVE_PATHS_H=]) AC_SUBST(HAVE_PATHS_H) # +# Define NO_GETTEXT if you don't have libintl.h +AC_CHECK_HEADER([libintl.h], +[NO_GETTEXT=], +[NO_GETTEXT=YesPlease]) +AC_SUBST(NO_GETTEXT) +# # Define NO_STRCASESTR if you don't have strcasestr. GIT_CHECK_FUNC(strcasestr, [NO_STRCASESTR=],
gettext.c new file +25 -0
@@ -0,0 +1,25 @@ +#ifdef NO_GETTEXT +void git_setup_gettext (void ) {} +#else +#include "exec_cmd.h" +#include <libintl.h> +#include <stdlib.h> + +void git_setup_gettext (void ) { +char *podir; +char *envdir =getenv ("GIT_TEXTDOMAINDIR" ); + +if (envdir) { + (void )bindtextdomain ("git" , envdir); + }else { + podir = (char *)system_path ("share/locale" ); +if (!podir)return ; + (void )bindtextdomain ("git" , podir); +free (podir); + } + + (void )setlocale (LC_MESSAGES ,"" ); + (void )setlocale (LC_CTYPE ,"" ); + (void )textdomain ("git" ); +} +#endif
gettext.h new file +13 -0
@@ -0,0 +1,13 @@ +#ifndef GETTEXT_H +#define GETTEXT_H + +void git_setup_gettext (void ); + +#ifdef NO_GETTEXT +#define _ (s) (s) +#else +#include <libintl.h> +#define _ (s) gettext(s) +#endif + +#endif
git-sh-i18n.sh new file +47 -0
47 lines not on this page · raw message
git.c +3 -0
@@ -3,6 +3,7 @@#include "cache.h" #include "quote.h" #include "run-command.h" +#include "gettext.h" const char git_usage_string[] ="git [--version] [--exec-path[=GIT_EXEC_PATH]] [--html-path]\n"
@@ -490,6 +491,8 @@ int main(int argc, const char **argv)if (!cmd) cmd ="git-help" ; +git_setup_gettext (); +/* * "git-xxxx" is the same as "git xxxx", but we obviously: *
perl/Git/I18N.pm new file +91 -0
91 lines not on this page · raw message
perl/Makefile +2 -1
@@ -5,6 +5,7 @@ makfile:=perl.mak PERL_PATH_SQ = $(subst ','\'',$(PERL_PATH)) prefix_SQ = $(subst ','\'',$(prefix)) +localedir_SQ = $(subst ','\'',$(localedir)) ifndef V QUIET = @
@@ -38,7 +39,7 @@ $(makfile): ../GIT-CFLAGS Makefile echo ' echo $(instdir_SQ)' >> $@ else $(makfile): Makefile.PL ../GIT-CFLAGS - $(PERL_PATH) $< PREFIX='$(prefix_SQ)' + $(PERL_PATH) $< PREFIX='$(prefix_SQ)' --localedir='$(localedir_SQ)' endif # this is just added comfort for calling make directly in perl dir
perl/Makefile.PL +13 -1
@@ -1,4 +1,12 @@ +use strict; +use warnings; use ExtUtils::MakeMaker; +use Getopt::Long; + +# Sanity: die at first unknown option +Getopt::Long::Configure qw/ pass_through /; + +GetOptions("localedir=s" => \my $localedir); sub MY::postamble { return <<'MAKE_FRAG';
@@ -16,7 +24,10 @@ endif MAKE_FRAG } -my %pm = ('Git.pm' => '$(INST_LIBDIR)/Git.pm'); +my %pm = ( + 'Git.pm' => '$(INST_LIBDIR)/Git.pm', + 'Git/I18N.pm' => '$(INST_LIBDIR)/Git/I18N.pm', +); # We come with our own bundled Error.pm. It's not in the set of default # Perl modules so install it if it's not available on the system yet.
@@ -33,6 +44,7 @@ WriteMakefile( NAME => 'Git', VERSION_FROM => 'Git.pm', PM => \%pm, + PM_FILTER => qq[\$(PERL) -pe "s<\\Q++LOCALEDIR++\\E><$localedir>"], MAKEFILE => 'perl.mak', INSTALLSITEMAN3DIR => '$(SITEPREFIX)/share/man/man3' );
po/.gitignore new file +1 -0
@@ -0,0 +1 @@ +/*.pot
po/is.po new file +46 -0
46 lines not on this page · raw message
t/t0200-gettext.sh new file +136 -0
136 lines not on this page · raw message
t/t0200/test.c new file +11 -0
@@ -0,0 +1,11 @@ +/* This is a phony C program that's only here to test xgettext message extraction */ + +int main (void ) +{ +/* TRANSLATORS: This is a test. You don't need to translate it. */ +puts (_ ("TEST: A C test string" )); + +/* TRANSLATORS: This is a test. You don't need to translate it. */ +printf (_ ("TEST: A C test string %s" ),"variable" ); +} +
t/t0200/test.perl new file +14 -0
@@ -0,0 +1,14 @@ +# This is a phony Perl program that's only here to test xgettext +# message extraction + +# so the above comment won't be folded into the next one by xgettext +1; + +# TRANSLATORS: This is a test. You don't need to translate it. +print __("TEST: A Perl test string"); + +# TRANSLATORS: This is a test. You don't need to translate it. +printf __("TEST: A Perl test variable %s"), "moo"; + +# TRANSLATORS: If you see this, Git has a bug +print _"TEST: A Perl string xgettext will not get";
t/t0200/test.sh new file +14 -0
@@ -0,0 +1,14 @@ +# This is a phony Shell program that's only here to test xgettext +# message extraction + +# so the above comment won't be folded into the next one by xgettext +echo + +# TRANSLATORS: This is a test. You don't need to translate it. +gettext "TEST: A Shell test string" + +# TRANSLATORS: This is a test. You don't need to translate it. +eval_gettext "TEST: A Shell test \$variable" + +# TRANSLATORS: If you see this, Git has a bug +_("TEST: A Shell string xgettext won't get")
t/t0201-gettext-fallbacks.sh new file +42 -0
42 lines not on this page · raw message
t/t0202-gettext-perl.sh new file +20 -0
@@ -0,0 +1,20 @@ +#!/bin/sh + +test_description='Perl gettext interface (Git::I18N)' +. ./test-lib.sh + +if ! test_have_prereq PERL; then + say 'skipping perl interface tests, perl not available' + test_done +fi + +"$PERL_PATH" -MTest::More -e 0 2>/dev/null || { + say "Perl Test::More unavailable, skipping test" + test_done +} + +test_external_without_stderr \ + 'Perl Git::I18N API' \ + "$PERL_PATH" "$TEST_DIRECTORY"/t0202/test.pl + +test_done
t/t0202/test.pl new file +104 -0
104 lines not on this page · raw message
t/test-lib.sh +1 -0
@@ -37,6 +37,7 @@ ORIGINAL_TERM=$TERM # For repeatability, reset the environment to known value. LANG=C LC_ALL=C +LANGUAGE=C PAGER=cat TZ=UTC TERM=dumb
Signature · 1 line
1.7.1.242.g64631
PATCH v6 2/2 Add initial C, Shell and Perl gettext translations
In reply to Ævar Arnfjörð BjarmasonChange the git status, git pull, and git send-email commands to have at least one translatable string. Each command uses a different core language, so this makes a good example of how C, Shell and Perl programs can be translated using gettext.
Since this introduces translation into the real Git tools more tests can be added to check if they translations actually work for real core tools.
--- git-pull.sh | 16 ++++--- git-send-email.perl | 3 +- po/is.po | 27 ++++++++++++- t/t0200/test.c | 1 - wt-status.c | 107 ++++++++++++++++++++++++++------------------------- 5 files changed, 90 insertions(+), 64 deletions(-)
git-pull.sh +9 -7
@@ -9,6 +9,7 @@ LONG_USAGE='Fetch one or more remote refs and merge it/them into the current HEA SUBDIRECTORY_OK=Yes OPTIONS_SPEC= . git-sh-setup +. git-sh-i18n set_reflog_action "pull $*" require_work_tree cd_to_toplevel
@@ -121,8 +122,8 @@ error_on_no_merge_candidates () { do case "$opt" in -t|--t|--ta|--tag|--tags) - echo "Fetching tags only, you probably meant:" - echo " git fetch --tags" + gettext "Fetching tags only, you probably meant:"; echo + gettext " git fetch --tags"; echo exit 1 esac done
@@ -154,11 +155,12 @@ error_on_no_merge_candidates () { echo "a branch. Because this is not the default configured remote" echo "for your current branch, you must specify a branch on the command line." elif [ -z "$curr_branch" ]; then - echo "You are not currently on a branch, so I cannot use any" - echo "'branch.<branchname>.merge' in your configuration file." - echo "Please specify which remote branch you want to use on the command" - echo "line and try again (e.g. 'git pull <repository> <refspec>')." - echo "See git-pull(1) for details." + gettext "You are not currently on a branch, so I cannot use any +'branch.<branchname>.merge' in your configuration file. +Please specify which remote branch you want to use on the command +line and try again (e.g. 'git pull <repository> <refspec>'). +See git-pull(1) for details."; + echo elif [ -z "$upstream" ]; then echo "You asked me to pull without telling me which branch you" echo "want to $op_type $op_prep, and 'branch.${curr_branch}.merge' in"
git-send-email.perl +2 -1
@@ -26,6 +26,7 @@ use Term::ANSIColor; use File::Temp qw/ tempdir tempfile /; use Error qw(:try); use Git; +use Git::I18N; Getopt::Long::Configure qw/ pass_through /;
@@ -674,7 +675,7 @@ if (!defined $sender) { $sender = $repoauthor || $repocommitter || ''; $sender = ask("Who should the emails appear to be from? [$sender] ", default => $sender); - print "Emails will be sent from: ", $sender, "\n"; + printf __("Emails will be sent from: %s\n"), $sender; $prompting++; }
po/is.po +25 -2
@@ -2,8 +2,8 @@ msgid "" msgstr "" "Project-Id-Version: Git\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2010-06-02 17:35+0000\n" -"PO-Revision-Date: 2010-06-02 16:01+0000\n" +"POT-Creation-Date: 2010-06-02 19:43+0000\n" +"PO-Revision-Date: 2010-06-02 19:35+0000\n" "Last-Translator: Ævar Arnfjörð Bjarmason <[email protected]>\n" "Language-Team: Git Mailing List <[email protected]>\n" "MIME-Version: 1.0\n"
@@ -12,6 +12,20 @@ msgstr "" "X-Poedit-Language: English\n" "X-Poedit-SourceCharset: utf-8\n" +#: wt-status.c:63 wt-status.c:79 wt-status.c:98 wt-status.c:110 +#: wt-status.c:622 +msgid "On branch " +msgstr "Á greininni " + +#: wt-status.c:629 +msgid "Not currently on any branch." +msgstr "Ekki á neinni grein." + +#: wt-status.c:663 +#, c-format +msgid "# No changes\n" +msgstr "# Engar breytingar\n" + #. TRANSLATORS: This is a test. You don't need to translate it. #: t/t0200-gettext/test.c:6 msgid "TEST: A C test string"
@@ -23,6 +37,10 @@ msgstr "TILRAUN: C tilraunastrengur" msgid "TEST: A C test string %s" msgstr "TILRAUN: C tilraunastrengur %s" +#: git-pull.sh:124 +msgid "Fetching tags only, you probably meant:" +msgstr "Næ aðeins í tögg, þú áttir líkast til við:" + #. TRANSLATORS: This is a test. You don't need to translate it. #: t/t0200-gettext/test.sh:8 msgid "TEST: A Shell test string"
@@ -34,6 +52,11 @@ msgstr "TILRAUN: Skeljartilraunastrengur" msgid "TEST: A Shell test $variable" msgstr "TILRAUN: Skeljartilraunastrengur með breytunni $variable" +#: git-send-email.perl:678 +#, perl-format +msgid "Emails will be sent from: %s\n" +msgstr "Póstarnir verða sendir frá: %s\n" + #. TRANSLATORS: This is a test. You don't need to translate it. #: t/t0200-gettext/test.perl:5 msgid "TEST: A Perl test string"
t/t0200/test.c +0 -1
@@ -8,4 +8,3 @@ int main(void)/* TRANSLATORS: This is a test. You don't need to translate it. */ printf (_ ("TEST: A C test string %s" ),"variable" ); } -
wt-status.c +54 -53
241 lines not on this page · raw message
Signature · 1 line
1.7.1.242.g64631