]> git.ipfire.org Git - thirdparty/cups.git/blame - locale/po2strings.c
Merge changes from CUPS 1.4svn-r8305.
[thirdparty/cups.git] / locale / po2strings.c
CommitLineData
bc44d920 1/*
2e4ff8af 2 * "$Id: po2strings.c 6921 2007-09-06 13:38:37Z mike $"
bc44d920 3 *
4 * Convert GNU gettext .po files to Apple .strings file (UTF-16 BE text file).
5 *
6 * Usage:
7 *
8 * po2strings filename.strings filename.po
9 *
10 * Compile with:
11 *
12 * gcc -o po2strings po2strings.c `cups-config --libs`
13 *
14 * Contents:
15 *
16 * main() - Convert .po file to .strings.
17 * write_string() - Write a string to the .strings file.
18 */
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <cups/i18n.h>
23
24
25/*
26 * The .strings file format is simple:
27 *
28 * // comment
29 * "id" = "str";
30 *
31 * Both the id and str strings use standard C quoting for special characters
32 * like newline and the double quote character.
33 */
34
35/*
36 * Local functions...
37 */
38
39static void write_string(FILE *strings, const char *s);
40
41
42/*
43 * main() - Convert .po file to .strings.
44 */
45
46int /* O - Exit code */
47main(int argc, /* I - Number of command-line args */
48 char *argv[]) /* I - Command-line arguments */
49{
50 FILE *strings; /* .strings file */
51 cups_array_t *po; /* .po file */
52 char iconv[1024]; /* iconv command */
53 _cups_message_t *msg; /* Current message */
54
55
56 if (argc != 3)
57 {
58 puts("Usage: po2strings filename.po filename.strings");
59 return (1);
60 }
61
62 /*
63 * Use the CUPS .po loader to get the message strings...
64 */
65
66 if ((po = _cupsMessageLoad(argv[1])) == NULL)
67 {
68 perror(argv[1]);
69 return (1);
70 }
71
72 /*
73 * Cheat by using iconv to write the .strings file with a UTF-16 encoding.
74 * The .po file uses UTF-8...
75 */
76
77 snprintf(iconv, sizeof(iconv), "iconv -f utf-8 -t utf-16 >'%s'", argv[2]);
78 if ((strings = popen(iconv, "w")) == NULL)
79 {
80 perror(argv[2]);
81 _cupsMessageFree(po);
82 return (1);
83 }
84
85 for (msg = (_cups_message_t *)cupsArrayFirst(po);
86 msg;
87 msg = (_cups_message_t *)cupsArrayNext(po))
88 {
89 write_string(strings, msg->id);
90 fputs(" = ", strings);
91 write_string(strings, msg->str);
92 fputs(";\n", strings);
93 }
94
95 printf("%s: %d messages.\n", argv[2], cupsArrayCount(po));
96
97 pclose(strings);
98 _cupsMessageFree(po);
99
100 return (0);
101}
102
103
104/*
105 * 'write_string()' - Write a string to the .strings file.
106 */
107
108static void
109write_string(FILE *strings, /* I - .strings file */
110 const char *s) /* I - String to write */
111{
112 putc('\"', strings);
113
114 while (*s)
115 {
116 switch (*s)
117 {
118 case '\n' :
119 fputs("\\n", strings);
120 break;
121 case '\t' :
122 fputs("\\t", strings);
123 break;
124 case '\\' :
125 fputs("\\\\", strings);
126 break;
127 case '\"' :
128 fputs("\\\"", strings);
129 break;
130 default :
131 putc(*s, strings);
132 break;
133 }
134
135 s ++;
136 }
137
138 putc('\"', strings);
139}
140
141
142/*
2e4ff8af 143 * End of "$Id: po2strings.c 6921 2007-09-06 13:38:37Z mike $".
bc44d920 144 */