Skip to content

Commit 54e6dc7

Browse files
pcloudsgitster
authored andcommitted
i18n: parseopt: lookup help and argument translations when showing usage
Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a3935e6 commit 54e6dc7

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

parse-options.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ static int usage_argh(const struct option *opts, FILE *outfile)
490490
s = literal ? "[%s]" : "[<%s>]";
491491
else
492492
s = literal ? " %s" : " <%s>";
493-
return fprintf(outfile, s, opts->argh ? opts->argh : "...");
493+
return fprintf(outfile, s, opts->argh ? _(opts->argh) : _("..."));
494494
}
495495

496496
#define USAGE_OPTS_WIDTH 24
@@ -508,13 +508,16 @@ static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
508508
if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
509509
fprintf(outfile, "cat <<\\EOF\n");
510510

511-
fprintf(outfile, "usage: %s\n", *usagestr++);
511+
fprintf_ln(outfile, _("usage: %s"), _(*usagestr++));
512512
while (*usagestr && **usagestr)
513-
fprintf(outfile, " or: %s\n", *usagestr++);
513+
/* TRANSLATORS: the colon here should align with the
514+
one in "usage: %s" translation */
515+
fprintf_ln(outfile, _(" or: %s"), _(*usagestr++));
514516
while (*usagestr) {
515-
fprintf(outfile, "%s%s\n",
516-
**usagestr ? " " : "",
517-
*usagestr);
517+
if (**usagestr)
518+
fprintf_ln(outfile, _(" %s"), _(*usagestr));
519+
else
520+
putchar('\n');
518521
usagestr++;
519522
}
520523

@@ -528,7 +531,7 @@ static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
528531
if (opts->type == OPTION_GROUP) {
529532
fputc('\n', outfile);
530533
if (*opts->help)
531-
fprintf(outfile, "%s\n", opts->help);
534+
fprintf(outfile, "%s\n", _(opts->help));
532535
continue;
533536
}
534537
if (!full && (opts->flags & PARSE_OPT_HIDDEN))
@@ -558,7 +561,7 @@ static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
558561
fputc('\n', outfile);
559562
pad = USAGE_OPTS_WIDTH;
560563
}
561-
fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
564+
fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", _(opts->help));
562565
}
563566
fputc('\n', outfile);
564567

parse-options.h

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,14 @@ typedef int parse_opt_ll_cb(struct parse_opt_ctx_t *ctx,
6666
*
6767
* `argh`::
6868
* token to explain the kind of argument this option wants. Keep it
69-
* homogeneous across the repository.
69+
* homogeneous across the repository. Should be wrapped by N_() for
70+
* translation.
7071
*
7172
* `help`::
7273
* the short help associated to what the option does.
7374
* Must never be NULL (except for OPTION_END).
7475
* OPTION_GROUP uses this pointer to store the group header.
76+
* Should be wrapped by N_() for translation.
7577
*
7678
* `flags`::
7779
* mask of parse_opt_option_flags.
@@ -128,37 +130,38 @@ struct option {
128130
#define OPT_BOOL(s, l, v, h) OPT_SET_INT(s, l, v, h, 1)
129131
#define OPT_SET_PTR(s, l, v, h, p) { OPTION_SET_PTR, (s), (l), (v), NULL, \
130132
(h), PARSE_OPT_NOARG, NULL, (p) }
131-
#define OPT_INTEGER(s, l, v, h) { OPTION_INTEGER, (s), (l), (v), "n", (h) }
133+
#define OPT_INTEGER(s, l, v, h) { OPTION_INTEGER, (s), (l), (v), N_("n"), (h) }
132134
#define OPT_STRING(s, l, v, a, h) { OPTION_STRING, (s), (l), (v), (a), (h) }
133135
#define OPT_STRING_LIST(s, l, v, a, h) \
134136
{ OPTION_CALLBACK, (s), (l), (v), (a), \
135137
(h), 0, &parse_opt_string_list }
136138
#define OPT_UYN(s, l, v, h) { OPTION_CALLBACK, (s), (l), (v), NULL, \
137139
(h), PARSE_OPT_NOARG, &parse_opt_tertiary }
138140
#define OPT_DATE(s, l, v, h) \
139-
{ OPTION_CALLBACK, (s), (l), (v), "time",(h), 0, \
141+
{ OPTION_CALLBACK, (s), (l), (v), N_("time"),(h), 0, \
140142
parse_opt_approxidate_cb }
141143
#define OPT_CALLBACK(s, l, v, a, h, f) \
142144
{ OPTION_CALLBACK, (s), (l), (v), (a), (h), 0, (f) }
143145
#define OPT_NUMBER_CALLBACK(v, h, f) \
144146
{ OPTION_NUMBER, 0, NULL, (v), NULL, (h), \
145147
PARSE_OPT_NOARG | PARSE_OPT_NONEG, (f) }
146148
#define OPT_FILENAME(s, l, v, h) { OPTION_FILENAME, (s), (l), (v), \
147-
"file", (h) }
149+
N_("file"), (h) }
148150
#define OPT_COLOR_FLAG(s, l, v, h) \
149-
{ OPTION_CALLBACK, (s), (l), (v), "when", (h), PARSE_OPT_OPTARG, \
151+
{ OPTION_CALLBACK, (s), (l), (v), N_("when"), (h), PARSE_OPT_OPTARG, \
150152
parse_opt_color_flag_cb, (intptr_t)"always" }
151153

152154
#define OPT_NOOP_NOARG(s, l) \
153155
{ OPTION_CALLBACK, (s), (l), NULL, NULL, \
154-
"no-op (backward compatibility)", \
156+
N_("no-op (backward compatibility)"), \
155157
PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, parse_opt_noop_cb }
156158

157159
/* Deprecated synonym */
158160
#define OPT_BOOLEAN OPT_COUNTUP
159161

160162
/* parse_options() will filter out the processed options and leave the
161-
* non-option arguments in argv[].
163+
* non-option arguments in argv[]. usagestr strings should be marked
164+
* for translation with N_().
162165
* Returns the number of arguments left in argv[].
163166
*/
164167
extern int parse_options(int argc, const char **argv, const char *prefix,
@@ -222,15 +225,15 @@ extern int parse_opt_noop_cb(const struct option *, const char *, int);
222225
#define OPT__VERBOSE(var, h) OPT_BOOLEAN('v', "verbose", (var), (h))
223226
#define OPT__QUIET(var, h) OPT_BOOLEAN('q', "quiet", (var), (h))
224227
#define OPT__VERBOSITY(var) \
225-
{ OPTION_CALLBACK, 'v', "verbose", (var), NULL, "be more verbose", \
228+
{ OPTION_CALLBACK, 'v', "verbose", (var), NULL, N_("be more verbose"), \
226229
PARSE_OPT_NOARG, &parse_opt_verbosity_cb, 0 }, \
227-
{ OPTION_CALLBACK, 'q', "quiet", (var), NULL, "be more quiet", \
230+
{ OPTION_CALLBACK, 'q', "quiet", (var), NULL, N_("be more quiet"), \
228231
PARSE_OPT_NOARG, &parse_opt_verbosity_cb, 0 }
229232
#define OPT__DRY_RUN(var, h) OPT_BOOLEAN('n', "dry-run", (var), (h))
230233
#define OPT__FORCE(var, h) OPT_BOOLEAN('f', "force", (var), (h))
231234
#define OPT__ABBREV(var) \
232-
{ OPTION_CALLBACK, 0, "abbrev", (var), "n", \
233-
"use <n> digits to display SHA-1s", \
235+
{ OPTION_CALLBACK, 0, "abbrev", (var), N_("n"), \
236+
N_("use <n> digits to display SHA-1s"), \
234237
PARSE_OPT_OPTARG, &parse_opt_abbrev_cb, 0 }
235238
#define OPT__COLOR(var, h) \
236239
OPT_COLOR_FLAG(0, "color", (var), (h))

0 commit comments

Comments
 (0)