git/t/t0200-gettext-basic.sh

109 lines
4.1 KiB
Bash
Raw Normal View History

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 libintl 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 falls 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's 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 <AANLkTilYD_NyIZMyj9dHtVk-ylVBfvyxpCC7982LWnVd@mail.gmail.com> 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. = 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. =Credits This patch is based on work by Jeff Epler <jepler@unpythonic.net> who did the initial Makefile / C work, and a lot of comments from the Git mailing list, including Jonathan Nieder, Jakub Narebski, Johannes Sixt, Erik Faye-Lund, Peter Krefting, Junio C Hamano, Thomas Rast and others. [jc: squashed a small Makefile fix from Ramsay] Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>
2011-11-18 07:14:42 +08:00
#!/bin/sh
#
# Copyright (c) 2010 Ævar Arnfjörð Bjarmason
#
test_description='Gettext support for Git'
. ./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: $TEXTDOMAIN is git' '
test $TEXTDOMAIN = "git"
'
test_expect_success 'xgettext sanity: Perl _() strings are not extracted' '
! grep "A Perl string xgettext will not get" "$GIT_PO_PATH"/is.po
'
test_expect_success 'xgettext sanity: Comment extraction with --add-comments' '
grep "TRANSLATORS: This is a test" "$TEST_DIRECTORY"/t0200/* | wc -l >expect &&
grep "TRANSLATORS: This is a test" "$GIT_PO_PATH"/is.po | wc -l >actual &&
test_cmp expect actual
'
test_expect_success 'xgettext sanity: Comment extraction with --add-comments stops at statements' '
! grep "This is a phony" "$GIT_PO_PATH"/is.po &&
! grep "the above comment" "$GIT_PO_PATH"/is.po
'
test_expect_success GETTEXT 'sanity: $TEXTDOMAINDIR exists without NO_GETTEXT=YesPlease' '
test -d "$TEXTDOMAINDIR" &&
test "$TEXTDOMAINDIR" = "$GIT_TEXTDOMAINDIR"
'
test_expect_success GETTEXT 'sanity: Icelandic locale was compiled' '
test -f "$TEXTDOMAINDIR/is/LC_MESSAGES/git.mo"
'
# TODO: When we have more locales, generalize this to test them
# all. Maybe we'll need a dir->locale map for that.
test_expect_success GETTEXT_LOCALE 'sanity: gettext("") metadata is OK' '
# Return value may be non-zero
LANGUAGE=is LC_ALL="$is_IS_locale" gettext "" >zero-expect &&
grep "Project-Id-Version: Git" zero-expect &&
grep "Git Mailing List <git@vger.kernel.org>" zero-expect &&
grep "Content-Type: text/plain; charset=UTF-8" zero-expect &&
grep "Content-Transfer-Encoding: 8bit" zero-expect
'
test_expect_success GETTEXT_LOCALE 'sanity: gettext(unknown) is passed through' '
printf "This is not a translation string" >expect &&
gettext "This is not a translation string" >actual &&
eval_gettext "This is not a translation string" >actual &&
test_cmp expect actual
'
# xgettext from C
test_expect_success GETTEXT_LOCALE 'xgettext: C extraction of _() and N_() strings' '
printf "TILRAUN: C tilraunastrengur" >expect &&
printf "\n" >>expect &&
printf "Sjá '\''git help SKIPUN'\'' til að sjá hjálp fyrir tiltekna skipun." >>expect &&
LANGUAGE=is LC_ALL="$is_IS_locale" gettext "TEST: A C test string" >actual &&
printf "\n" >>actual &&
LANGUAGE=is LC_ALL="$is_IS_locale" gettext "See '\''git help COMMAND'\'' for more information on a specific command." >>actual &&
test_cmp expect actual
'
test_expect_success GETTEXT_LOCALE 'xgettext: C extraction with %s' '
printf "TILRAUN: C tilraunastrengur %%s" >expect &&
LANGUAGE=is LC_ALL="$is_IS_locale" gettext "TEST: A C test string %s" >actual &&
test_cmp expect actual
'
# xgettext from Shell
test_expect_success GETTEXT_LOCALE 'xgettext: Shell extraction' '
printf "TILRAUN: Skeljartilraunastrengur" >expect &&
LANGUAGE=is LC_ALL="$is_IS_locale" gettext "TEST: A Shell test string" >actual &&
test_cmp expect actual
'
test_expect_success GETTEXT_LOCALE 'xgettext: Shell extraction with $variable' '
printf "TILRAUN: Skeljartilraunastrengur með breytunni a var i able" >x-expect &&
LANGUAGE=is LC_ALL="$is_IS_locale" variable="a var i able" eval_gettext "TEST: A Shell test \$variable" >x-actual &&
test_cmp x-expect x-actual
'
# xgettext from Perl
test_expect_success GETTEXT_LOCALE 'xgettext: Perl extraction' '
printf "TILRAUN: Perl tilraunastrengur" >expect &&
LANGUAGE=is LC_ALL="$is_IS_locale" gettext "TEST: A Perl test string" >actual &&
test_cmp expect actual
'
test_expect_success GETTEXT_LOCALE 'xgettext: Perl extraction with %s' '
printf "TILRAUN: Perl tilraunastrengur með breytunni %%s" >expect &&
LANGUAGE=is LC_ALL="$is_IS_locale" gettext "TEST: A Perl test variable %s" >actual &&
test_cmp expect actual
'
test_expect_success GETTEXT_LOCALE 'sanity: Some gettext("") data for real locale' '
LANGUAGE=is LC_ALL="$is_IS_locale" gettext "" >real-locale &&
test -s real-locale
'
test_done