i18n: mark relative dates for translation
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jonathan Nieder committed
Apr 23, 2012 at 19:30 UTC
7d29afd43cac38a332ef9fc176d11ec38d2569d4
3 files changed
+62
-46
cache.h
+2
-4
@@ -920,10 +920,8 @@ enum date_mode {
920
};
921
922
const char *show_date(unsigned long time, int timezone, enum date_mode mode);
923
-const char *show_date_relative(unsigned long time, int tz,
924
- const struct timeval *now,
925
- char *timebuf,
926
- size_t timebuf_size);
923
+void show_date_relative(unsigned long time, int tz, const struct timeval *now,
924
+ struct strbuf *timebuf);
925
int parse_date(const char *date, char *buf, int bufsize);
926
int parse_date_basic(const char *date, unsigned long *timestamp, int *offset);
927
void datestamp(char *buf, int bufsize);
date.c
+56
-39
@@ -86,83 +86,98 @@ static int local_tzoffset(unsigned long time)
86
return offset * eastwest;
87
}
88
89
-const char *show_date_relative(unsigned long time, int tz,
89
+void show_date_relative(unsigned long time, int tz,
90
const struct timeval *now,
91
- char *timebuf,
92
- size_t timebuf_size)
91
+ struct strbuf *timebuf)
92
{
93
unsigned long diff;
95
- if (now->tv_sec < time)
96
- return "in the future";
94
+ if (now->tv_sec < time) {
95
+ strbuf_addstr(timebuf, _("in the future"));
96
+ return;
97
+ }
98
diff = now->tv_sec - time;
99
if (diff < 90) {
99
- snprintf(timebuf, timebuf_size, "%lu seconds ago", diff);
100
- return timebuf;
100
+ strbuf_addf(timebuf,
101
+ Q_("%lu second ago", "%lu seconds ago", diff), diff);
102
+ return;
103
}
104
/* Turn it into minutes */
105
diff = (diff + 30) / 60;
106
if (diff < 90) {
105
- snprintf(timebuf, timebuf_size, "%lu minutes ago", diff);
106
- return timebuf;
107
+ strbuf_addf(timebuf,
108
+ Q_("%lu minute ago", "%lu minutes ago", diff), diff);
109
+ return;
110
}
111
/* Turn it into hours */
112
diff = (diff + 30) / 60;
113
if (diff < 36) {
111
- snprintf(timebuf, timebuf_size, "%lu hours ago", diff);
112
- return timebuf;
114
+ strbuf_addf(timebuf,
115
+ Q_("%lu hour ago", "%lu hours ago", diff), diff);
116
+ return;
117
}
118
/* We deal with number of days from here on */
119
diff = (diff + 12) / 24;
120
if (diff < 14) {
117
- snprintf(timebuf, timebuf_size, "%lu days ago", diff);
118
- return timebuf;
121
+ strbuf_addf(timebuf,
122
+ Q_("%lu day ago", "%lu days ago", diff), diff);
123
+ return;
124
}
125
/* Say weeks for the past 10 weeks or so */
126
if (diff < 70) {
122
- snprintf(timebuf, timebuf_size, "%lu weeks ago", (diff + 3) / 7);
123
- return timebuf;
127
+ strbuf_addf(timebuf,
128
+ Q_("%lu week ago", "%lu weeks ago", (diff + 3) / 7),
129
+ (diff + 3) / 7);
130
+ return;
131
}
132
/* Say months for the past 12 months or so */
133
if (diff < 365) {
127
- snprintf(timebuf, timebuf_size, "%lu months ago", (diff + 15) / 30);
128
- return timebuf;
134
+ strbuf_addf(timebuf,
135
+ Q_("%lu month ago", "%lu months ago", (diff + 15) / 30),
136
+ (diff + 15) / 30);
137
+ return;
138
}
139
/* Give years and months for 5 years or so */
140
if (diff < 1825) {
141
unsigned long totalmonths = (diff * 12 * 2 + 365) / (365 * 2);
142
unsigned long years = totalmonths / 12;
143
unsigned long months = totalmonths % 12;
135
- int n;
136
- n = snprintf(timebuf, timebuf_size, "%lu year%s",
137
- years, (years > 1 ? "s" : ""));
138
- if (months)
139
- snprintf(timebuf + n, timebuf_size - n,
140
- ", %lu month%s ago",
141
- months, (months > 1 ? "s" : ""));
142
- else
143
- snprintf(timebuf + n, timebuf_size - n, " ago");
144
- return timebuf;
144
+ if (months) {
145
+ struct strbuf sb = STRBUF_INIT;
146
+ strbuf_addf(&sb, Q_("%lu year", "%lu years", years), years);
147
+ /* TRANSLATORS: "%s" is "<n> years" */
148
+ strbuf_addf(timebuf,
149
+ Q_("%s, %lu month ago", "%s, %lu months ago", months),
150
+ sb.buf, months);
151
+ strbuf_release(&sb);
152
+ } else
153
+ strbuf_addf(timebuf,
154
+ Q_("%lu year ago", "%lu years ago", years), years);
155
+ return;
156
}
157
/* Otherwise, just years. Centuries is probably overkill. */
147
- snprintf(timebuf, timebuf_size, "%lu years ago", (diff + 183) / 365);
148
- return timebuf;
158
+ strbuf_addf(timebuf,
159
+ Q_("%lu year ago", "%lu years ago", (diff + 183) / 365),
160
+ (diff + 183) / 365);
161
}
162
163
const char *show_date(unsigned long time, int tz, enum date_mode mode)
164
{
165
struct tm *tm;
154
- static char timebuf[200];
166
+ static struct strbuf timebuf = STRBUF_INIT;
167
168
if (mode == DATE_RAW) {
157
- snprintf(timebuf, sizeof(timebuf), "%lu %+05d", time, tz);
158
- return timebuf;
169
+ strbuf_reset(&timebuf);
170
+ strbuf_addf(&timebuf, "%lu %+05d", time, tz);
171
+ return timebuf.buf;
172
}
173
174
if (mode == DATE_RELATIVE) {
175
struct timeval now;
176
+
177
+ strbuf_reset(&timebuf);
178
gettimeofday(&now, NULL);
164
- return show_date_relative(time, tz, &now,
165
- timebuf, sizeof(timebuf));
179
+ show_date_relative(time, tz, &now, &timebuf);
180
+ return timebuf.buf;
181
}
182
183
if (mode == DATE_LOCAL)
@@ -171,23 +186,25 @@ const char *show_date(unsigned long time, int tz, enum date_mode mode)
186
tm = time_to_tm(time, tz);
187
if (!tm)
188
return NULL;
189
+
190
+ strbuf_reset(&timebuf);
191
if (mode == DATE_SHORT)
175
- sprintf(timebuf, "%04d-%02d-%02d", tm->tm_year + 1900,
192
+ strbuf_addf(&timebuf, "%04d-%02d-%02d", tm->tm_year + 1900,
193
tm->tm_mon + 1, tm->tm_mday);
194
else if (mode == DATE_ISO8601)
178
- sprintf(timebuf, "%04d-%02d-%02d %02d:%02d:%02d %+05d",
195
+ strbuf_addf(&timebuf, "%04d-%02d-%02d %02d:%02d:%02d %+05d",
196
tm->tm_year + 1900,
197
tm->tm_mon + 1,
198
tm->tm_mday,
199
tm->tm_hour, tm->tm_min, tm->tm_sec,
200
tz);
201
else if (mode == DATE_RFC2822)
185
- sprintf(timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
202
+ strbuf_addf(&timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
203
weekday_names[tm->tm_wday], tm->tm_mday,
204
month_names[tm->tm_mon], tm->tm_year + 1900,
205
tm->tm_hour, tm->tm_min, tm->tm_sec, tz);
206
else
190
- sprintf(timebuf, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d",
207
+ strbuf_addf(&timebuf, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d",
208
weekday_names[tm->tm_wday],
209
month_names[tm->tm_mon],
210
tm->tm_mday,
@@ -195,7 +212,7 @@ const char *show_date(unsigned long time, int tz, enum date_mode mode)
212
tm->tm_year + 1900,
213
(mode == DATE_LOCAL) ? 0 : ' ',
214
tz);
198
- return timebuf;
215
+ return timebuf.buf;
216
}
217
218
/*
test-date.c
+4
-3
@@ -7,13 +7,14 @@ static const char *usage_msg = "\n"
7
8
static void show_dates(char **argv, struct timeval *now)
9
{
10
- char buf[128];
10
+ struct strbuf buf = STRBUF_INIT;
11
12
for (; *argv; argv++) {
13
time_t t = atoi(*argv);
14
- show_date_relative(t, 0, now, buf, sizeof(buf));
15
- printf("%s -> %s\n", *argv, buf);
14
+ show_date_relative(t, 0, now, &buf);
15
+ printf("%s -> %s\n", *argv, buf.buf);
16
}
17
+ strbuf_release(&buf);
18
}
19
20
static void parse_dates(char **argv, struct timeval *now)