i18n: add--interactive: mark patch prompt for translation

Mark prompt message assembled in place for translation, unfolding each use case for each entry in the %patch_modes hash table. Previously, this script relied on whether $patch_mode was set to run the command patch_update_cmd() or show status and loop the main loop. Now, it uses $cmd to indicate we must run patch_update_cmd() and $patch_mode is used to tell which flavor of the %patch_modes are we on. This is introduced in order to be able to mark and unfold the message prompt knowing in which context we are. The tracking of context was done previously by point %patch_mode_flavour hash table to the correct entry of %patch_modes, focusing only on value of %patch_modes. Now, we are also interested in the key ('staged', 'stash', 'checkout_head', ...). Signed-off-by: Vasco Almeida <vascomalmeida@sapo.pt> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Vasco Almeida committed Dec 14, 2016 at 11:54 UTC 0539d5e6d535760a99694ff5d5592e46ed929d15
4 files changed +61 -11
Makefile
+1 -1
@@ -2110,7 +2110,7 @@ XGETTEXT_FLAGS_C = $(XGETTEXT_FLAGS) --language=C \
2110 XGETTEXT_FLAGS_SH = $(XGETTEXT_FLAGS) --language=Shell \
2111 --keyword=gettextln --keyword=eval_gettextln
2112 XGETTEXT_FLAGS_PERL = $(XGETTEXT_FLAGS) --language=Perl \
2113 - --keyword=__ --keyword="__n:1,2"
2113 + --keyword=__ --keyword=N__ --keyword="__n:1,2"
2114 LOCALIZED_C = $(C_OBJ:o=c) $(LIB_H) $(GENERATED_H)
2115 LOCALIZED_SH = $(SCRIPT_SH)
2116 LOCALIZED_SH += git-parse-remote.sh
git-add--interactive.perl
+46 -8
@@ -92,6 +92,7 @@ sub colored {
92 }
93
94 # command line options
95 +my $cmd;
96 my $patch_mode;
97 my $patch_mode_revision;
98
@@ -172,7 +173,8 @@ my %patch_modes = (
173 },
174 );
175
175 -my %patch_mode_flavour = %{$patch_modes{stage}};
176 +$patch_mode = 'stage';
177 +my %patch_mode_flavour = %{$patch_modes{$patch_mode}};
178
179 sub run_cmd_pipe {
180 if ($^O eq 'MSWin32') {
@@ -1308,6 +1310,44 @@ sub display_hunks {
1310 return $i;
1311 }
1312
1313 +my %patch_update_prompt_modes = (
1314 + stage => {
1315 + mode => N__("Stage mode change [y,n,q,a,d,/%s,?]? "),
1316 + deletion => N__("Stage deletion [y,n,q,a,d,/%s,?]? "),
1317 + hunk => N__("Stage this hunk [y,n,q,a,d,/%s,?]? "),
1318 + },
1319 + stash => {
1320 + mode => N__("Stash mode change [y,n,q,a,d,/%s,?]? "),
1321 + deletion => N__("Stash deletion [y,n,q,a,d,/%s,?]? "),
1322 + hunk => N__("Stash this hunk [y,n,q,a,d,/%s,?]? "),
1323 + },
1324 + reset_head => {
1325 + mode => N__("Unstage mode change [y,n,q,a,d,/%s,?]? "),
1326 + deletion => N__("Unstage deletion [y,n,q,a,d,/%s,?]? "),
1327 + hunk => N__("Unstage this hunk [y,n,q,a,d,/%s,?]? "),
1328 + },
1329 + reset_nothead => {
1330 + mode => N__("Apply mode change to index [y,n,q,a,d,/%s,?]? "),
1331 + deletion => N__("Apply deletion to index [y,n,q,a,d,/%s,?]? "),
1332 + hunk => N__("Apply this hunk to index [y,n,q,a,d,/%s,?]? "),
1333 + },
1334 + checkout_index => {
1335 + mode => N__("Discard mode change from worktree [y,n,q,a,d,/%s,?]? "),
1336 + deletion => N__("Discard deletion from worktree [y,n,q,a,d,/%s,?]? "),
1337 + hunk => N__("Discard this hunk from worktree [y,n,q,a,d,/%s,?]? "),
1338 + },
1339 + checkout_head => {
1340 + mode => N__("Discard mode change from index and worktree [y,n,q,a,d,/%s,?]? "),
1341 + deletion => N__("Discard deletion from index and worktree [y,n,q,a,d,/%s,?]? "),
1342 + hunk => N__("Discard this hunk from index and worktree [y,n,q,a,d,/%s,?]? "),
1343 + },
1344 + checkout_nothead => {
1345 + mode => N__("Apply mode change to index and worktree [y,n,q,a,d,/%s,?]? "),
1346 + deletion => N__("Apply deletion to index and worktree [y,n,q,a,d,/%s,?]? "),
1347 + hunk => N__("Apply this hunk to index and worktree [y,n,q,a,d,/%s,?]? "),
1348 + },
1349 +);
1350 +
1351 sub patch_update_file {
1352 my $quit = 0;
1353 my ($ix, $num);
@@ -1380,12 +1420,9 @@ sub patch_update_file {
1420 for (@{$hunk[$ix]{DISPLAY}}) {
1421 print;
1422 }
1383 - print colored $prompt_color, $patch_mode_flavour{VERB},
1384 - ($hunk[$ix]{TYPE} eq 'mode' ? ' mode change' :
1385 - $hunk[$ix]{TYPE} eq 'deletion' ? ' deletion' :
1386 - ' this hunk'),
1387 - $patch_mode_flavour{TARGET},
1388 - " [y,n,q,a,d,/$other,?]? ";
1423 + print colored $prompt_color,
1424 + sprintf(__($patch_update_prompt_modes{$patch_mode}{$hunk[$ix]{TYPE}}), $other);
1425 +
1426 my $line = prompt_single_character;
1427 last unless defined $line;
1428 if ($line) {
@@ -1641,6 +1678,7 @@ sub process_args {
1678 die sprintf(__("invalid argument %s, expecting --"),
1679 $arg) unless $arg eq "--";
1680 %patch_mode_flavour = %{$patch_modes{$patch_mode}};
1681 + $cmd = 1;
1682 }
1683 elsif ($arg ne "--") {
1684 die sprintf(__("invalid argument %s, expecting --"), $arg);
@@ -1677,7 +1715,7 @@ sub main_loop {
1715
1716 process_args();
1717 refresh();
1680 -if ($patch_mode) {
1718 +if ($cmd) {
1719 patch_update_cmd();
1720 }
1721 else {
perl/Git/I18N.pm
+10 -1
@@ -13,7 +13,7 @@ BEGIN {
13 }
14 }
15
16 -our @EXPORT = qw(__ __n);
16 +our @EXPORT = qw(__ __n N__);
17 our @EXPORT_OK = @EXPORT;
18
19 sub __bootstrap_locale_messages {
@@ -54,6 +54,8 @@ BEGIN
54 *__ = sub ($) { $_[0] };
55 *__n = sub ($$$) { $_[2] == 1 ? $_[0] : $_[1] };
56 };
57 +
58 + sub N__($) { return shift; }
59 }
60
61 1;
@@ -74,6 +76,7 @@ Git::I18N - Perl interface to Git's Gettext localizations
76
77 printf __n("commited %d file\n", "commited %d files\n", $files), $files;
78
79 +
80 =head1 DESCRIPTION
81
82 Git's internal Perl interface to gettext via L<Locale::Messages>. If
@@ -95,6 +98,12 @@ passthrough fallback function.
98
99 L<Locale::Messages>'s ngettext function or passthrough fallback function.
100
101 +=head2 N__($)
102 +
103 +No-operation that only returns its argument. Use this if you want xgettext to
104 +extract the text to the pot template but do not want to trigger retrival of the
105 +translation at run time.
106 +
107 =head1 AUTHOR
108
109 E<AElig>var ArnfjE<ouml>rE<eth> Bjarmason <avarab@gmail.com>
t/t0202/test.pl
+4 -1
@@ -4,7 +4,7 @@ use lib (split(/:/, $ENV{GITPERLLIB}));
4 use strict;
5 use warnings;
6 use POSIX qw(:locale_h);
7 -use Test::More tests => 11;
7 +use Test::More tests => 13;
8 use Git::I18N;
9
10 my $has_gettext_library = $Git::I18N::__HAS_LIBRARY;
@@ -32,6 +32,7 @@ is_deeply(\@Git::I18N::EXPORT, \@Git::I18N::EXPORT_OK, "sanity: Git::I18N export
32 my %prototypes = (qw(
33 __ $
34 __n $$$
35 + N__ $
36 ));
37 while (my ($sub, $proto) = each %prototypes) {
38 is(prototype(\&{"Git::I18N::$sub"}), $proto, "sanity: $sub has a $proto prototype");
@@ -55,6 +56,8 @@ is_deeply(\@Git::I18N::EXPORT, \@Git::I18N::EXPORT_OK, "sanity: Git::I18N export
56 "Get singular string through __n() in C locale");
57 is(__n($got_singular, $got_plural, 2), $expect_plural,
58 "Get plural string through __n() in C locale");
59 +
60 + is(N__($got), $expect, "Passing a string through N__() in the C locale works");
61 }
62
63 # Test a basic message on different locales