]>
git.ipfire.org Git - thirdparty/cups.git/blob - locale/checkpo.c
2 * "$Id: checkpo.c 6922 2007-09-06 14:18:02Z mike $"
4 * Verify that translations in the .po file have the same number and type of
5 * printf-style format strings.
9 * checkpo filename.po [... filenameN.po]
13 * gcc -o checkpo checkpo.c `cups-config --libs`
17 * main() - Validate .po files.
18 * abbreviate() - Abbreviate a message string as needed.
19 * collect_formats() - Collect all of the format strings in the msgid.
20 * free_formats() - Free all of the format strings.
25 #include <cups/string.h>
26 #include <cups/i18n.h>
33 static char *abbreviate(const char *s
, char *buf
, int bufsize
);
34 static cups_array_t
*collect_formats(const char *id
);
35 static void free_formats(cups_array_t
*fmts
);
39 * 'main()' - Validate .po files.
42 int /* O - Exit code */
43 main(int argc
, /* I - Number of command-line args */
44 char *argv
[]) /* I - Command-line arguments */
46 int i
; /* Looping var */
47 cups_array_t
*po
; /* .po file */
48 _cups_message_t
*msg
; /* Current message */
49 cups_array_t
*idfmts
, /* Format strings in msgid */
50 *strfmts
; /* Format strings in msgstr */
51 char *idfmt
, /* Current msgid format string */
52 *strfmt
; /* Current msgstr format string */
53 int fmtidx
; /* Format index */
54 int status
, /* Exit status */
55 pass
, /* Pass/fail status */
56 untranslated
; /* Untranslated messages */
57 char idbuf
[80], /* Abbreviated msgid */
58 strbuf
[80]; /* Abbreviated msgstr */
63 puts("Usage: checkpo filename.po [... filenameN.po]");
68 * Check every .po file on the command-line...
71 for (i
= 1, status
= 0; i
< argc
; i
++)
74 * Use the CUPS .po loader to get the message strings...
77 if ((po
= _cupsMessageLoad(argv
[i
])) == NULL
)
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
))
98 if (!msg
->str
|| !msg
->str
[0])
103 else if (strchr(msg
->id
, '%'))
105 idfmts
= collect_formats(msg
->id
);
106 strfmts
= collect_formats(msg
->str
);
109 for (strfmt
= (char *)cupsArrayFirst(strfmts
);
111 strfmt
= (char *)cupsArrayNext(strfmts
))
113 if (isdigit(strfmt
[1] & 255) && strfmt
[2] == '$')
116 * Handle positioned format stuff...
119 fmtidx
= strfmt
[1] - '1';
121 if ((idfmt
= (char *)cupsArrayIndex(idfmts
, fmtidx
)) != NULL
)
127 * Compare against the current format...
130 idfmt
= (char *)cupsArrayIndex(idfmts
, fmtidx
);
135 if (!idfmt
|| strcmp(strfmt
, idfmt
))
139 if (cupsArrayCount(strfmts
) != cupsArrayCount(idfmts
) || strfmt
)
147 printf(" Bad translation string \"%s\"\n for \"%s\"\n",
148 abbreviate(msg
->str
, strbuf
, sizeof(strbuf
)),
149 abbreviate(msg
->id
, idbuf
, sizeof(idbuf
)));
150 fputs(" Translation formats:", stdout
);
151 for (strfmt
= (char *)cupsArrayFirst(strfmts
);
153 strfmt
= (char *)cupsArrayNext(strfmts
))
154 printf(" %s", strfmt
);
155 fputs("\n Original formats:", stdout
);
156 for (idfmt
= (char *)cupsArrayFirst(idfmts
);
158 idfmt
= (char *)cupsArrayNext(idfmts
))
159 printf(" %s", idfmt
);
163 free_formats(idfmts
);
164 free_formats(strfmts
);
167 if ((!strncmp(msg
->id
, "ALERT:", 6) && strncmp(msg
->str
, "ALERT:", 6)) ||
168 (!strncmp(msg
->id
, "CRIT:", 5) && strncmp(msg
->str
, "CRIT:", 5)) ||
169 (!strncmp(msg
->id
, "DEBUG:", 6) && strncmp(msg
->str
, "DEBUG:", 6)) ||
170 (!strncmp(msg
->id
, "DEBUG2:", 7) && strncmp(msg
->str
, "DEBUG2:", 7)) ||
171 (!strncmp(msg
->id
, "EMERG:", 6) && strncmp(msg
->str
, "EMERG:", 6)) ||
172 (!strncmp(msg
->id
, "ERROR:", 6) && strncmp(msg
->str
, "ERROR:", 6)) ||
173 (!strncmp(msg
->id
, "INFO:", 5) && strncmp(msg
->str
, "INFO:", 5)) ||
174 (!strncmp(msg
->id
, "NOTICE:", 7) && strncmp(msg
->str
, "NOTICE:", 7)) ||
175 (!strncmp(msg
->id
, "WARNING:", 8) && strncmp(msg
->str
, "WARNING:", 8)))
183 printf(" Bad prefix on filter message \"%s\"\n for \"%s\"\n",
184 abbreviate(msg
->str
, strbuf
, sizeof(strbuf
)),
185 abbreviate(msg
->id
, idbuf
, sizeof(idbuf
)));
191 if ((untranslated
* 10) >= cupsArrayCount(po
))
194 * Only allow 10% of messages to be untranslated before we fail...
199 printf(" Too many untranslated messages (%d of %d)\n", untranslated
,
202 else if (untranslated
> 0)
203 printf("PASS (%d of %d untranslated)\n", untranslated
,
212 _cupsMessageFree(po
);
220 * 'abbreviate()' - Abbreviate a message string as needed.
223 static char * /* O - Abbreviated string */
224 abbreviate(const char *s
, /* I - String to abbreviate */
225 char *buf
, /* I - Buffer */
226 int bufsize
) /* I - Size of buffer */
228 char *bufptr
; /* Pointer into buffer */
231 for (bufptr
= buf
, bufsize
-= 4; *s
&& bufsize
> 0; s
++)
251 else if (*s
>= 0 && *s
< ' ')
256 sprintf(bufptr
, "\\%03o", *s
);
268 strcpy(bufptr
, "...");
277 * 'collect_formats()' - Collect all of the format strings in the msgid.
280 static cups_array_t
* /* O - Array of format strings */
281 collect_formats(const char *id
) /* I - msgid string */
283 cups_array_t
*fmts
; /* Array of format strings */
284 char buf
[255], /* Format string buffer */
285 *bufptr
; /* Pointer into format string */
288 fmts
= cupsArrayNew(NULL
, NULL
);
290 while ((id
= strchr(id
, '%')) != NULL
)
302 for (bufptr
= buf
; *id
&& bufptr
< (buf
+ sizeof(buf
) - 1); id
++)
306 if (strchr("CDEFGIOSUXcdeifgopsux", *id
))
314 cupsArrayAdd(fmts
, strdup(buf
));
322 * 'free_formats()' - Free all of the format strings.
326 free_formats(cups_array_t
*fmts
) /* I - Array of format strings */
328 char *s
; /* Current string */
331 for (s
= (char *)cupsArrayFirst(fmts
); s
; s
= (char *)cupsArrayNext(fmts
))
334 cupsArrayDelete(fmts
);
339 * End of "$Id: checkpo.c 6922 2007-09-06 14:18:02Z mike $".