]>
git.ipfire.org Git - thirdparty/cups.git/blob - locale/checkpo.c
2 * Verify that translations in the .po file have the same number and type of
3 * printf-style format strings.
5 * Copyright 2007-2017 by Apple Inc.
6 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
8 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
12 * checkpo filename.{po,strings} [... filenameN.{po,strings}]
16 * gcc -o checkpo checkpo.c `cups-config --libs`
19 #include <cups/cups-private.h>
26 static char *abbreviate(const char *s
, char *buf
, int bufsize
);
27 static cups_array_t
*collect_formats(const char *id
);
28 static void free_formats(cups_array_t
*fmts
);
32 * 'main()' - Validate .po and .strings files.
35 int /* O - Exit code */
36 main(int argc
, /* I - Number of command-line args */
37 char *argv
[]) /* I - Command-line arguments */
39 int i
; /* Looping var */
40 cups_array_t
*po
; /* .po file */
41 _cups_message_t
*msg
; /* Current message */
42 cups_array_t
*idfmts
, /* Format strings in msgid */
43 *strfmts
; /* Format strings in msgstr */
44 char *idfmt
, /* Current msgid format string */
45 *strfmt
; /* Current msgstr format string */
46 int fmtidx
; /* Format index */
47 int status
, /* Exit status */
48 pass
, /* Pass/fail status */
49 untranslated
; /* Untranslated messages */
50 char idbuf
[80], /* Abbreviated msgid */
51 strbuf
[80]; /* Abbreviated msgstr */
56 puts("Usage: checkpo filename.{po,strings} [... filenameN.{po,strings}]");
61 * Check every .po or .strings file on the command-line...
64 for (i
= 1, status
= 0; i
< argc
; i
++)
67 * Use the CUPS .po loader to get the message strings...
70 if (strstr(argv
[i
], ".strings"))
71 po
= _cupsMessageLoad(argv
[i
], _CUPS_MESSAGE_STRINGS
);
73 po
= _cupsMessageLoad(argv
[i
], _CUPS_MESSAGE_PO
| _CUPS_MESSAGE_EMPTY
);
83 printf("%s: ", argv
[i
]);
87 * Scan every message for a % string and then match them up with
88 * the corresponding string in the translation...
94 for (msg
= (_cups_message_t
*)cupsArrayFirst(po
);
96 msg
= (_cups_message_t
*)cupsArrayNext(po
))
99 * Make sure filter message prefixes are not translated...
102 if (!strncmp(msg
->msg
, "ALERT:", 6) || !strncmp(msg
->msg
, "CRIT:", 5) ||
103 !strncmp(msg
->msg
, "DEBUG:", 6) || !strncmp(msg
->msg
, "DEBUG2:", 7) ||
104 !strncmp(msg
->msg
, "EMERG:", 6) || !strncmp(msg
->msg
, "ERROR:", 6) ||
105 !strncmp(msg
->msg
, "INFO:", 5) || !strncmp(msg
->msg
, "NOTICE:", 7) ||
106 !strncmp(msg
->msg
, "WARNING:", 8))
114 printf(" Bad prefix on filter message \"%s\"\n",
115 abbreviate(msg
->msg
, idbuf
, sizeof(idbuf
)));
118 idfmt
= msg
->msg
+ strlen(msg
->msg
) - 1;
119 if (idfmt
>= msg
->msg
&& *idfmt
== '\n')
127 printf(" Trailing newline in message \"%s\"\n",
128 abbreviate(msg
->msg
, idbuf
, sizeof(idbuf
)));
131 for (; idfmt
>= msg
->msg
; idfmt
--)
132 if (!isspace(*idfmt
& 255))
135 if (idfmt
>= msg
->msg
&& *idfmt
== '!')
143 printf(" Exclamation in message \"%s\"\n",
144 abbreviate(msg
->msg
, idbuf
, sizeof(idbuf
)));
147 if ((idfmt
- 2) >= msg
->msg
&& !strncmp(idfmt
- 2, "...", 3))
155 printf(" Ellipsis in message \"%s\"\n",
156 abbreviate(msg
->msg
, idbuf
, sizeof(idbuf
)));
159 if (!msg
->str
|| !msg
->str
[0])
164 else if (strchr(msg
->msg
, '%'))
166 idfmts
= collect_formats(msg
->msg
);
167 strfmts
= collect_formats(msg
->str
);
170 for (strfmt
= (char *)cupsArrayFirst(strfmts
);
172 strfmt
= (char *)cupsArrayNext(strfmts
))
174 if (isdigit(strfmt
[1] & 255) && strfmt
[2] == '$')
177 * Handle positioned format stuff...
180 fmtidx
= strfmt
[1] - '1';
182 if ((idfmt
= (char *)cupsArrayIndex(idfmts
, fmtidx
)) != NULL
)
188 * Compare against the current format...
191 idfmt
= (char *)cupsArrayIndex(idfmts
, fmtidx
);
196 if (!idfmt
|| strcmp(strfmt
, idfmt
))
200 if (cupsArrayCount(strfmts
) != cupsArrayCount(idfmts
) || strfmt
)
208 printf(" Bad translation string \"%s\"\n for \"%s\"\n",
209 abbreviate(msg
->str
, strbuf
, sizeof(strbuf
)),
210 abbreviate(msg
->msg
, idbuf
, sizeof(idbuf
)));
211 fputs(" Translation formats:", stdout
);
212 for (strfmt
= (char *)cupsArrayFirst(strfmts
);
214 strfmt
= (char *)cupsArrayNext(strfmts
))
215 printf(" %s", strfmt
);
216 fputs("\n Original formats:", stdout
);
217 for (idfmt
= (char *)cupsArrayFirst(idfmts
);
219 idfmt
= (char *)cupsArrayNext(idfmts
))
220 printf(" %s", idfmt
);
225 free_formats(idfmts
);
226 free_formats(strfmts
);
230 * Only allow \\, \n, \r, \t, \", and \### character escapes...
233 for (strfmt
= msg
->str
; *strfmt
; strfmt
++)
239 if (*strfmt
!= '\\' && *strfmt
!= 'n' && *strfmt
!= 'r' && *strfmt
!= 't' && *strfmt
!= '\"' && !isdigit(*strfmt
& 255))
247 printf(" Bad escape \\%c in filter message \"%s\"\n"
248 " for \"%s\"\n", strfmt
[1],
249 abbreviate(msg
->str
, strbuf
, sizeof(strbuf
)),
250 abbreviate(msg
->msg
, idbuf
, sizeof(idbuf
)));
259 int count
= cupsArrayCount(po
); /* Total number of messages */
261 if (untranslated
>= (count
/ 10) && strcmp(argv
[i
], "cups.pot"))
264 * Only allow 10% of messages to be untranslated before we fail...
269 printf(" Too many untranslated messages (%d of %d or %.1f%% are translated)\n", count
- untranslated
, count
, 100.0 - 100.0 * untranslated
/ count
);
271 else if (untranslated
> 0)
272 printf("PASS (%d of %d or %.1f%% are translated)\n", count
- untranslated
, count
, 100.0 - 100.0 * untranslated
/ count
);
280 _cupsMessageFree(po
);
288 * 'abbreviate()' - Abbreviate a message string as needed.
291 static char * /* O - Abbreviated string */
292 abbreviate(const char *s
, /* I - String to abbreviate */
293 char *buf
, /* I - Buffer */
294 int bufsize
) /* I - Size of buffer */
296 char *bufptr
; /* Pointer into buffer */
299 for (bufptr
= buf
, bufsize
-= 4; *s
&& bufsize
> 0; s
++)
319 else if (*s
>= 0 && *s
< ' ')
324 sprintf(bufptr
, "\\%03o", *s
);
336 memcpy(bufptr
, "...", 4);
345 * 'collect_formats()' - Collect all of the format strings in the msgid.
348 static cups_array_t
* /* O - Array of format strings */
349 collect_formats(const char *id
) /* I - msgid string */
351 cups_array_t
*fmts
; /* Array of format strings */
352 char buf
[255], /* Format string buffer */
353 *bufptr
; /* Pointer into format string */
356 fmts
= cupsArrayNew(NULL
, NULL
);
358 while ((id
= strchr(id
, '%')) != NULL
)
370 for (bufptr
= buf
; *id
&& bufptr
< (buf
+ sizeof(buf
) - 1); id
++)
374 if (strchr("CDEFGIOSUXcdeifgopsux", *id
))
382 cupsArrayAdd(fmts
, strdup(buf
));
390 * 'free_formats()' - Free all of the format strings.
394 free_formats(cups_array_t
*fmts
) /* I - Array of format strings */
396 char *s
; /* Current string */
399 for (s
= (char *)cupsArrayFirst(fmts
); s
; s
= (char *)cupsArrayNext(fmts
))
402 cupsArrayDelete(fmts
);