1 /* Writing tcl/msgcat .msg files.
2 Copyright (C) 2002-2003, 2005 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2002.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include "write-tcl.h"
35 #include "msgl-iconv.h"
36 #include "po-charset.h"
40 #include "fwriteerror.h"
42 #include "utf8-ucs4.h"
45 #define _(str) gettext (str)
48 /* Write a string in Tcl Unicode notation to the given stream.
49 Tcl 8 uses Unicode for its internal string representation.
50 In tcl-8.3.3, the .msg files are read in using the locale dependent
51 encoding. The only way to specify strings in an encoding independent
52 form is the \unnnn notation. Newer tcl versions have this fixed:
53 they read the .msg files in UTF-8 encoding. */
55 write_tcl_string (FILE *stream
, const char *str
)
57 static const char hexdigit
[] = "0123456789abcdef";
58 const char *str_limit
= str
+ strlen (str
);
60 fprintf (stream
, "\"");
61 while (str
< str_limit
)
65 count
= u8_mbtouc (&uc
, (const unsigned char *) str
, str_limit
- str
);
68 /* Single UCS-2 'char'. */
70 fprintf (stream
, "\\n");
71 else if (uc
== 0x000d)
72 fprintf (stream
, "\\r");
73 else if (uc
== 0x0022)
74 fprintf (stream
, "\\\"");
75 else if (uc
== 0x0024)
76 fprintf (stream
, "\\$");
77 else if (uc
== 0x005b)
78 fprintf (stream
, "\\[");
79 else if (uc
== 0x005c)
80 fprintf (stream
, "\\\\");
81 else if (uc
== 0x005d)
82 fprintf (stream
, "\\]");
83 /* No need to escape '{' and '}' because we don't have opening
84 braces outside the strings. */
86 else if (uc
== 0x007b)
87 fprintf (stream
, "\\{");
88 else if (uc
== 0x007d)
89 fprintf (stream
, "\\}");
91 else if (uc
>= 0x0020 && uc
< 0x007f)
92 fprintf (stream
, "%c", uc
);
94 fprintf (stream
, "\\u%c%c%c%c",
95 hexdigit
[(uc
>> 12) & 0x0f], hexdigit
[(uc
>> 8) & 0x0f],
96 hexdigit
[(uc
>> 4) & 0x0f], hexdigit
[uc
& 0x0f]);
99 /* The \unnnn notation doesn't support characters >= 0x10000.
100 We output them as UTF-8 byte sequences and hope that either
101 the Tcl version reading them will be new enough or that the
102 user is using an UTF-8 locale. */
103 fwrite (str
, 1, count
, stream
);
106 fprintf (stream
, "\"");
111 write_msg (FILE *output_file
, message_list_ty
*mlp
, const char *locale_name
)
115 /* We don't care about esthetic formattic of the output (like respecting
116 a maximum line width, or including the translator comments) because
117 the \unnnn notation is unesthetic anyway. Translators shall edit
119 for (j
= 0; j
< mlp
->nitems
; j
++)
121 message_ty
*mp
= mlp
->item
[j
];
123 if (mp
->msgid
[0] == '\0')
124 /* Tcl's msgcat unit ignores this, but msgunfmt needs it. */
125 fprintf (output_file
, "set ::msgcat::header ");
128 fprintf (output_file
, "::msgcat::mcset %s ", locale_name
);
129 write_tcl_string (output_file
, mp
->msgid
);
130 fprintf (output_file
, " ");
132 write_tcl_string (output_file
, mp
->msgstr
);
133 fprintf (output_file
, "\n");
138 msgdomain_write_tcl (message_list_ty
*mlp
, const char *canon_encoding
,
139 const char *locale_name
,
140 const char *directory
)
142 /* If no entry for this domain don't even create the file. */
143 if (mlp
->nitems
== 0)
146 /* Determine whether mlp has plural entries. */
152 for (j
= 0; j
< mlp
->nitems
; j
++)
153 if (mlp
->item
[j
]->msgid_plural
!= NULL
)
157 multiline_error (xstrdup (""),
159 message catalog has plural form translations\n\
160 but the Tcl message catalog format doesn't support plural handling\n")));
165 /* Convert the messages to Unicode. */
166 iconv_message_list (mlp
, canon_encoding
, po_charset_utf8
, NULL
);
168 /* Now create the file. */
171 char *frobbed_locale_name
;
176 /* Convert the locale name to lowercase and remove any encoding. */
177 len
= strlen (locale_name
);
178 frobbed_locale_name
= (char *) xallocsa (len
+ 1);
179 memcpy (frobbed_locale_name
, locale_name
, len
+ 1);
180 for (p
= frobbed_locale_name
; *p
!= '\0'; p
++)
181 if (*p
>= 'A' && *p
<= 'Z')
189 file_name
= concatenated_pathname (directory
, frobbed_locale_name
, ".msg");
191 output_file
= fopen (file_name
, "w");
192 if (output_file
== NULL
)
194 error (0, errno
, _("error while opening \"%s\" for writing"),
196 freesa (frobbed_locale_name
);
200 write_msg (output_file
, mlp
, frobbed_locale_name
);
202 /* Make sure nothing went wrong. */
203 if (fwriteerror (output_file
))
204 error (EXIT_FAILURE
, errno
, _("error while writing \"%s\" file"),
207 freesa (frobbed_locale_name
);