]> git.ipfire.org Git - thirdparty/cups.git/blob - locale/po2strings.c
Merge changes from CUPS 1.5svn-r9049 (private header support)
[thirdparty/cups.git] / locale / po2strings.c
1 /*
2 * "$Id: po2strings.c 6921 2007-09-06 13:38:37Z mike $"
3 *
4 * Convert GNU gettext .po files to Apple .strings file (UTF-16 BE text file).
5 *
6 * Copyright 2007-2010 by Apple Inc.
7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Apple Inc. and are protected by Federal copyright
10 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
11 * which should have been included with this file. If this file is
12 * file is missing or damaged, see the license at "http://www.cups.org/".
13 *
14 * Usage:
15 *
16 * po2strings filename.strings filename.po
17 *
18 * Compile with:
19 *
20 * gcc -o po2strings po2strings.c `cups-config --libs`
21 *
22 * Contents:
23 *
24 * main() - Convert .po file to .strings.
25 * write_string() - Write a string to the .strings file.
26 */
27
28 #include <cups/cups-private.h>
29
30
31 /*
32 * The .strings file format is simple:
33 *
34 * // comment
35 * "id" = "str";
36 *
37 * Both the id and str strings use standard C quoting for special characters
38 * like newline and the double quote character.
39 */
40
41 /*
42 * Local functions...
43 */
44
45 static void write_string(FILE *strings, const char *s);
46
47
48 /*
49 * main() - Convert .po file to .strings.
50 */
51
52 int /* O - Exit code */
53 main(int argc, /* I - Number of command-line args */
54 char *argv[]) /* I - Command-line arguments */
55 {
56 int i; /* Looping var */
57 FILE *strings; /* .strings file */
58 cups_array_t *po; /* .po file */
59 char iconv[1024]; /* iconv command */
60 _cups_message_t *msg; /* Current message */
61 const char *srcfile, /* Source file */
62 *dstfile; /* Destination file */
63 int use_msgid; /* Use msgid strings for msgstr? */
64
65
66 srcfile = NULL;
67 dstfile = NULL;
68 use_msgid = 0;
69
70
71 for (i = 1; i < argc; i ++)
72 if (!strcmp(argv[i], "-m"))
73 use_msgid = 1;
74 else if (argv[i][0] == '-')
75 {
76 puts("Usage: po2strings [-m] filename.po filename.strings");
77 return (1);
78 }
79 else if (srcfile == NULL)
80 srcfile = argv[i];
81 else if (dstfile == NULL)
82 dstfile = argv[i];
83 else
84 {
85 puts("Usage: po2strings [-m] filename.po filename.strings");
86 return (1);
87 }
88
89 if (!srcfile || !dstfile)
90 {
91 puts("Usage: po2strings [-m] filename.po filename.strings");
92 return (1);
93 }
94
95 /*
96 * Use the CUPS .po loader to get the message strings...
97 */
98
99 if ((po = _cupsMessageLoad(srcfile, 1)) == NULL)
100 {
101 perror(srcfile);
102 return (1);
103 }
104
105 /*
106 * Cheat by using iconv to write the .strings file with a UTF-16 encoding.
107 * The .po file uses UTF-8...
108 */
109
110 snprintf(iconv, sizeof(iconv), "iconv -f utf-8 -t utf-16 >'%s'", dstfile);
111 if ((strings = popen(iconv, "w")) == NULL)
112 {
113 perror(argv[2]);
114 _cupsMessageFree(po);
115 return (1);
116 }
117
118 for (msg = (_cups_message_t *)cupsArrayFirst(po);
119 msg;
120 msg = (_cups_message_t *)cupsArrayNext(po))
121 {
122 write_string(strings, msg->id);
123 fputs(" = ", strings);
124 write_string(strings, use_msgid ? msg->id : msg->str);
125 fputs(";\n", strings);
126 }
127
128 printf("%s: %d messages.\n", argv[2], cupsArrayCount(po));
129
130 pclose(strings);
131 _cupsMessageFree(po);
132
133 return (0);
134 }
135
136
137 /*
138 * 'write_string()' - Write a string to the .strings file.
139 */
140
141 static void
142 write_string(FILE *strings, /* I - .strings file */
143 const char *s) /* I - String to write */
144 {
145 putc('\"', strings);
146
147 while (*s)
148 {
149 switch (*s)
150 {
151 case '\n' :
152 fputs("\\n", strings);
153 break;
154 case '\t' :
155 fputs("\\t", strings);
156 break;
157 case '\\' :
158 fputs("\\\\", strings);
159 break;
160 case '\"' :
161 fputs("\\\"", strings);
162 break;
163 default :
164 putc(*s, strings);
165 break;
166 }
167
168 s ++;
169 }
170
171 putc('\"', strings);
172 }
173
174
175 /*
176 * End of "$Id: po2strings.c 6921 2007-09-06 13:38:37Z mike $".
177 */