]> git.proxmox.com Git - proxmox-i18n.git/blob - po2js.pl
update Traditional Chinese translations
[proxmox-i18n.git] / po2js.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Encode;
7 use Getopt::Long;
8 use JSON;
9 use Locale::PO;
10
11 # current limits:
12 # - we do not support plural. forms
13 # - no message content support
14
15 my $options = {};
16 GetOptions($options, 't=s', 'o=s', 'v=s') or die "unable to parse options\n";
17
18 die "no files specified\n" if !scalar(@ARGV);
19
20 #my $filename = shift || die "no po file specified\n";
21
22 # like FNV32a, but we only return 31 bits (positive numbers)
23 sub fnv31a {
24 my ($string) = @_;
25
26 my $hval = 0x811c9dc5;
27
28 foreach my $c (unpack('C*', $string)) {
29 $hval ^= $c;
30 $hval += (
31 (($hval << 1) ) +
32 (($hval << 4) ) +
33 (($hval << 7) ) +
34 (($hval << 8) ) +
35 (($hval << 24) ) );
36 $hval = $hval & 0xffffffff;
37 }
38 return $hval & 0x7fffffff;
39 }
40
41 my $catalog = {};
42
43 foreach my $filename (@ARGV) {
44 my $href = Locale::PO->load_file_ashash($filename) ||
45 die "unable to load '$filename'\n";
46
47 my $charset;
48 my $hpo = $href->{'""'} || die "no header";
49 my $header = $hpo->dequote($hpo->msgstr);
50 if ($header =~ m|^Content-Type:\s+text/plain;\s+charset=(\S+)$|im) {
51 $charset = $1;
52 } else {
53 die "unable to get charset\n" if !$charset;
54 }
55
56
57 foreach my $k (keys %$href) {
58 my $po = $href->{$k};
59 next if $po->fuzzy(); # skip fuzzy entries
60 my $ref = $po->reference();
61
62 # skip unused entries
63 next if !$ref;
64
65 # skip entries if t is defined (pve/pmg) and the string is
66 # not used there or in the widget toolkit
67 next if $options->{t} && $ref !~ m/($options->{t}|proxmox)\-/;
68
69 my $qmsgid = decode($charset, $po->msgid);
70 my $msgid = $po->dequote($qmsgid);
71
72 my $qmsgstr = decode($charset, $po->msgstr);
73 my $msgstr = $po->dequote($qmsgstr);
74
75 next if !length($msgid); # skip header
76
77 next if !length($msgstr); # skip untranslated entries
78
79 my $digest = fnv31a($msgid);
80
81 die "duplicate digest" if $catalog->{$digest};
82
83 $catalog->{$digest} = [ $msgstr ];
84 # later, we can add plural forms to the array
85 }
86 }
87
88 my $json = to_json($catalog, {canonical => 1, utf8 => 1});
89
90 my $version = $options->{v} // ("dev-build " . localtime());
91 my $content = "// $version\n"; # write version to the beginning to better avoid stale cache
92
93 my $outfile = $options->{o};
94
95 $content .= "// Proxmox Message Catalog: $outfile\n" if $outfile;
96
97 $content .= <<__EOD;
98 __proxmox_i18n_msgcat__ = $json;
99
100 function fnv31a(text) {
101 var len = text.length;
102 var hval = 0x811c9dc5;
103 for (var i = 0; i < len; i++) {
104 var c = text.charCodeAt(i);
105 hval ^= c;
106 hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24);
107 }
108 hval &= 0x7fffffff;
109 return hval;
110 }
111
112 function gettext(buf) {
113 var digest = fnv31a(buf);
114 var data = __proxmox_i18n_msgcat__[digest];
115 if (!data) {
116 return buf;
117 }
118 return data[0] || buf;
119 }
120 __EOD
121
122 if ($outfile) {
123 open(my $fh, '>', $outfile) ||
124 die "unable to open '$outfile' - $!\n";
125 print $fh $content;
126 } else {
127 print $content;
128 }