]> git.proxmox.com Git - proxmox-i18n.git/blame - po2js.pl
copied scripts from pve-manager, modified to support proxmox-widget-toolkit
[proxmox-i18n.git] / po2js.pl
CommitLineData
3f7b1143
DM
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Locale::PO;
7use JSON;
8use Encode;
9
10# current limits:
11# - we do not support plural. forms
12# - no message content support
13
14die "no files specified\n" if !scalar(@ARGV);
15
16#my $filename = shift || die "no po file specified\n";
17
18# like FNV32a, but we only return 31 bits (positive numbers)
19sub fnv31a {
20 my ($string) = @_;
21
22 my $hval = 0x811c9dc5;
23
24 foreach my $c (unpack('C*', $string)) {
25 $hval ^= $c;
26 $hval += (
27 (($hval << 1) ) +
28 (($hval << 4) ) +
29 (($hval << 7) ) +
30 (($hval << 8) ) +
31 (($hval << 24) ) );
32 $hval = $hval & 0xffffffff;
33 }
34 return $hval & 0x7fffffff;
35}
36
37my $catalog;
38
39foreach my $filename (@ARGV) {
40 my $href = Locale::PO->load_file_ashash($filename) ||
41 die "unable to load '$filename'\n";
42
43 my $charset;
44 my $hpo = $href->{'""'} || die "no header";
45 my $header = $hpo->dequote($hpo->msgstr);
46 if ($header =~ m|^Content-Type:\s+text/plain;\s+charset=(\S+)$|im) {
47 $charset = $1;
48 } else {
49 die "unable to get charset\n" if !$charset;
50 }
51
52
53 foreach my $k (keys %$href) {
54 my $po = $href->{$k};
55 next if $po->fuzzy(); # skip fuzzy entries
56 my $ref = $po->reference();
57
58 my $qmsgid = decode($charset, $po->msgid);
59 my $msgid = $po->dequote($qmsgid);
60
61 my $qmsgstr = decode($charset, $po->msgstr);
62 my $msgstr = $po->dequote($qmsgstr);
63
64 next if !length($msgid); # skip header
65
66 #next if !length($msgstr); # skip untranslated entries
67
68 my $digest = fnv31a($msgid);
69
70 die "duplicate digest" if $catalog->{$digest};
71
72 $catalog->{$digest} = [ $msgstr ];
73 # later, we can add plural forms to the array
74 }
75}
76
77my $json = to_json($catalog, {canonical => 1, utf8 => 1});
78
79print <<__EOD
80PVE = { i18n_msgcat: $json }
81
82function fnv31a(text) {
83 var len = text.length;
84 var hval = 0x811c9dc5;
85 for (var i = 0; i < len; i++) {
86 var c = text.charCodeAt(i);
87 hval ^= c;
88 hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24);
89 }
90 hval &= 0x7fffffff;
91 return hval;
92}
93
94function gettext(buf) {
95 var digest = fnv31a(buf);
96 var data = PVE.i18n_msgcat[digest];
97 if (!data) {
98 return buf;
99 }
100 return data[0] || buf;
101}
102
103__EOD
104