Version 5.1.3 RELEASED.
[mp-5.x.git] / tools / po2mpsl.mpsl
blob837ecd656986fb3493daf4ed48efe1b231b55bdc
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;
41                 local i = 0;
42                 local v = [];
44                 while ((l = read(f)) != NULL) {
46                         l = mp.chomp(l);
48                         local s = regex([ '/^(msgid|msgstr)*/', '/\s*\".*\"$/' ], l);
49                         s[1] = convert_po_line(s[1]);
51                         if (s[0] eq 'msgid') {
52                                 i = 0;
53                                 push(v, [[], []]);
54                         }
55                         else
56                         if (s[0] eq 'msgstr') {
57                                 i = 1;
58                         }
60                         if (s[1]) {
61                                 push(v[-1][i], s[1]);
62                         }
63                 }
65                 push(r, "/* Built by po2mpsl - Don't modify, change the .po file instead */");
66                 push(r, "__I18N__ = {");
68                 foreach (l, v) {
69                         if (size(l[1]))
70                                 push(r, "\t\"" ~ join('', l[0]) ~ "\" => \"" ~ join('', l[1]) ~ "\",");
71                 }
73                 push(r, "\t\"-\" => \"-\"\n};\n");
75                 close(f);
76         }
78         /* back to default encoding */
79         encoding();
81         return join("\n", r);
84 /* main */
86 foreach (f, glob('po/*.po')) { /**/
87         local output = parse_po_file(f);
89         /* strip extension and path */
90         f = shift(split('.po', f));
91         f = pop(split('/', f));
93         local o = open('lang/' ~ f ~ '.mpsl', 'w');
94         write(o, output);
95         close(o);
98 mp.exit();