Skip to contentSkip to conversation

i18n-ize relative dates, git-help and parseopt

PATCH In review Nguyễn Thái Ngọc Duy 4 participants 15 messages Jun 15, 2016 Kbuild

lore
Share & tools
lore
#0 lore raw

cover i18n-ize relative dates, git-help and parseopt

Except "git log --date=relative", the focus of this series is "git help" and "git blah -h" (only "git help -h" for the latter because other commands need a lot of N_() additions). I think I got everything from "git help [options]" fully translatable.

Nguyễn Thái Ngọc Duy (5):
i18n: keep the last \n even when text is poisoned
i18n: mark relative dates for translation
i18n: parseopt: lookup help and argument translations when showing
usage
i18n: help: mark parseopt strings for translation
i18n: help: mark strings for translation

 Makefile            |    2 +-
 builtin/help.c      |   58 +++++++++++++++++++++++++-------------------------
 date.c              |   46 +++++++++++++++++++++++++---------------
 generate-cmdlist.sh |    2 +-
 gettext.c           |   10 ++++++++
 gettext.h           |    6 +++-
 git.c               |    2 +-
 help.c              |   34 ++++++++++++++++++-----------
 parse-options.c     |   15 ++++++-------
 9 files changed, 103 insertions(+), 72 deletions(-)
Signature · 1 line
1.7.3.1.256.g2539c.dirty
#1 lore raw

PATCH 1/5 i18n: keep the last \n even when text is poisoned

This helps maintain the layout somewhat when translatable text is poisoned.

---
 gettext.c |   10 ++++++++++
 gettext.h |    6 ++++--
 2 files changed, 14 insertions(+), 2 deletions(-)
gettext.c +10 -0
index f75bca7..60bc2ad 100644
@@ -24,6 +24,16 @@ int use_gettext_poison(void)
 		poison_requested = getenv("GIT_GETTEXT_POISON") ? 1 : 0;
 	return poison_requested;
 }
+
+const char *poison_text(const char *msgid)
+{
+	int len = strlen(msgid);
+	if (len && msgid[len-1] == '\n')
+		return "# GETTEXT POISON #\n";
+	else
+		return "# GETTEXT POISON #";
+}
+
 #endif
 
 #ifndef NO_GETTEXT
gettext.h +4 -2
index 57ba8bb..2313c84 100644
@@ -38,20 +38,22 @@ static inline void git_setup_gettext(void)
 
 #ifdef GETTEXT_POISON
 extern int use_gettext_poison(void);
+extern const char *poison_text(const char *msgid);
 #else
 #define use_gettext_poison() 0
+#define poison_text(x) NULL
 #endif
 
 static inline FORMAT_PRESERVING(1) const char *_(const char *msgid)
 {
-	return use_gettext_poison() ? "# GETTEXT POISON #" : gettext(msgid);
+	return use_gettext_poison() ? poison_text(msgid) : gettext(msgid);
 }
 
 static inline FORMAT_PRESERVING(1) FORMAT_PRESERVING(2)
 const char *Q_(const char *msgid, const char *plu, unsigned long n)
 {
 	if (use_gettext_poison())
-		return "# GETTEXT POISON #";
+		return poison_text(msgid);
 	return ngettext(msgid, plu, n);
 }
 
Signature · 1 line
1.7.3.1.256.g2539c.dirty
#2 lore raw

Re: [PATCH 1/5] i18n: keep the last \n even when text is poisoned

Quoted 4 lines · Nguyễn Thái Ngọc Duy
gettext.c +0 -0
@@ -24,6 +24,16 @@ int use_gettext_poison(void)
 		poison_requested = getenv("GIT_GETTEXT_POISON") ? 1 : 0;

gettext.c int use_gettext_poison(void) · line 34

Quoted 10 lines
gettext.c +8 -0
 	return poison_requested;
 }
+
+const char *poison_text(const char *msgid)
+{
+	int len = strlen(msgid);
+	if (len && msgid[len-1] == '\n')
+		return "# GETTEXT POISON #\n";
+	else
+		return "# GETTEXT POISON #";

I realize this was not the motivation behind the above patch, but if the translation of some message has to end with a newline for git to function correctly, would we consider that a bug?

I am of two minds on that:

- on one hand, translators tend to be trustworthy, reasonable folks
- on the other hand, anything we can do to make the translation process less fussy seems like time well spent

The latter wins out for me, so I would prefer not to have this patch so the test suite can detect important newlines that should be not be part of the translatable string.

Just my two cents,
Jonathan

#3 lore raw

Re: [PATCH 1/5] i18n: keep the last \n even when text is poisoned

2012/3/8 Jonathan Nieder <[email protected]>:

Quoted 3 lines

The latter wins out for me, so I would prefer not to have this patch so the test suite can detect important newlines that should be not be part of the translatable string.

FWIW that's why I kept it this way. I can't recall the specific cases but I smoked out some issues with the poison feature that I wouldn't have smoked out if it had a \n at the end.

Arguably we should make it even less user friendly and have it randomly output 10-50 bytes of binary garbage to catch more errors.

#4 lore raw

Re: [PATCH 1/5] i18n: keep the last \n even when text is poisoned

In reply to Jonathan Nieder

2012/3/9 Jonathan Nieder <[email protected]>:

Quoted 7 lines

I realize this was not the motivation behind the above patch, but if the translation of some message has to end with a newline for git to function correctly, would we consider that a bug?

I am of two minds on that:

 - on one hand, translators tend to be trustworthy, reasonable folks

We trust msgfmt and it does check \n in msgstr if there is in msgid.

Quoted 2 lines

 - on the other hand, anything we can do to make the translation process less fussy seems like time well spent

That's at the cost of more code to split \n out of the strings, and there are 113 of them (some not published yet).

Quoted 3 lines

The latter wins out for me, so I would prefer not to have this patch so the test suite can detect important newlines that should be not be part of the translatable string.

It's the former for me, but that's probably because I'm also a translator :) Anyway for tricky cases where msgfmt fails to detect, there's the role of l10n coordinators, who should review translation and catch these errors.

Quoted 2 lines

Just my two cents,
Jonathan

Signature · 1 line
Duy
#5 lore raw

Re: [PATCH 1/5] i18n: keep the last \n even when text is poisoned

Quoted 4 lines · Nguyen Thai Ngoc Duy

It's the former for me, but that's probably because I'm also a translator :) Anyway for tricky cases where msgfmt fails to detect, there's the role of l10n coordinators, who should review translation and catch these errors.

On second thought, most of call sites are [fs]printf or strbuf_addf, and we have to update the call sites anyway to mark for translation. If we introduce [fs]printf_ln, changes should be minimum. So now I'm on your side too.

Signature · 1 line
Duy
#6 lore raw

PATCH 2/5 i18n: mark relative dates for translation

In reply to Nguyễn Thái Ngọc Duy

English dates get correct plural/singular form as a side effect.

---
 date.c |   46 +++++++++++++++++++++++++++++-----------------
 1 files changed, 29 insertions(+), 17 deletions(-)
date.c +29 -17
index a5055ca..063f344 100644
@@ -93,38 +93,46 @@ const char *show_date_relative(unsigned long time, int tz,
 {
 	unsigned long diff;
 	if (now->tv_sec < time)
-		return "in the future";
+		return _("in the future");
 	diff = now->tv_sec - time;
 	if (diff < 90) {
-		snprintf(timebuf, timebuf_size, "%lu seconds ago", diff);
+		snprintf(timebuf, timebuf_size,
+			 Q_("%lu second ago", "%lu seconds ago", diff), diff);
 		return timebuf;
 	}
 	/* Turn it into minutes */
 	diff = (diff + 30) / 60;
 	if (diff < 90) {
-		snprintf(timebuf, timebuf_size, "%lu minutes ago", diff);
+		snprintf(timebuf, timebuf_size,
+			 Q_("%lu minute ago", "%lu minutes ago", diff), diff);
 		return timebuf;
 	}
 	/* Turn it into hours */
 	diff = (diff + 30) / 60;
 	if (diff < 36) {
-		snprintf(timebuf, timebuf_size, "%lu hours ago", diff);
+		snprintf(timebuf, timebuf_size,
+			 Q_("%lu hour ago", "%lu hours ago", diff), diff);
 		return timebuf;
 	}
 	/* We deal with number of days from here on */
 	diff = (diff + 12) / 24;
 	if (diff < 14) {
-		snprintf(timebuf, timebuf_size, "%lu days ago", diff);
+		snprintf(timebuf, timebuf_size,
+			 Q_("%lu day ago", "%lu days ago", diff), diff);
 		return timebuf;
 	}
 	/* Say weeks for the past 10 weeks or so */
 	if (diff < 70) {
-		snprintf(timebuf, timebuf_size, "%lu weeks ago", (diff + 3) / 7);
+		snprintf(timebuf, timebuf_size,
+			 Q_("%lu week ago", "%lu weeks ago", (diff + 3) / 7),
+			 (diff + 3) / 7);
 		return timebuf;
 	}
 	/* Say months for the past 12 months or so */
 	if (diff < 365) {
-		snprintf(timebuf, timebuf_size, "%lu months ago", (diff + 15) / 30);
+		snprintf(timebuf, timebuf_size,
+			 Q_("%lu month ago", "%lu months ago", (diff + 15) / 30),
+			 (diff + 15) / 30);
 		return timebuf;
 	}
 	/* Give years and months for 5 years or so */
@@ -132,19 +140,23 @@ const char *show_date_relative(unsigned long time, int tz,
 		unsigned long totalmonths = (diff * 12 * 2 + 365) / (365 * 2);
 		unsigned long years = totalmonths / 12;
 		unsigned long months = totalmonths % 12;
-		int n;
-		n = snprintf(timebuf, timebuf_size, "%lu year%s",
-				years, (years > 1 ? "s" : ""));
-		if (months)
-			snprintf(timebuf + n, timebuf_size - n,
-					", %lu month%s ago",
-					months, (months > 1 ? "s" : ""));
-		else
-			snprintf(timebuf + n, timebuf_size - n, " ago");
+		if (months) {
+			struct strbuf sb = STRBUF_INIT;
+			strbuf_addf(&sb, Q_("%lu year", "%lu years", years), years);
+			/* TRANSLATORS: "%s" is "<n> years" */
+			snprintf(timebuf, timebuf_size,
+				 Q_("%s, %lu month ago", "%s, %lu months ago", months),
+				 sb.buf, months);
+			strbuf_release(&sb);
+		} else
+			snprintf(timebuf, timebuf_size,
+				 Q_("%lu year ago", "%lu years ago", years), years);
 		return timebuf;
 	}
 	/* Otherwise, just years. Centuries is probably overkill. */
-	snprintf(timebuf, timebuf_size, "%lu years ago", (diff + 183) / 365);
+	snprintf(timebuf, timebuf_size,
+		 Q_("%lu year ago", "%lu years ago", (diff + 183) / 365),
+		 (diff + 183) / 365);
 	return timebuf;
 }
 
Signature · 1 line
1.7.3.1.256.g2539c.dirty
#11 lore raw

PATCH 3/5 i18n: parseopt: lookup help and argument translations when showing usage

In reply to Nguyễn Thái Ngọc Duy

"struct option" and the help usage array needs N_() marking on help and argument text for this to work.

---
 parse-options.c |   15 +++++++--------
 1 files changed, 7 insertions(+), 8 deletions(-)
parse-options.c +7 -8
index 850cfa7..6247c20 100644
@@ -490,7 +490,7 @@ static int usage_argh(const struct option *opts, FILE *outfile)
 			s = literal ? "[%s]" : "[<%s>]";
 	else
 		s = literal ? " %s" : " <%s>";
-	return fprintf(outfile, s, opts->argh ? opts->argh : "...");
+	return fprintf(outfile, s, opts->argh ? gettext(opts->argh) : "...");
 }
 
 #define USAGE_OPTS_WIDTH 24
@@ -508,13 +508,12 @@ static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
 	if (!err && ctx && ctx->flags & PARSE_OPT_SHELL_EVAL)
 		fprintf(outfile, "cat <<\\EOF\n");
 
-	fprintf(outfile, "usage: %s\n", *usagestr++);
+	fprintf(outfile, _("usage: %s\n"), gettext(*usagestr++));
 	while (*usagestr && **usagestr)
-		fprintf(outfile, "   or: %s\n", *usagestr++);
+		fprintf(outfile, _("   or: %s\n"), gettext(*usagestr++));
 	while (*usagestr) {
-		fprintf(outfile, "%s%s\n",
-				**usagestr ? "    " : "",
-				*usagestr);
+		fprintf(outfile, "%s%s\n", **usagestr ? "    " : "",
+			gettext(*usagestr));
 		usagestr++;
 	}
 
@@ -528,7 +527,7 @@ static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
 		if (opts->type == OPTION_GROUP) {
 			fputc('\n', outfile);
 			if (*opts->help)
-				fprintf(outfile, "%s\n", opts->help);
+				fprintf(outfile, "%s\n", gettext(opts->help));
 			continue;
 		}
 		if (!full && (opts->flags & PARSE_OPT_HIDDEN))
@@ -558,7 +557,7 @@ static int usage_with_options_internal(struct parse_opt_ctx_t *ctx,
 			fputc('\n', outfile);
 			pad = USAGE_OPTS_WIDTH;
 		}
-		fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
+		fprintf(outfile, "%*s%s\n", pad + USAGE_GAP, "", gettext(opts->help));
 	}
 	fputc('\n', outfile);
 
Signature · 1 line
1.7.3.1.256.g2539c.dirty
#12 lore raw

Re: [PATCH 3/5] i18n: parseopt: lookup help and argument translations when showing usage

parse-options.c static int usage_argh(const struct option *opts, FILE *outfile) · line 493

Quoted 8 lines · Nguyễn Thái Ngọc Duy
parse-options.c +1 -1
@@ -490,7 +490,7 @@ static int usage_argh(const struct option *opts, FILE *outfile)
 			s = literal ? "[%s]" : "[<%s>]";
 	else
 		s = literal ? " %s" : " <%s>";
-	return fprintf(outfile, s, opts->argh ? opts->argh : "...");
+	return fprintf(outfile, s, opts->argh ? gettext(opts->argh) : "...");

It is ok to use _() with an argument that is not a string literal, and in fact it is needed if the argument is to be poisoned.

Maybe it would be worth resending the patch to make the gettext() identifier not available in NO_GETTEXT builds, so this kind of thing is easier to catch.

Thanks and hope that helps,
Jonathan

#13 lore raw

PATCH 4/5 i18n: help: mark parseopt strings for translation

In reply to Nguyễn Thái Ngọc Duy

---
A demonstration of the previous patch.

 builtin/help.c |   10 +++++-----
 1 files changed, 5 insertions(+), 5 deletions(-)
builtin/help.c +5 -5
index 61ff798..22756bd 100644
@@ -32,17 +32,17 @@ enum help_format {
 static int show_all = 0;
 static enum help_format help_format = HELP_FORMAT_NONE;
 static struct option builtin_help_options[] = {
-	OPT_BOOLEAN('a', "all", &show_all, "print all available commands"),
-	OPT_SET_INT('m', "man", &help_format, "show man page", HELP_FORMAT_MAN),
-	OPT_SET_INT('w', "web", &help_format, "show manual in web browser",
+	OPT_BOOLEAN('a', "all", &show_all, N_("print all available commands")),
+	OPT_SET_INT('m', "man", &help_format, N_("show man page"), HELP_FORMAT_MAN),
+	OPT_SET_INT('w', "web", &help_format, N_("show manual in web browser"),
 			HELP_FORMAT_WEB),
-	OPT_SET_INT('i', "info", &help_format, "show info page",
+	OPT_SET_INT('i', "info", &help_format, N_("show info page"),
 			HELP_FORMAT_INFO),
 	OPT_END(),
 };
 
 static const char * const builtin_help_usage[] = {
-	"git help [--all] [--man|--web|--info] [command]",
+	N_("git help [--all] [--man|--web|--info] [command]"),
 	NULL
 };
 
Signature · 1 line
1.7.3.1.256.g2539c.dirty
#14 lore raw

PATCH 5/5 i18n: help: mark strings for translation

In reply to Nguyễn Thái Ngọc Duy

This patch also marks most common commands' synopsis for translation so that "git help" gives a friendly listing.

---
 Makefile            |    2 +-
 builtin/help.c      |   48 ++++++++++++++++++++++++------------------------
 generate-cmdlist.sh |    2 +-
 git.c               |    2 +-
 help.c              |   34 +++++++++++++++++++++-------------
 5 files changed, 48 insertions(+), 40 deletions(-)
Makefile +1 -1
index be1957a..34cf971 100644
@@ -2282,7 +2282,7 @@ XGETTEXT_FLAGS_C = $(XGETTEXT_FLAGS) --language=C \
 	--keyword=_ --keyword=N_ --keyword="Q_:1,2"
 XGETTEXT_FLAGS_SH = $(XGETTEXT_FLAGS) --language=Shell
 XGETTEXT_FLAGS_PERL = $(XGETTEXT_FLAGS) --keyword=__ --language=Perl
-LOCALIZED_C := $(C_OBJ:o=c)
+LOCALIZED_C := $(C_OBJ:o=c) common-cmds.h
 LOCALIZED_SH := $(SCRIPT_SH)
 LOCALIZED_PERL := $(SCRIPT_PERL)
 
builtin/help.c +24 -24

158 lines not on this page · raw message

generate-cmdlist.sh +1 -1
index 1093ef4..9a4c9b9 100755
@@ -16,7 +16,7 @@ do
      /^NAME/,/git-'"$cmd"'/H
      ${
 	    x
-	    s/.*git-'"$cmd"' - \(.*\)/  {"'"$cmd"'", "\1"},/
+	    s/.*git-'"$cmd"' - \(.*\)/  {"'"$cmd"'", N_("\1")},/
 	    p
      }' "Documentation/git-$cmd.txt"
 done
git.c +1 -1
index 3805616..4486deb 100644
@@ -13,7 +13,7 @@ const char git_usage_string[] =
 	"           <command> [<args>]";
 
 const char git_more_info_string[] =
-	"See 'git help <command>' for more information on a specific command.";
+	N_("See 'git help <command>' for more information on a specific command.");
 
 static struct startup_info git_startup_info;
 static int use_pager = -1;
help.c +21 -13
index 14eefc9..69927d8 100644
@@ -217,8 +217,9 @@ void list_commands(const char *title, struct cmdnames *main_cmds,
 
 	if (main_cmds->cnt) {
 		const char *exec_path = git_exec_path();
-		printf("available %s in '%s'\n", title, exec_path);
-		printf("----------------");
+		printf(_("available %s in '%s'\n"), title, exec_path);
+		/* TRANSLATIONS: this must align with "available %s in '%s'\n"*/
+		printf(_("----------------"));
 		mput_char('-', strlen(title) + strlen(exec_path));
 		putchar('\n');
 		pretty_print_string_list(main_cmds, longest);
@@ -226,8 +227,12 @@ void list_commands(const char *title, struct cmdnames *main_cmds,
 	}
 
 	if (other_cmds->cnt) {
-		printf("%s available from elsewhere on your $PATH\n", title);
-		printf("---------------------------------------");
+		printf(_("%s available from elsewhere on your $PATH\n"), title);
+		/* TRANSLATIONS:
+		 * this must align with
+		 * "%s available from elsewhere on your $PATH\n"
+		 */
+		printf(_("---------------------------------------"));
 		mput_char('-', strlen(title));
 		putchar('\n');
 		pretty_print_string_list(other_cmds, longest);
@@ -341,7 +346,7 @@ const char *help_unknown_cmd(const char *cmd)
 	      sizeof(*main_cmds.names), levenshtein_compare);
 
 	if (!main_cmds.cnt)
-		die ("Uh oh. Your system reports no Git commands at all.");
+		die(_("Uh oh. Your system reports no Git commands at all."));
 
 	/* skip and count prefix matches */
 	for (n = 0; n < main_cmds.cnt && !main_cmds.names[n]->len; n++)
@@ -362,23 +367,26 @@ const char *help_unknown_cmd(const char *cmd)
 		const char *assumed = main_cmds.names[0]->name;
 		main_cmds.names[0] = NULL;
 		clean_cmdnames(&main_cmds);
-		fprintf(stderr, "WARNING: You called a Git command named '%s', "
-			"which does not exist.\n"
-			"Continuing under the assumption that you meant '%s'\n",
+		fprintf(stderr,
+			_("WARNING: You called a Git command named '%s', "
+			  "which does not exist.\n"
+			  "Continuing under the assumption that you meant '%s'\n"),
 			cmd, assumed);
 		if (autocorrect > 0) {
-			fprintf(stderr, "in %0.1f seconds automatically...\n",
+			fprintf(stderr, _("in %0.1f seconds automatically...\n"),
 				(float)autocorrect/10.0);
 			poll(NULL, 0, autocorrect * 100);
 		}
 		return assumed;
 	}
 
-	fprintf(stderr, "git: '%s' is not a git command. See 'git --help'.\n", cmd);
+	fprintf(stderr, _("git: '%s' is not a git command. See 'git --help'.\n"), cmd);
 
 	if (SIMILAR_ENOUGH(best_similarity)) {
-		fprintf(stderr, "\nDid you mean %s?\n",
-			n < 2 ? "this": "one of these");
+		fprintf(stderr,
+			Q_("\nDid you mean this?\n",
+			   "\nDid you mean one of these?\n",
+			   n));
 
 		for (i = 0; i < n; i++)
 			fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
@@ -389,6 +397,6 @@ const char *help_unknown_cmd(const char *cmd)
 
 int cmd_version(int argc, const char **argv, const char *prefix)
 {
-	printf("git version %s\n", git_version_string);
+	printf(_("git version %s\n"), git_version_string);
 	return 0;
 }
Signature · 1 line
1.7.3.1.256.g2539c.dirty
#7 lore raw

Re: [PATCH 2/5] i18n: mark relative dates for translation

In reply to Nguyễn Thái Ngọc Duy

Hi,

Quoted 1 line · Nguyễn Thái Ngọc Duy

English dates get correct plural/singular form as a side effect.

[...]

Quoted 2 lines

+++ b/date.c

(quoted diff) +0 -0
@@ -93,38 +93,46 @@ const char *show_date_relative(unsigned long time, int tz,

[...]

Review

Quoted 5 lines
(quoted diff) +2 -1
 	if (diff < 90) {
-		snprintf(timebuf, timebuf_size, "%lu seconds ago", diff);
+		snprintf(timebuf, timebuf_size,
+			 Q_("%lu second ago", "%lu seconds ago", diff), diff);
 		return timebuf;

This leaves me vaguely nervous --- sure, no language is going to use an expression for "<n> years" that is more than 200 bytes long, but if one does, it would get truncated.

Would something like the following (untested) on top make sense?

---
My other worry is that

Review

Quoted 7 lines
(quoted diff) +7 -0
+	struct strbuf sb = STRBUF_INIT;
+	strbuf_addf(&sb, Q_("%lu year", "%lu years", years), years);
+	/* TRANSLATORS: "%s" is "<n> years" */
+	snprintf(timebuf, timebuf_size,
+	         Q_("%s, %lu month ago", "%s, %lu months ago", months),
+	         sb.buf, months);
+	strbuf_release(&sb);

seems excessively complicated. How do translations normally deal with cases like this of strings with multiple numbers in them?

 cache.h     |    5 ++--
 date.c      |   73 +++++++++++++++++++++++++++++++----------------------------
 test-date.c |    7 +++---
 3 files changed, 45 insertions(+), 40 deletions(-)
cache.h +2 -3
index e5e1aa4e..ca704bdd 100644
@@ -906,10 +906,9 @@ enum date_mode {
 };
 
 const char *show_date(unsigned long time, int timezone, enum date_mode mode);
-const char *show_date_relative(unsigned long time, int tz,
+void show_date_relative(unsigned long time, int tz,
 			       const struct timeval *now,
-			       char *timebuf,
-			       size_t timebuf_size);
+			       struct strbuf *timebuf);
 int parse_date(const char *date, char *buf, int bufsize);
 int parse_date_basic(const char *date, unsigned long *timestamp, int *offset);
 void datestamp(char *buf, int bufsize);
date.c +37 -32

161 lines not on this page · raw message

test-date.c +4 -3
index 6bcd5b03..10afaabb 100644
@@ -7,13 +7,14 @@ static const char *usage_msg = "\n"
 
 static void show_dates(char **argv, struct timeval *now)
 {
-	char buf[128];
+	struct strbuf buf = STRBUF_INIT;
 
 	for (; *argv; argv++) {
 		time_t t = atoi(*argv);
-		show_date_relative(t, 0, now, buf, sizeof(buf));
-		printf("%s -> %s\n", *argv, buf);
+		show_date_relative(t, 0, now, &buf);
+		printf("%s -> %s\n", *argv, buf.buf);
 	}
+	strbuf_release(&buf);
 }
 
 static void parse_dates(char **argv, struct timeval *now)
#8 lore raw

Re: [PATCH 2/5] i18n: mark relative dates for translation

Am 15.03.2012 19:51, schrieb Jonathan Nieder:

date.c const char *show_date_relative(unsigned long time, int tz, · line 148

Quoted 9 lines
date.c +1 -1
@@ -141,40 +142,42 @@ const char *show_date_relative(unsigned long time, int tz,
 		unsigned long years = totalmonths / 12;
 		unsigned long months = totalmonths % 12;
 		if (months) {
 			struct strbuf sb = STRBUF_INIT;
 			strbuf_addf(&sb, Q_("%lu year", "%lu years", years), years);
 			/* TRANSLATORS: "%s" is "<n> years" */
-			snprintf(timebuf, timebuf_size,
+			strbuf_addf(timebuf,

OT: You have 6 lines of context here, unlike all other hunks, which have only 3 lines. Something weird or simple to explain?

-- Hannes

#9 lore raw

Re: [PATCH 2/5] i18n: mark relative dates for translation

Quoted 14 lines · Johannes Sixt

Am 15.03.2012 19:51, schrieb Jonathan Nieder:

date.c const char *show_date_relative(unsigned long time, int tz, · line 148

Quoted 9 lines
date.c +1 -1
@@ -141,40 +142,42 @@ const char *show_date_relative(unsigned long time, int tz,
 		unsigned long years = totalmonths / 12;
 		unsigned long months = totalmonths % 12;
 		if (months) {
 			struct strbuf sb = STRBUF_INIT;
 			strbuf_addf(&sb, Q_("%lu year", "%lu years", years), years);
 			/* TRANSLATORS: "%s" is "<n> years" */
-			snprintf(timebuf, timebuf_size,
+			strbuf_addf(timebuf,

OT: You have 6 lines of context here, unlike all other hunks, which have only 3 lines. Something weird or simple to explain?

Just manual patch mangling. ;-)

#10 lore raw

Re: [PATCH 2/5] i18n: mark relative dates for translation

In reply to Jonathan Nieder
Quoted 15 lines · Jonathan Nieder

Hi,

Quoted 1 line · Nguyễn Thái Ngọc Duy

English dates get correct plural/singular form as a side effect.

[...]

Quoted 2 lines

+++ b/date.c

(quoted diff) +0 -0
@@ -93,38 +93,46 @@ const char *show_date_relative(unsigned long time, int tz,

[...]

Quoted 5 lines

      if (diff < 90) {
-             snprintf(timebuf, timebuf_size, "%lu seconds ago", diff);
+             snprintf(timebuf, timebuf_size,
+                      Q_("%lu second ago", "%lu seconds ago", diff), diff); return timebuf;

Quoted 5 lines

This leaves me vaguely nervous --- sure, no language is going to use an expression for "<n> years" that is more than 200 bytes long, but if one does, it would get truncated.

Would something like the following (untested) on top make sense?

It does. I will reuse your patch next time.

Quoted 10 lines

My other worry is that

Quoted 7 lines

+     struct strbuf sb = STRBUF_INIT;
+     strbuf_addf(&sb, Q_("%lu year", "%lu years", years), years);
+     /* TRANSLATORS: "%s" is "<n> years" */
+     snprintf(timebuf, timebuf_size,
+              Q_("%s, %lu month ago", "%s, %lu months ago", months),
+              sb.buf, months);
+     strbuf_release(&sb);

Quoted 2 lines

seems excessively complicated.  How do translations normally deal with cases like this of strings with multiple numbers in them?

I don't recall any similar cases. A search for 'ago"' in all gnome translations I have only shows "<one number> ago", or "%d blah ago, <absolute time>". The closet is probably strftime, where translators are free to reorder date and time items, something like this

/* Translators: the first %s is the number of months, the second the number of years */
sprintf(.., "%s, %s ago", month_string, year_string);

Or we can just round it up and show only one number.

Signature · 1 line
Duy