]>
git.ipfire.org Git - thirdparty/cups.git/blob - locale/checkpo.c
2 * "$Id: checkpo.c 7223 2008-01-16 23:41:19Z mike $"
4 * Verify that translations in the .po file have the same number and type of
5 * printf-style format strings.
7 * Copyright 2007-2009 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.
34 #include <cups/string.h>
35 #include <cups/i18n.h>
42 static char *abbreviate(const char *s
, char *buf
, int bufsize
);
43 static cups_array_t
*collect_formats(const char *id
);
44 static void free_formats(cups_array_t
*fmts
);
48 * 'main()' - Validate .po files.
51 int /* O - Exit code */
52 main(int argc
, /* I - Number of command-line args */
53 char *argv
[]) /* I - Command-line arguments */
55 int i
; /* Looping var */
56 cups_array_t
*po
; /* .po file */
57 _cups_message_t
*msg
; /* Current message */
58 cups_array_t
*idfmts
, /* Format strings in msgid */
59 *strfmts
; /* Format strings in msgstr */
60 char *idfmt
, /* Current msgid format string */
61 *strfmt
; /* Current msgstr format string */
62 int fmtidx
; /* Format index */
63 int status
, /* Exit status */
64 pass
, /* Pass/fail status */
65 untranslated
; /* Untranslated messages */
66 char idbuf
[80], /* Abbreviated msgid */
67 strbuf
[80]; /* Abbreviated msgstr */
72 puts("Usage: checkpo filename.po [... filenameN.po]");
77 * Check every .po file on the command-line...
80 for (i
= 1, status
= 0; i
< argc
; i
++)
83 * Use the CUPS .po loader to get the message strings...
86 if ((po
= _cupsMessageLoad(argv
[i
])) == NULL
)
92 printf("%s: ", argv
[i
]);
96 * Scan every message for a % string and then match them up with
97 * the corresponding string in the translation...
103 for (msg
= (_cups_message_t
*)cupsArrayFirst(po
);
105 msg
= (_cups_message_t
*)cupsArrayNext(po
))
107 if (!msg
->str
|| !msg
->str
[0])
112 else if (strchr(msg
->id
, '%'))
114 idfmts
= collect_formats(msg
->id
);
115 strfmts
= collect_formats(msg
->str
);
118 for (strfmt
= (char *)cupsArrayFirst(strfmts
);
120 strfmt
= (char *)cupsArrayNext(strfmts
))
122 if (isdigit(strfmt
[1] & 255) && strfmt
[2] == '$')
125 * Handle positioned format stuff...
128 fmtidx
= strfmt
[1] - '1';
130 if ((idfmt
= (char *)cupsArrayIndex(idfmts
, fmtidx
)) != NULL
)
136 * Compare against the current format...
139 idfmt
= (char *)cupsArrayIndex(idfmts
, fmtidx
);
144 if (!idfmt
|| strcmp(strfmt
, idfmt
))
148 if (cupsArrayCount(strfmts
) != cupsArrayCount(idfmts
) || strfmt
)
156 printf(" Bad translation string \"%s\"\n for \"%s\"\n",
157 abbreviate(msg
->str
, strbuf
, sizeof(strbuf
)),
158 abbreviate(msg
->id
, idbuf
, sizeof(idbuf
)));
159 fputs(" Translation formats:", stdout
);
160 for (strfmt
= (char *)cupsArrayFirst(strfmts
);
162 strfmt
= (char *)cupsArrayNext(strfmts
))
163 printf(" %s", strfmt
);
164 fputs("\n Original formats:", stdout
);
165 for (idfmt
= (char *)cupsArrayFirst(idfmts
);
167 idfmt
= (char *)cupsArrayNext(idfmts
))
168 printf(" %s", idfmt
);
173 free_formats(idfmts
);
174 free_formats(strfmts
);
177 if ((!strncmp(msg
->id
, "ALERT:", 6) && strncmp(msg
->str
, "ALERT:", 6)) ||
178 (!strncmp(msg
->id
, "CRIT:", 5) && strncmp(msg
->str
, "CRIT:", 5)) ||
179 (!strncmp(msg
->id
, "DEBUG:", 6) && strncmp(msg
->str
, "DEBUG:", 6)) ||
180 (!strncmp(msg
->id
, "DEBUG2:", 7) && strncmp(msg
->str
, "DEBUG2:", 7)) ||
181 (!strncmp(msg
->id
, "EMERG:", 6) && strncmp(msg
->str
, "EMERG:", 6)) ||
182 (!strncmp(msg
->id
, "ERROR:", 6) && strncmp(msg
->str
, "ERROR:", 6)) ||
183 (!strncmp(msg
->id
, "INFO:", 5) && strncmp(msg
->str
, "INFO:", 5)) ||
184 (!strncmp(msg
->id
, "NOTICE:", 7) && strncmp(msg
->str
, "NOTICE:", 7)) ||
185 (!strncmp(msg
->id
, "WARNING:", 8) && strncmp(msg
->str
, "WARNING:", 8)))
193 printf(" Bad prefix on filter message \"%s\"\n for \"%s\"\n\n",
194 abbreviate(msg
->str
, strbuf
, sizeof(strbuf
)),
195 abbreviate(msg
->id
, idbuf
, sizeof(idbuf
)));
201 if ((untranslated
* 10) >= cupsArrayCount(po
))
204 * Only allow 10% of messages to be untranslated before we fail...
209 printf(" Too many untranslated messages (%d of %d)\n\n",
210 untranslated
, cupsArrayCount(po
));
212 else if (untranslated
> 0)
213 printf("PASS (%d of %d untranslated)\n\n", untranslated
,
222 _cupsMessageFree(po
);
230 * 'abbreviate()' - Abbreviate a message string as needed.
233 static char * /* O - Abbreviated string */
234 abbreviate(const char *s
, /* I - String to abbreviate */
235 char *buf
, /* I - Buffer */
236 int bufsize
) /* I - Size of buffer */
238 char *bufptr
; /* Pointer into buffer */
241 for (bufptr
= buf
, bufsize
-= 4; *s
&& bufsize
> 0; s
++)
261 else if (*s
>= 0 && *s
< ' ')
266 sprintf(bufptr
, "\\%03o", *s
);
278 strcpy(bufptr
, "...");
287 * 'collect_formats()' - Collect all of the format strings in the msgid.
290 static cups_array_t
* /* O - Array of format strings */
291 collect_formats(const char *id
) /* I - msgid string */
293 cups_array_t
*fmts
; /* Array of format strings */
294 char buf
[255], /* Format string buffer */
295 *bufptr
; /* Pointer into format string */
298 fmts
= cupsArrayNew(NULL
, NULL
);
300 while ((id
= strchr(id
, '%')) != NULL
)
312 for (bufptr
= buf
; *id
&& bufptr
< (buf
+ sizeof(buf
) - 1); id
++)
316 if (strchr("CDEFGIOSUXcdeifgopsux", *id
))
324 cupsArrayAdd(fmts
, strdup(buf
));
332 * 'free_formats()' - Free all of the format strings.
336 free_formats(cups_array_t
*fmts
) /* I - Array of format strings */
338 char *s
; /* Current string */
341 for (s
= (char *)cupsArrayFirst(fmts
); s
; s
= (char *)cupsArrayNext(fmts
))
344 cupsArrayDelete(fmts
);
349 * End of "$Id: checkpo.c 7223 2008-01-16 23:41:19Z mike $".