]>
git.ipfire.org Git - thirdparty/cups.git/blob - locale/checkpo.c
638e1bb27fbabc0ca5ea25a432539f60f3a558dd
4 * Verify that translations in the .po file have the same number and type of
5 * printf-style format strings.
7 * Copyright 2007-2010 by Apple Inc.
8 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
10 * These coded instructions, statements, and computer programs are the
11 * property of Apple Inc. and are protected by Federal copyright
12 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
13 * which should have been included with this file. If this file is
14 * file is missing or damaged, see the license at "http://www.cups.org/".
18 * checkpo filename.po [... filenameN.po]
22 * gcc -o checkpo checkpo.c `cups-config --libs`
26 * main() - Validate .po files.
27 * abbreviate() - Abbreviate a message string as needed.
28 * collect_formats() - Collect all of the format strings in the msgid.
29 * free_formats() - Free all of the format strings.
32 #include <cups/cups-private.h>
39 static char *abbreviate(const char *s
, char *buf
, int bufsize
);
40 static cups_array_t
*collect_formats(const char *id
);
41 static void free_formats(cups_array_t
*fmts
);
45 * 'main()' - Validate .po files.
48 int /* O - Exit code */
49 main(int argc
, /* I - Number of command-line args */
50 char *argv
[]) /* I - Command-line arguments */
52 int i
; /* Looping var */
53 cups_array_t
*po
; /* .po file */
54 _cups_message_t
*msg
; /* Current message */
55 cups_array_t
*idfmts
, /* Format strings in msgid */
56 *strfmts
; /* Format strings in msgstr */
57 char *idfmt
, /* Current msgid format string */
58 *strfmt
; /* Current msgstr format string */
59 int fmtidx
; /* Format index */
60 int status
, /* Exit status */
61 pass
, /* Pass/fail status */
62 untranslated
; /* Untranslated messages */
63 char idbuf
[80], /* Abbreviated msgid */
64 strbuf
[80]; /* Abbreviated msgstr */
69 puts("Usage: checkpo filename.po [... filenameN.po]");
74 * Check every .po file on the command-line...
77 for (i
= 1, status
= 0; i
< argc
; i
++)
80 * Use the CUPS .po loader to get the message strings...
83 if ((po
= _cupsMessageLoad(argv
[i
], 0)) == NULL
)
89 printf("%s: ", argv
[i
]);
93 * Scan every message for a % string and then match them up with
94 * the corresponding string in the translation...
100 for (msg
= (_cups_message_t
*)cupsArrayFirst(po
);
102 msg
= (_cups_message_t
*)cupsArrayNext(po
))
104 if (!msg
->str
|| !msg
->str
[0])
109 else if (strchr(msg
->id
, '%'))
111 idfmts
= collect_formats(msg
->id
);
112 strfmts
= collect_formats(msg
->str
);
115 for (strfmt
= (char *)cupsArrayFirst(strfmts
);
117 strfmt
= (char *)cupsArrayNext(strfmts
))
119 if (isdigit(strfmt
[1] & 255) && strfmt
[2] == '$')
122 * Handle positioned format stuff...
125 fmtidx
= strfmt
[1] - '1';
127 if ((idfmt
= (char *)cupsArrayIndex(idfmts
, fmtidx
)) != NULL
)
133 * Compare against the current format...
136 idfmt
= (char *)cupsArrayIndex(idfmts
, fmtidx
);
141 if (!idfmt
|| strcmp(strfmt
, idfmt
))
145 if (cupsArrayCount(strfmts
) != cupsArrayCount(idfmts
) || strfmt
)
153 printf(" Bad translation string \"%s\"\n for \"%s\"\n",
154 abbreviate(msg
->str
, strbuf
, sizeof(strbuf
)),
155 abbreviate(msg
->id
, idbuf
, sizeof(idbuf
)));
156 fputs(" Translation formats:", stdout
);
157 for (strfmt
= (char *)cupsArrayFirst(strfmts
);
159 strfmt
= (char *)cupsArrayNext(strfmts
))
160 printf(" %s", strfmt
);
161 fputs("\n Original formats:", stdout
);
162 for (idfmt
= (char *)cupsArrayFirst(idfmts
);
164 idfmt
= (char *)cupsArrayNext(idfmts
))
165 printf(" %s", idfmt
);
170 free_formats(idfmts
);
171 free_formats(strfmts
);
175 * Only allow \\, \n, \r, \t, \", and \### character escapes...
178 for (strfmt
= msg
->str
; *strfmt
; strfmt
++)
179 if (*strfmt
== '\\' &&
180 strfmt
[1] != '\\' && strfmt
[1] != 'n' && strfmt
[1] != 'r' &&
181 strfmt
[1] != 't' && strfmt
[1] != '\"' && !isdigit(strfmt
[1] & 255))
189 printf(" Bad escape \\%c in filter message \"%s\"\n"
190 " for \"%s\"\n\n", strfmt
[1],
191 abbreviate(msg
->str
, strbuf
, sizeof(strbuf
)),
192 abbreviate(msg
->id
, idbuf
, sizeof(idbuf
)));
197 * Make sure filter message prefixes are not translated...
200 if (!strncmp(msg
->id
, "ALERT:", 6) || !strncmp(msg
->id
, "CRIT:", 5) ||
201 !strncmp(msg
->id
, "DEBUG:", 6) || !strncmp(msg
->id
, "DEBUG2:", 7) ||
202 !strncmp(msg
->id
, "EMERG:", 6) || !strncmp(msg
->id
, "ERROR:", 6) ||
203 !strncmp(msg
->id
, "INFO:", 5) || !strncmp(msg
->id
, "NOTICE:", 7) ||
204 !strncmp(msg
->id
, "WARNING:", 8))
212 printf(" Bad prefix on filter message \"%s\"\n\n",
213 abbreviate(msg
->id
, idbuf
, sizeof(idbuf
)));
219 if ((untranslated
* 10) >= cupsArrayCount(po
))
222 * Only allow 10% of messages to be untranslated before we fail...
227 printf(" Too many untranslated messages (%d of %d)\n\n",
228 untranslated
, cupsArrayCount(po
));
230 else if (untranslated
> 0)
231 printf("PASS (%d of %d untranslated)\n\n", untranslated
,
240 _cupsMessageFree(po
);
248 * 'abbreviate()' - Abbreviate a message string as needed.
251 static char * /* O - Abbreviated string */
252 abbreviate(const char *s
, /* I - String to abbreviate */
253 char *buf
, /* I - Buffer */
254 int bufsize
) /* I - Size of buffer */
256 char *bufptr
; /* Pointer into buffer */
259 for (bufptr
= buf
, bufsize
-= 4; *s
&& bufsize
> 0; s
++)
279 else if (*s
>= 0 && *s
< ' ')
284 sprintf(bufptr
, "\\%03o", *s
);
296 strcpy(bufptr
, "...");
305 * 'collect_formats()' - Collect all of the format strings in the msgid.
308 static cups_array_t
* /* O - Array of format strings */
309 collect_formats(const char *id
) /* I - msgid string */
311 cups_array_t
*fmts
; /* Array of format strings */
312 char buf
[255], /* Format string buffer */
313 *bufptr
; /* Pointer into format string */
316 fmts
= cupsArrayNew(NULL
, NULL
);
318 while ((id
= strchr(id
, '%')) != NULL
)
330 for (bufptr
= buf
; *id
&& bufptr
< (buf
+ sizeof(buf
) - 1); id
++)
334 if (strchr("CDEFGIOSUXcdeifgopsux", *id
))
342 cupsArrayAdd(fmts
, strdup(buf
));
350 * 'free_formats()' - Free all of the format strings.
354 free_formats(cups_array_t
*fmts
) /* I - Array of format strings */
356 char *s
; /* Current string */
359 for (s
= (char *)cupsArrayFirst(fmts
); s
; s
= (char *)cupsArrayNext(fmts
))
362 cupsArrayDelete(fmts
);