]> git.ipfire.org Git - thirdparty/cups.git/blob - locale/strings2po.c
6f912bb1908ca5270dd5c25fae3315a33ac2a52d
[thirdparty/cups.git] / locale / strings2po.c
1 /*
2 * Convert Apple .strings file (UTF-16 BE text file) to GNU gettext .po files.
3 *
4 * Copyright 2007-2014 by Apple Inc.
5 *
6 * These coded instructions, statements, and computer programs are the
7 * property of Apple Inc. and are protected by Federal copyright
8 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
9 * which should have been included with this file. If this file is
10 * file is missing or damaged, see the license at "http://www.cups.org/".
11 *
12 * Usage:
13 *
14 * strings2po filename.strings filename.po
15 *
16 * Compile with:
17 *
18 * gcc -o strings2po strings2po.c
19 */
20
21 #include <stdio.h>
22 #include <stdlib.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
39 static int read_strings(FILE *strings, char *buffer, size_t bufsize,
40 char **id, char **str);
41 static void write_po(FILE *po, const char *what, const char *s);
42
43
44 /*
45 * main() - Convert .strings file to .po.
46 */
47
48 int /* O - Exit code */
49 main(int argc, /* I - Number of command-line args */
50 char *argv[]) /* I - Command-line arguments */
51 {
52 FILE *strings, /* .strings file */
53 *po; /* .po file */
54 char iconv[1024], /* iconv command */
55 buffer[8192], /* Line buffer */
56 *id, /* ID string */
57 *str; /* Translation string */
58 int count; /* Number of messages converted */
59
60
61 if (argc != 3)
62 {
63 puts("Usage: strings2po filename.strings filename.po");
64 return (1);
65 }
66
67 /*
68 * Cheat by using iconv to convert the .strings file from UTF-16 to UTF-8
69 * which is what we need for the .po file (and it makes things a lot
70 * simpler...)
71 */
72
73 snprintf(iconv, sizeof(iconv), "iconv -f utf-16 -t utf-8 '%s'", argv[1]);
74 if ((strings = popen(iconv, "r")) == NULL)
75 {
76 perror(argv[1]);
77 return (1);
78 }
79
80 if ((po = fopen(argv[2], "w")) == NULL)
81 {
82 perror(argv[2]);
83 pclose(strings);
84 return (1);
85 }
86
87 count = 0;
88
89 while (read_strings(strings, buffer, sizeof(buffer), &id, &str))
90 {
91 count ++;
92 write_po(po, "msgid", id);
93 write_po(po, "msgstr", str);
94 }
95
96 pclose(strings);
97 fclose(po);
98
99 printf("%s: %d messages.\n", argv[2], count);
100
101 return (0);
102 }
103
104
105 /*
106 * 'read_strings()' - Read a line from a .strings file.
107 */
108
109 static int /* O - 1 on success, 0 on failure */
110 read_strings(FILE *strings, /* I - .strings file */
111 char *buffer, /* I - Line buffer */
112 size_t bufsize, /* I - Size of line buffer */
113 char **id, /* O - Pointer to ID string */
114 char **str) /* O - Pointer to translation string */
115 {
116 char *bufptr; /* Pointer into buffer */
117
118
119 while (fgets(buffer, (int)bufsize, strings))
120 {
121 if (buffer[0] != '\"')
122 continue;
123
124 *id = buffer + 1;
125
126 for (bufptr = buffer + 1; *bufptr && *bufptr != '\"'; bufptr ++)
127 if (*bufptr == '\\')
128 bufptr ++;
129
130 if (*bufptr != '\"')
131 continue;
132
133 *bufptr++ = '\0';
134
135 while (*bufptr && *bufptr != '\"')
136 bufptr ++;
137
138 if (!*bufptr)
139 continue;
140
141 bufptr ++;
142 *str = bufptr;
143
144 for (; *bufptr && *bufptr != '\"'; bufptr ++)
145 if (*bufptr == '\\')
146 bufptr ++;
147
148 if (*bufptr != '\"')
149 continue;
150
151 *bufptr = '\0';
152
153 return (1);
154 }
155
156 return (0);
157 }
158
159
160 /*
161 * 'write_po()' - Write a line to the .po file.
162 */
163
164 static void
165 write_po(FILE *po, /* I - .po file */
166 const char *what, /* I - Type of string */
167 const char *s) /* I - String to write */
168 {
169 fprintf(po, "%s \"%s\"\n", what, s);
170 }