i18n: add--interactive: mark plural strings

Mark plural strings for translation.  Unfold each action case in one
entire sentence.

Pass new keyword for xgettext to extract.

Update test to include new subroutine __n() for plural strings handling.

Update documentation to include a description of the new __n()
subroutine.

Signed-off-by: Vasco Almeida <vascomalmeida@sapo.pt>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Vasco Almeida
2016-12-14 11:54:29 -01:00
committed by Junio C Hamano
parent 901707babc
commit c4a85c3b8e
4 changed files with 39 additions and 12 deletions

View File

@ -2109,7 +2109,8 @@ XGETTEXT_FLAGS_C = $(XGETTEXT_FLAGS) --language=C \
--keyword=_ --keyword=N_ --keyword="Q_:1,2" --keyword=_ --keyword=N_ --keyword="Q_:1,2"
XGETTEXT_FLAGS_SH = $(XGETTEXT_FLAGS) --language=Shell \ XGETTEXT_FLAGS_SH = $(XGETTEXT_FLAGS) --language=Shell \
--keyword=gettextln --keyword=eval_gettextln --keyword=gettextln --keyword=eval_gettextln
XGETTEXT_FLAGS_PERL = $(XGETTEXT_FLAGS) --keyword=__ --language=Perl XGETTEXT_FLAGS_PERL = $(XGETTEXT_FLAGS) --language=Perl \
--keyword=__ --keyword="__n:1,2"
LOCALIZED_C = $(C_OBJ:o=c) $(LIB_H) $(GENERATED_H) LOCALIZED_C = $(C_OBJ:o=c) $(LIB_H) $(GENERATED_H)
LOCALIZED_SH = $(SCRIPT_SH) LOCALIZED_SH = $(SCRIPT_SH)
LOCALIZED_SH += git-parse-remote.sh LOCALIZED_SH += git-parse-remote.sh

View File

@ -668,12 +668,18 @@ sub status_cmd {
sub say_n_paths { sub say_n_paths {
my $did = shift @_; my $did = shift @_;
my $cnt = scalar @_; my $cnt = scalar @_;
print "$did "; if ($did eq 'added') {
if (1 < $cnt) { printf(__n("added %d path\n", "added %d paths\n",
print "$cnt paths\n"; $cnt), $cnt);
} } elsif ($did eq 'updated') {
else { printf(__n("updated %d path\n", "updated %d paths\n",
print "one path\n"; $cnt), $cnt);
} elsif ($did eq 'reverted') {
printf(__n("reverted %d path\n", "reverted %d paths\n",
$cnt), $cnt);
} else {
printf(__n("touched %d path\n", "touched %d paths\n",
$cnt), $cnt);
} }
} }
@ -1420,7 +1426,8 @@ sub patch_update_file {
} elsif (0 < $response && $response <= $num) { } elsif (0 < $response && $response <= $num) {
$ix = $response - 1; $ix = $response - 1;
} else { } else {
error_msg "Sorry, only $num hunks available.\n"; error_msg sprintf(__n("Sorry, only %d hunk available.\n",
"Sorry, only %d hunks available.\n", $num), $num);
} }
next; next;
} }
@ -1515,8 +1522,10 @@ sub patch_update_file {
elsif ($other =~ /s/ && $line =~ /^s/) { elsif ($other =~ /s/ && $line =~ /^s/) {
my @split = split_hunk($hunk[$ix]{TEXT}, $hunk[$ix]{DISPLAY}); my @split = split_hunk($hunk[$ix]{TEXT}, $hunk[$ix]{DISPLAY});
if (1 < @split) { if (1 < @split) {
print colored $header_color, "Split into ", print colored $header_color, sprintf(
scalar(@split), " hunks.\n"; __n("Split into %d hunk.\n",
"Split into %d hunks.\n",
scalar(@split)), scalar(@split));
} }
splice (@hunk, $ix, 1, @split); splice (@hunk, $ix, 1, @split);
$num = scalar @hunk; $num = scalar @hunk;

View File

@ -13,7 +13,7 @@ BEGIN {
} }
} }
our @EXPORT = qw(__); our @EXPORT = qw(__ __n);
our @EXPORT_OK = @EXPORT; our @EXPORT_OK = @EXPORT;
sub __bootstrap_locale_messages { sub __bootstrap_locale_messages {
@ -44,6 +44,7 @@ BEGIN
eval { eval {
__bootstrap_locale_messages(); __bootstrap_locale_messages();
*__ = \&Locale::Messages::gettext; *__ = \&Locale::Messages::gettext;
*__n = \&Locale::Messages::ngettext;
1; 1;
} or do { } or do {
# Tell test.pl that we couldn't load the gettext library. # Tell test.pl that we couldn't load the gettext library.
@ -51,6 +52,7 @@ BEGIN
# Just a fall-through no-op # Just a fall-through no-op
*__ = sub ($) { $_[0] }; *__ = sub ($) { $_[0] };
*__n = sub ($$$) { $_[2] == 1 ? $_[0] : $_[1] };
}; };
} }
@ -70,6 +72,8 @@ Git::I18N - Perl interface to Git's Gettext localizations
printf __("The following error occurred: %s\n"), $error; printf __("The following error occurred: %s\n"), $error;
printf __n("commited %d file\n", "commited %d files\n", $files), $files;
=head1 DESCRIPTION =head1 DESCRIPTION
Git's internal Perl interface to gettext via L<Locale::Messages>. If Git's internal Perl interface to gettext via L<Locale::Messages>. If
@ -87,6 +91,10 @@ it.
L<Locale::Messages>'s gettext function if all goes well, otherwise our L<Locale::Messages>'s gettext function if all goes well, otherwise our
passthrough fallback function. passthrough fallback function.
=head2 __n($$$)
L<Locale::Messages>'s ngettext function or passthrough fallback function.
=head1 AUTHOR =head1 AUTHOR
E<AElig>var ArnfjE<ouml>rE<eth> Bjarmason <avarab@gmail.com> E<AElig>var ArnfjE<ouml>rE<eth> Bjarmason <avarab@gmail.com>

View File

@ -4,7 +4,7 @@ use lib (split(/:/, $ENV{GITPERLLIB}));
use strict; use strict;
use warnings; use warnings;
use POSIX qw(:locale_h); use POSIX qw(:locale_h);
use Test::More tests => 8; use Test::More tests => 11;
use Git::I18N; use Git::I18N;
my $has_gettext_library = $Git::I18N::__HAS_LIBRARY; my $has_gettext_library = $Git::I18N::__HAS_LIBRARY;
@ -31,6 +31,7 @@ is_deeply(\@Git::I18N::EXPORT, \@Git::I18N::EXPORT_OK, "sanity: Git::I18N export
# more gettext wrapper functions. # more gettext wrapper functions.
my %prototypes = (qw( my %prototypes = (qw(
__ $ __ $
__n $$$
)); ));
while (my ($sub, $proto) = each %prototypes) { while (my ($sub, $proto) = each %prototypes) {
is(prototype(\&{"Git::I18N::$sub"}), $proto, "sanity: $sub has a $proto prototype"); is(prototype(\&{"Git::I18N::$sub"}), $proto, "sanity: $sub has a $proto prototype");
@ -46,6 +47,14 @@ is_deeply(\@Git::I18N::EXPORT, \@Git::I18N::EXPORT_OK, "sanity: Git::I18N export
my ($got, $expect) = (('TEST: A Perl test string') x 2); my ($got, $expect) = (('TEST: A Perl test string') x 2);
is(__($got), $expect, "Passing a string through __() in the C locale works"); is(__($got), $expect, "Passing a string through __() in the C locale works");
my ($got_singular, $got_plural, $expect_singular, $expect_plural) =
(('TEST: 1 file', 'TEST: n files') x 2);
is(__n($got_singular, $got_plural, 1), $expect_singular,
"Get singular string through __n() in C locale");
is(__n($got_singular, $got_plural, 2), $expect_plural,
"Get plural string through __n() in C locale");
} }
# Test a basic message on different locales # Test a basic message on different locales