Skip to contentSkip to conversation

i18n back from the dead

RFC/PATCH Ævar Arnfjörð Bjarmason 3 participants 6 messages Jun 15, 2016

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

Or, a patch so huge it needs it own cover letter.

After looking at my dormant i18n series I found that it was spread out over so many pieces that even I didn't understand it anymore.

So here's an attempt to fix that. I've squashed everything we'll need to get i18n working in Git down to one patch. Much of the previous complexity came about because I was adding little things to it incrementally, and I kept carrying around commits that were effectively bugfixes on top of the original code.

Now it's all in one big patch, and while the diffstat looks scary at first a lot of it is tests, documentation and a giant comment.

It's an RFC because there are bound to be bugs in there that I haven't spotted, and po/README is out of date, I haven't updated it for some of the new C and Shell functions we now have.

But it seems to function just fine. I've tested it on two Linux boxes and a Solaris box and it works as intended, but review and testing on other platforms would be most welcome.

This is all we need to start using PO files that are dropped in po/, which I don't think is all that shabby for 1300 lines changed.

Ævar Arnfjörð Bjarmason (1):
i18n: add infrastructure for translating Git with gettext

 .gitignore                          |    1 +
 Documentation/CodingGuidelines      |    7 +
 INSTALL                             |   12 ++
 Makefile                            |   81 +++++++++++++-
 config.mak.in                       |    3 +
 configure.ac                        |   19 +++
 daemon.c                            |    2 +
 fast-import.c                       |    2 +
 gettext.c                           |  116 +++++++++++++++++++
 gettext.h                           |   25 ++++-
 git-sh-i18n.sh                      |  100 ++++++++++++-----
 git.c                               |    2 +
 http-backend.c                      |    2 +
 http-fetch.c                        |    2 +
 http-push.c                         |    2 +
 imap-send.c                         |    2 +
 perl/Git/I18N.pm                    |   89 +++++++++++++++
 perl/Makefile                       |    3 +-
 perl/Makefile.PL                    |   14 ++-
 po/README                           |  209 +++++++++++++++++++++++++++++++++++
 po/is.po                            |   93 ++++++++++++++++
 shell.c                             |    1 +
 show-index.c                        |    2 +
 t/lib-gettext.sh                    |   55 +++++++++
 t/t0200-gettext-basic.sh            |  108 ++++++++++++++++++
 t/t0200/test.c                      |   23 ++++
 t/t0200/test.perl                   |   14 +++
 t/t0200/test.sh                     |   14 +++
 t/t0201-gettext-fallbacks.sh        |   20 +++-
 t/t0202-gettext-perl.sh             |   27 +++++
 t/t0202/test.pl                     |  110 ++++++++++++++++++
 t/t0203-gettext-setlocale-sanity.sh |   26 +++++
 t/t0204-gettext-reencode-sanity.sh  |   78 +++++++++++++
 t/t0205-gettext-poison.sh           |   36 ++++++
 t/test-lib.sh                       |    3 +
 upload-pack.c                       |    2 +
 wrap-for-bin.sh                     |    3 +-
 37 files changed, 1269 insertions(+), 39 deletions(-)
 create mode 100644 perl/Git/I18N.pm
 create mode 100644 po/README
 create mode 100644 po/is.po
 create mode 100644 t/lib-gettext.sh
 create mode 100755 t/t0200-gettext-basic.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/t0202-gettext-perl.sh
 create mode 100644 t/t0202/test.pl
 create mode 100755 t/t0203-gettext-setlocale-sanity.sh
 create mode 100755 t/t0204-gettext-reencode-sanity.sh
 create mode 100755 t/t0205-gettext-poison.sh
Signature · 1 line
1.7.6.3
#1 lore raw

[RFC/PATCH] i18n: add infrastructure for translating Git with gettext

Change the skeleton implementation of i18n in Git to one that can show localized strings to users for our C, Shell and Perl programs using either GNU libint or the Solaris gettext implementation.

This new internationalization support is enabled by default. If gettext isn't available, or if Git is compiled with NO_GETTEXT=YesPlease, Git fall back on its current behavior of showing interface messages in English. When using the autoconf script we'll auto-detect if the gettext libraries are installed and act appropriately.

This change is somewhat large because as well as adding a C, Shell and Perl i18n interface we're adding a lot of tests for them, and for those tests to work we need a skeleton PO file to actually test translations. A minimal Icelandic translation is included for this purpose. Icelandic includes multi-byte characters which makes it easy to test various edge cases, and it's a language I happen to understand.

The rest of the commit message goes into detail about various sub-parts of this commit.

= Installation

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 that's 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.

See <[email protected]> for a further elaboration on this topic.

= Shell

Shell code that's to be localized should use the git-sh-i18n library. It's basically just a wrapper for the system's gettext.sh.

If gettext.sh isn't available we'll fall back on gettext(1) if it's available. The latter is available without the former on Solaris, which has its own non-GNU gettext implementation. We also need to emulate eval_gettext() there.

If neither are present we'll use a dumb printf(1) 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, and unlikely to ever do so.

= About libcharset.h and langinfo.h

We use libcharset to query the character set of the current locale if it's available. I.e. we'll use it instead of nl_langinfo if HAVE_LIBCHARSET_H is set.

The GNU gettext manual recommends using langinfo.h's nl_langinfo(CODESET) to acquire the current character set, but on systems that have libcharset.h's locale_charset() using the latter is either saner, or the only option on those systems.

GNU and Solaris have a nl_langinfo(CODESET), FreeBSD can use either, but MinGW and some others need to use libcharset.h's locale_charset() instead.

---
 .gitignore                          |    1 +
 Documentation/CodingGuidelines      |    7 +
 INSTALL                             |   12 ++
 Makefile                            |   81 +++++++++++++-
 config.mak.in                       |    3 +
 configure.ac                        |   19 +++
 daemon.c                            |    2 +
 fast-import.c                       |    2 +
 gettext.c                           |  116 +++++++++++++++++++
 gettext.h                           |   25 ++++-
 git-sh-i18n.sh                      |  100 ++++++++++++-----
 git.c                               |    2 +
 http-backend.c                      |    2 +
 http-fetch.c                        |    2 +
 http-push.c                         |    2 +
 imap-send.c                         |    2 +
 perl/Git/I18N.pm                    |   89 +++++++++++++++
 perl/Makefile                       |    3 +-
 perl/Makefile.PL                    |   14 ++-
 po/README                           |  209 +++++++++++++++++++++++++++++++++++
 po/is.po                            |   93 ++++++++++++++++
 shell.c                             |    1 +
 show-index.c                        |    2 +
 t/lib-gettext.sh                    |   55 +++++++++
 t/t0200-gettext-basic.sh            |  108 ++++++++++++++++++
 t/t0200/test.c                      |   23 ++++
 t/t0200/test.perl                   |   14 +++
 t/t0200/test.sh                     |   14 +++
 t/t0201-gettext-fallbacks.sh        |   20 +++-
 t/t0202-gettext-perl.sh             |   27 +++++
 t/t0202/test.pl                     |  110 ++++++++++++++++++
 t/t0203-gettext-setlocale-sanity.sh |   26 +++++
 t/t0204-gettext-reencode-sanity.sh  |   78 +++++++++++++
 t/t0205-gettext-poison.sh           |   36 ++++++
 t/test-lib.sh                       |    3 +
 upload-pack.c                       |    2 +
 wrap-for-bin.sh                     |    3 +-
 37 files changed, 1269 insertions(+), 39 deletions(-)
 create mode 100644 perl/Git/I18N.pm
 create mode 100644 po/README
 create mode 100644 po/is.po
 create mode 100644 t/lib-gettext.sh
 create mode 100755 t/t0200-gettext-basic.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/t0202-gettext-perl.sh
 create mode 100644 t/t0202/test.pl
 create mode 100755 t/t0203-gettext-setlocale-sanity.sh
 create mode 100755 t/t0204-gettext-reencode-sanity.sh
 create mode 100755 t/t0205-gettext-poison.sh
.gitignore +1 -0
index 8572c8c..c47f3a8 100644
@@ -224,3 +224,4 @@
 *.pdb
 /Debug/
 /Release/
+/share/
Documentation/CodingGuidelines +7 -0
index fe1c1e5..4830086 100644
@@ -81,6 +81,10 @@ For shell scripts specifically (not exhaustive):
      are ERE elements not BRE (note that \? and \+ are not even part
      of BRE -- making them accessible from BRE is a GNU extension).
 
+ - Use Git's gettext wrappers in git-sh-i18n to make the user
+   interface translatable. See "Marking strings for translation" in
+   po/README.
+
 For C programs:
 
  - We use tabs to indent, and interpret tabs as taking up to
@@ -144,6 +148,9 @@ For C programs:
  - When we pass <string, length> pair to functions, we should try to
    pass them in that order.
 
+ - Use Git's gettext wrappers to make the user interface
+   translatable. See "Marking strings for translation" in po/README.
+
 Writing Documentation:
 
  Every user-visible change should be reflected in the documentation.
INSTALL +12 -0
index bf0d97e..8120641 100644
@@ -106,6 +106,18 @@ Issues of note:
 	  history graphically, and in git-gui.  If you don't want gitk or
 	  git-gui, you can use NO_TCLTK.
 
+	- A gettext library is used by default for localizing Git. The
+	  primary target is GNU libintl, but the Solaris gettext
+	  implementation also works.
+
+	  We need a gettext.h on the system for C code, gettext.sh (or
+	  Solaris gettext(1)) 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 +77 -4

238 lines not on this page · raw message

config.mak.in +3 -0
index ab37101..10698c8 100644
@@ -35,6 +35,9 @@ NO_CURL=@NO_CURL@
 NO_EXPAT=@NO_EXPAT@
 NO_LIBGEN_H=@NO_LIBGEN_H@
 HAVE_PATHS_H=@HAVE_PATHS_H@
+HAVE_LIBCHARSET_H=@HAVE_LIBCHARSET_H@
+NO_GETTEXT=@NO_GETTEXT@
+LIBC_CONTAINS_LIBINTL=@LIBC_CONTAINS_LIBINTL@
 NEEDS_LIBICONV=@NEEDS_LIBICONV@
 NEEDS_SOCKET=@NEEDS_SOCKET@
 NEEDS_RESOLV=@NEEDS_RESOLV@
configure.ac +19 -0
index 048a1d4..630dbdd 100644
@@ -636,6 +636,12 @@ AC_CHECK_LIB([c], [basename],
 AC_SUBST(NEEDS_LIBGEN)
 test -n "$NEEDS_LIBGEN" && LIBS="$LIBS -lgen"
 
+AC_CHECK_LIB([c], [gettext],
+[LIBC_CONTAINS_LIBINTL=YesPlease],
+[LIBC_CONTAINS_LIBINTL=])
+AC_SUBST(LIBC_CONTAINS_LIBINTL)
+test -n "$LIBC_CONTAINS_LIBINTL" || LIBS="$LIBS -lintl"
+
 ## Checks for header files.
 AC_MSG_NOTICE([CHECKS for header files])
 #
@@ -818,6 +824,19 @@ AC_CHECK_HEADER([paths.h],
 [HAVE_PATHS_H=])
 AC_SUBST(HAVE_PATHS_H)
 #
+# Define NO_GETTEXT if you don't want Git output to be translated.
+# A translated Git requires GNU libintl or another gettext implementation
+AC_CHECK_HEADER([libintl.h],
+[NO_GETTEXT=],
+[NO_GETTEXT=YesPlease])
+AC_SUBST(NO_GETTEXT)
+#
+# Define HAVE_LIBCHARSET_H if have libcharset.h
+AC_CHECK_HEADER([libcharset.h],
+[HAVE_LIBCHARSET_H=YesPlease],
+[HAVE_LIBCHARSET_H=])
+AC_SUBST(HAVE_LIBCHARSET_H)
+#
 # Define NO_STRCASESTR if you don't have strcasestr.
 GIT_CHECK_FUNC(strcasestr,
 [NO_STRCASESTR=],
daemon.c +2 -0
index fa28300..15ce918 100644
@@ -1099,6 +1099,8 @@ int main(int argc, char **argv)
 	struct credentials *cred = NULL;
 	int i;
 
+	git_setup_gettext();
+
 	git_extract_argv0_path(argv[0]);
 
 	for (i = 1; i < argc; i++) {
fast-import.c +2 -0
index 8d8ea3c..b59e7db 100644
@@ -3292,6 +3292,8 @@ int main(int argc, const char **argv)
 
 	git_extract_argv0_path(argv[0]);
 
+	git_setup_gettext();
+
 	if (argc == 2 && !strcmp(argv[1], "-h"))
 		usage(fast_import_usage);
 
gettext.c +116 -0

125 lines not on this page · raw message

gettext.h +23 -2
index 24d9182..57ba8bb 100644
@@ -13,8 +13,29 @@
 #error "namespace conflict: '_' or 'Q_' is pre-defined?"
 #endif
 
+#ifndef NO_GETTEXT
+#	include <libintl.h>
+#else
+#	ifdef gettext
+#		undef gettext
+#	endif
+#	define gettext(s) (s)
+#	ifdef ngettext
+#		undef ngettext
+#	endif
+#	define ngettext(s, p, n) ((n == 1) ? (s) : (p))
+#endif
+
 #define FORMAT_PRESERVING(n) __attribute__((format_arg(n)))
 
+#ifndef NO_GETTEXT
+extern void git_setup_gettext(void);
+#else
+static inline void git_setup_gettext(void)
+{
+}
+#endif
+
 #ifdef GETTEXT_POISON
 extern int use_gettext_poison(void);
 #else
@@ -23,7 +44,7 @@ extern int use_gettext_poison(void);
 
 static inline FORMAT_PRESERVING(1) const char *_(const char *msgid)
 {
-	return use_gettext_poison() ? "# GETTEXT POISON #" : msgid;
+	return use_gettext_poison() ? "# GETTEXT POISON #" : gettext(msgid);
 }
 
 static inline FORMAT_PRESERVING(1) FORMAT_PRESERVING(2)
@@ -31,7 +52,7 @@ const char *Q_(const char *msgid, const char *plu, unsigned long n)
 {
 	if (use_gettext_poison())
 		return "# GETTEXT POISON #";
-	return n == 1 ? msgid : plu;
+	return ngettext(msgid, plu, n);
 }
 
 /* Mark msgid for translation but do not translate it. */
git-sh-i18n.sh +72 -28

119 lines not on this page · raw message

git.c +2 -0
index 8e34903..fa918b5 100644
@@ -537,6 +537,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:
 	 *
http-backend.c +2 -0
index 59ad7da..869d515 100644
@@ -545,6 +545,8 @@ int main(int argc, char **argv)
 	char *cmd_arg = NULL;
 	int i;
 
+	git_setup_gettext();
+
 	git_extract_argv0_path(argv[0]);
 	set_die_routine(die_webcgi);
 
http-fetch.c +2 -0
index 69299b7..9719d58 100644
@@ -22,6 +22,8 @@ int main(int argc, const char **argv)
 	int get_verbosely = 0;
 	int get_recover = 0;
 
+	git_setup_gettext();
+
 	git_extract_argv0_path(argv[0]);
 
 	while (arg < argc && argv[arg][0] == '-') {
http-push.c +2 -0
index edd553b..f856299 100644
@@ -1748,6 +1748,8 @@ int main(int argc, char **argv)
 	int new_refs;
 	struct ref *ref, *local_refs;
 
+	git_setup_gettext();
+
 	git_extract_argv0_path(argv[0]);
 
 	repo = xcalloc(sizeof(*repo), 1);
imap-send.c +2 -0
index e1ad1a4..9fba422 100644
@@ -1539,6 +1539,8 @@ int main(int argc, char **argv)
 
 	git_extract_argv0_path(argv[0]);
 
+	git_setup_gettext();
+
 	if (argc != 1)
 		usage(imap_send_usage);
 
perl/Git/I18N.pm new file +89 -0

89 lines not on this page · raw message

perl/Makefile +2 -1
index a2ffb64..b2977cd 100644
@@ -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)' INSTALL_BASE=''
+	$(PERL_PATH) $< PREFIX='$(prefix_SQ)' INSTALL_BASE='' --localedir='$(localedir_SQ)'
 endif
 
 # this is just added comfort for calling make directly in perl dir
perl/Makefile.PL +13 -1
index 0b9deca..456d45b 100644
@@ -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/README new file +209 -0

209 lines not on this page · raw message

po/is.po new file +93 -0

93 lines not on this page · raw message

shell.c +1 -0
index abb8622..713def9 100644
@@ -137,6 +137,7 @@ int main(int argc, char **argv)
 	int devnull_fd;
 	int count;
 
+	git_setup_gettext();
 	git_extract_argv0_path(argv[0]);
 
 	/*
show-index.c +2 -0
index 63f9da5..5a9eed7 100644
@@ -11,6 +11,8 @@ int main(int argc, char **argv)
 	unsigned int version;
 	static unsigned int top_index[256];
 
+	git_setup_gettext();
+
 	if (argc != 1)
 		usage(show_index_usage);
 	if (fread(top_index, 2 * 4, 1, stdin) != 1)
t/lib-gettext.sh new file +55 -0

55 lines not on this page · raw message

t/t0200-gettext-basic.sh new file +108 -0

108 lines not on this page · raw message

t/t0200/test.c new file +23 -0
new file mode 100644
index 0000000..584d45c
@@ -0,0 +1,23 @@
+/* This is a phony C program that's only here to test xgettext message extraction */
+
+const char help[] =
+	/* TRANSLATORS: This is a test. You don't need to translate it. */
+	N_("See 'git help COMMAND' for more information on a specific command.");
+
+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");
+
+	/* TRANSLATORS: This is a test. You don't need to translate it. */
+	printf(_("TEST: Hello World!"));
+
+	/* TRANSLATORS: This is a test. You don't need to translate it. */
+	printf(_("TEST: Old English Runes"));
+
+	/* TRANSLATORS: This is a test. You don't need to translate it. */
+	printf(_("TEST: ‘single’ and “double” quotes"));
+}
t/t0200/test.perl new file +14 -0
new file mode 100644
index 0000000..36fba34
@@ -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
new file mode 100644
index 0000000..022d607
@@ -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 +18 -2
index 54d98b9..52b1c27 100755
@@ -5,8 +5,24 @@
 
 test_description='Gettext Shell fallbacks'
 
-. ./test-lib.sh
-. "$GIT_BUILD_DIR"/git-sh-i18n
+GIT_INTERNAL_GETTEXT_TEST_FALLBACKS=YesPlease
+export GIT_INTERNAL_GETTEXT_TEST_FALLBACKS
+
+. ./lib-gettext.sh
+
+test_expect_success "sanity: \$GIT_INTERNAL_GETTEXT_SH_SCHEME is set (to $GIT_INTERNAL_GETTEXT_SH_SCHEME)" '
+    test -n "$GIT_INTERNAL_GETTEXT_SH_SCHEME"
+'
+
+test_expect_success 'sanity: $GIT_INTERNAL_GETTEXT_TEST_FALLBACKS is set' '
+    test -n "$GIT_INTERNAL_GETTEXT_TEST_FALLBACKS"
+'
+
+test_expect_success C_LOCALE_OUTPUT 'sanity: $GIT_INTERNAL_GETTEXT_SH_SCHEME" is fallthrough' '
+    echo fallthrough >expect &&
+    echo $GIT_INTERNAL_GETTEXT_SH_SCHEME >actual &&
+    test_cmp expect actual
+'
 
 test_expect_success 'gettext: our gettext() fallback has pass-through semantics' '
     printf "test" >expect &&
t/t0202-gettext-perl.sh new file +27 -0
new file mode 100755
index 0000000..428ebb0
@@ -0,0 +1,27 @@
+#!/bin/sh
+#
+# Copyright (c) 2010 Ævar Arnfjörð Bjarmason
+#
+
+test_description='Perl gettext interface (Git::I18N)'
+
+. ./lib-gettext.sh
+
+if ! test_have_prereq PERL; then
+	skip_all='skipping perl interface tests, perl not available'
+	test_done
+fi
+
+"$PERL_PATH" -MTest::More -e 0 2>/dev/null || {
+	skip_all="Perl Test::More unavailable, skipping test"
+	test_done
+}
+
+# The external test will outputs its own plan
+test_external_has_tap=1
+
+test_external_without_stderr \
+    'Perl Git::I18N API' \
+    "$PERL_PATH" "$TEST_DIRECTORY"/t0202/test.pl
+
+test_done
t/t0202/test.pl new file +110 -0

110 lines not on this page · raw message

t/t0203-gettext-setlocale-sanity.sh new file +26 -0
new file mode 100755
index 0000000..a212460
@@ -0,0 +1,26 @@
+#!/bin/sh
+#
+# Copyright (c) 2010 Ævar Arnfjörð Bjarmason
+#
+
+test_description="The Git C functions aren't broken by setlocale(3)"
+
+. ./lib-gettext.sh
+
+test_expect_success 'git show a ISO-8859-1 commit under C locale' '
+	. "$TEST_DIRECTORY"/t3901-8859-1.txt &&
+	test_commit "iso-c-commit" iso-under-c &&
+	git show >out 2>err &&
+	! test -s err &&
+	grep -q "iso-c-commit" out
+'
+
+test_expect_success GETTEXT_LOCALE 'git show a ISO-8859-1 commit under a UTF-8 locale' '
+	. "$TEST_DIRECTORY"/t3901-8859-1.txt &&
+	test_commit "iso-utf8-commit" iso-under-utf8 &&
+	LANGUAGE=is LC_ALL="$is_IS_locale" git show >out 2>err &&
+	! test -s err &&
+	grep -q "iso-utf8-commit" out
+'
+
+test_done
t/t0204-gettext-reencode-sanity.sh new file +78 -0

78 lines not on this page · raw message

t/t0205-gettext-poison.sh new file +36 -0
new file mode 100755
index 0000000..2361590
@@ -0,0 +1,36 @@
+#!/bin/sh
+#
+# Copyright (c) 2010 Ævar Arnfjörð Bjarmason
+#
+
+test_description='Gettext Shell poison'
+
+. ./lib-gettext.sh
+
+test_expect_success GETTEXT_POISON "sanity: \$GIT_INTERNAL_GETTEXT_SH_SCHEME is set (to $GIT_INTERNAL_GETTEXT_SH_SCHEME)" '
+    test -n "$GIT_INTERNAL_GETTEXT_SH_SCHEME"
+'
+
+test_expect_success GETTEXT_POISON 'sanity: $GIT_INTERNAL_GETTEXT_SH_SCHEME" is poison' '
+    test "$GIT_INTERNAL_GETTEXT_SH_SCHEME" = "poison"
+'
+
+test_expect_success GETTEXT_POISON 'gettext: our gettext() fallback has poison semantics' '
+    printf "# GETTEXT POISON #" >expect &&
+    gettext "test" >actual &&
+    test_cmp expect actual &&
+    printf "# GETTEXT POISON #" >expect &&
+    gettext "test more words" >actual &&
+    test_cmp expect actual
+'
+
+test_expect_success GETTEXT_POISON 'eval_gettext: our eval_gettext() fallback has poison semantics' '
+    printf "# GETTEXT POISON #" >expect &&
+    eval_gettext "test" >actual &&
+    test_cmp expect actual &&
+    printf "# GETTEXT POISON #" >expect &&
+    eval_gettext "test more words" >actual &&
+    test_cmp expect actual
+'
+
+test_done
t/test-lib.sh +3 -0
index bdd9513..9cfabe4 100644
@@ -44,6 +44,7 @@ export LANG LC_ALL PAGER TERM TZ
 EDITOR=:
 unset VISUAL
 unset EMAIL
+unset LANGUAGE
 unset $(perl -e '
 	my @env = keys %ENV;
 	my $ok = join("|", qw(
@@ -1113,12 +1114,14 @@ esac
 test -z "$NO_PERL" && test_set_prereq PERL
 test -z "$NO_PYTHON" && test_set_prereq PYTHON
 test -n "$USE_LIBPCRE" && test_set_prereq LIBPCRE
+test -z "$NO_GETTEXT" && test_set_prereq GETTEXT
 
 # Can we rely on git's output in the C locale?
 if test -n "$GETTEXT_POISON"
 then
 	GIT_GETTEXT_POISON=YesPlease
 	export GIT_GETTEXT_POISON
+	test_set_prereq GETTEXT_POISON
 else
 	test_set_prereq C_LOCALE_OUTPUT
 fi
upload-pack.c +2 -0
index 470cffd..6f36f62 100644
@@ -784,6 +784,8 @@ int main(int argc, char **argv)
 	int i;
 	int strict = 0;
 
+	git_setup_gettext();
+
 	packet_trace_identity("upload-pack");
 	git_extract_argv0_path(argv[0]);
 	read_replace_refs = 0;
wrap-for-bin.sh +2 -1
index 09feb1f..d2d9dcc 100644
@@ -15,7 +15,8 @@ else
 	export GIT_TEMPLATE_DIR
 fi
 GITPERLLIB='@@BUILD_DIR@@/perl/blib/lib'
+GIT_TEXTDOMAINDIR='@@BUILD_DIR@@/share/locale'
 PATH='@@BUILD_DIR@@/bin-wrappers:'"$PATH"
-export GIT_EXEC_PATH GITPERLLIB PATH
+export GIT_EXEC_PATH GITPERLLIB PATH GIT_TEXTDOMAINDIR
 
 exec "${GIT_EXEC_PATH}/@@PROG@@" "$@"
Signature · 1 line
1.7.6.3
#2 lore raw

Re: [RFC/PATCH] i18n: add infrastructure for translating Git with gettext

Quoted 9 lines · Ævar Arnfjörð Bjarmason

= 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.

Nice of you using libintl-perl instead of old Locale::MakeText (with gettext compatibility layer).

Quoted 2 lines

See <[email protected]> for a further elaboration on this topic.

http://thread.gmane.org/gmane.comp.version-control.git/148446/focus=148478

Quoted 10 lines

= Shell

Shell code that's to be localized should use the git-sh-i18n library. It's basically just a wrapper for the system's gettext.sh.

If gettext.sh isn't available we'll fall back on gettext(1) if it's available. The latter is available without the former on Solaris, which has its own non-GNU gettext implementation. We also need to emulate eval_gettext() there.

Quoted 8 lines

If neither are present we'll use a dumb printf(1) 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, and unlikely to ever do so.

Didn't we decide that the only sane way to handle eval_gettext is to provide minimal implemetation in the form of external command?

Thanks for working on this.

Signature · 1 line
Jakub Narębski
#3 lore raw

Re: [RFC/PATCH] i18n: add infrastructure for translating Git with gettext

Quoted 8 lines · Jakub Narebski
Quoted 5 lines

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, and unlikely to ever do so.

Didn't we decide that the only sane way to handle eval_gettext is to provide minimal implemetation in the form of external command?

This comment is a bit out of date and probably shouldn't be in the commit message. But it doesn't have to do with eval_gettext. I mean at one point I tried to change the fall-through wrapper from:

    gettext () {
	    printf "%s" "$1"
	}

to:

    gettext () {
	    echo -n $1
	}

But found it to bee too troublesome with various echo implementations and just not worth it for the complexity.

But yeah, for eval_gettext() we need an external program, which we already have in git.git as git-sh-i18n--envsubst.

#4 lore raw

Re: [RFC/PATCH] i18n: add infrastructure for translating Git with gettext

In reply to Ævar Arnfjörð Bjarmason

Just an extremely minor issue, but a few things seem to seep through the "make V=''" build:

SUBDIR perl
/usr/bin/perl -pe "s<\Q++LOCALEDIR++\E><.../share/locale>" <Git/I18N.pm >blib/lib/Git/I18N.pm /usr/bin/perl -pe "s<\Q++LOCALEDIR++\E><.../share/locale>" <Git.pm >blib/lib/Git.pm Manifying blib/man3/Git::I18N.3pm
Manifying blib/man3/Git.3pm
SUBDIR git_remote_helpers

#5 lore raw

Re: [RFC/PATCH] i18n: add infrastructure for translating Git with gettext

Quoted 9 lines · Junio C Hamano

Just an extremely minor issue, but a few things seem to seep through the "make V=''" build:

   SUBDIR perl
/usr/bin/perl -pe "s<\Q++LOCALEDIR++\E><.../share/locale>" <Git/I18N.pm >blib/lib/Git/I18N.pm /usr/bin/perl -pe "s<\Q++LOCALEDIR++\E><.../share/locale>" <Git.pm >blib/lib/Git.pm Manifying blib/man3/Git::I18N.3pm
Manifying blib/man3/Git.3pm
   SUBDIR git_remote_helpers

That behavior hasn't changed with this patch, we just print out more stuff now. Without the patch with V='':

SUBDIR perl
cp Git.pm blib/lib/Git.pm
Manifying blib/man3/Git.3pm

And with it:

        SUBDIR perl
    /usr/bin/perl -pe "s<\Q++LOCALEDIR++\E></home/avar/share/locale>"
<Git/I18N.pm >blib/lib/Git/I18N.pm
    /usr/bin/perl -pe "s<\Q++LOCALEDIR++\E></home/avar/share/locale>"
<Git.pm >blib/lib/Git.pm
    Manifying blib/man3/Git::I18N.3pm
    Manifying blib/man3/Git.3pm

So the change is:

* We now have 2 files instead of 1
* We have a filtering command instead of just a "cp".

The reason this happens is that we have this in the perl.mak:

    PM_FILTER = $(PERL) -pe "s<\Q++LOCALEDIR++\E></home/avar/share/locale>"

And we then later call:

    pm_to_blib : $(FIRST_MAKEFILE) $(TO_INST_PM)
        $(NOECHO) $(ABSPERLRUN) -MExtUtils::Install -e
'pm_to_blib({@ARGV}, '\''$(INST_LIB)/auto'\'', q[$(PM_FILTER)],
'\''$(PERM_DIR)'\'')' -- \
          Git/I18N.pm $(INST_LIBDIR)/Git/I18N.pm \
          Git.pm $(INST_LIBDIR)/Git.pm
        $(NOECHO) $(TOUCH) pm_to_blib

Which in ExtUtils::Install calls pm_to_blib, which is defined like this:

    sub pm_to_blib {
        my($fromto,$autodir,$pm_filter) = @_;

        _mkpath($autodir,0,0755);
        while(my($from, $to) = each %$fromto) {
            if( -f $to && -s $from == -s $to && -M $to < -M $from ) {
                print "Skip $to (unchanged)\n";
                next;
            }

            # When a pm_filter is defined, we need to pre-process the
source first
            # to determine whether it has changed or not.  Therefore,
only perform
            # the comparison check when there's no filter to be ran.
            #    -- RAM, 03/01/2001
            my $need_filtering = defined $pm_filter && length $pm_filter &&
                                 $from =~ /\.pm$/;

            if (!$need_filtering && 0 == compare($from,$to)) {
                print "Skip $to (unchanged)\n";
                next;
            }
            if (-f $to){
                # we wont try hard here. its too likely to mess things up.
                forceunlink($to);
            } else {
                _mkpath(dirname($to),0,0755);
            }
            if ($need_filtering) {
                run_filter($pm_filter, $from, $to);
                print "$pm_filter <$from >$to\n";
            } else {
                _copy( $from, $to );
                print "cp $from $to\n";
            }

I.e. there's no way to stop it from printing what it copies / filters. We could quiet it with some Perl or Makefile hack, but let's try to address that outside of this series.