Updated language files.
[mp-5.x.git] / tools / po2mpsl.mpsl
blobfe5bc50525a0f2e7de31d0852baa0839bca10b71
1 /*
3         Parses .po files and dumps MPSL code.
5         Angel Ortega <angel@triptico.com>
7 */
9 sub convert_po_line(line)
11         line = sregex('/^\s*"/', line);
12         line = sregex('/"$/', line);
14         line = split(NULL, line);
16         line = join('',
17                         map(sub (e) {
18                                 (ord(e) > 127) &&
19                                 sprintf("\\x{%04x}", ord(e)) ||
20                                 e ;
21                         },
22                         line
23                 )
24         );
26         return line;
30 sub parse_po_file(pofile)
32         local f;
33         local r = [];
35         /* only UTF-8 po files are supported */
36         encoding('UTF-8');
38         if ((f = open(pofile, "r")) != NULL) {
40                 local l, k;
41                 local v = [];
43                 push(r, "/* Built by po2mpsl - Don't modify, change the .po file instead */");
44                 push(r, "__I18N__ = {");
46                 while ((l = read(f)) != NULL) {
48                         l = mp.chomp(l);
50                         local s = regex([ '/^(msgid|msgstr)*/', '/\s*\".*\"$/' ], l);
51                         s[1] = convert_po_line(s[1]);
53                         if (s[0] eq 'msgid') {
54                                 if (k) {
55                                         k = grep(sub (e) { e != NULL; }, k);
56                                         v = grep(sub (e) { e != NULL; }, v);
58                                         if (size(v)) {
59                                                 push(r, "\t\"" ~
60                                                         join('', k) ~
61                                                         "\" => \"" ~
62                                                         join('', v) ~
63                                                         "\","
64                                                 );
65                                         }
67                                         k = NULL;
68                                 }
70                                 v = [ s[1] ];
71                         }
72                         else
73                         if (s[0] eq 'msgstr') {
74                                 k = v;
75                                 v = [ s[1] ];
76                         }
77                         else
78                         if (s[1]) {
79                                 push(v, s[1]);
80                         }
81                 }
83                 push(r, "\t\"-\" => \"-\"\n};");
85                 close(f);
86         }
88         /* back to default encoding */
89         encoding();
91         return join("\n", r);
94 /* main */
96 foreach (f, glob('po/*.po')) { /**/
97         local output = parse_po_file(f);
99         /* strip extension and path */
100         f = shift(split('.po', f));
101         f = pop(split('/', f));
103         local o = open('lang/' ~ f ~ '.mpsl', 'w');
104         write(o, output);
105         close(o);
108 mp.exit();