]> git.proxmox.com Git - proxmox-i18n.git/blobdiff - po2js.pl
d/control: add missing ${misc:Depends}
[proxmox-i18n.git] / po2js.pl
index 067b3fcc3af28fd393357c868f2092efb90d627d..93c10c1ca8457c5ecd391013a730bfdfba261ea0 100755 (executable)
--- a/po2js.pl
+++ b/po2js.pl
@@ -2,38 +2,29 @@
 
 use strict;
 use warnings;
-use Getopt::Std;
-use Locale::PO;
-use JSON;
+
 use Encode;
+use Getopt::Long;
+use JSON;
+use Locale::PO;
 
 # current limits:
 # - we do not support plural. forms
 # - no message content support
 
 my $options = {};
-
-getopts('o:', $options) ||
-    die "unable to parse options\n";
+GetOptions($options, 't=s', 'o=s', 'v=s') or die "unable to parse options\n";
 
 die "no files specified\n" if !scalar(@ARGV);
 
-#my $filename = shift || die "no po file specified\n";
-
 # like FNV32a, but we only return 31 bits (positive numbers)
 sub fnv31a {
     my ($string) = @_;
 
     my $hval = 0x811c9dc5;
-
-    foreach my $c (unpack('C*', $string)) {
+    for my $c (unpack('C*', $string)) {
        $hval ^= $c;
-       $hval += (
-           (($hval << 1) ) +
-           (($hval << 4) ) +
-           (($hval << 7) ) +
-           (($hval << 8) ) +
-           (($hval << 24) ) );
+       $hval += ($hval << 1) + ($hval << 4) + ($hval << 7) + ($hval << 8) + ($hval << 24);
        $hval = $hval & 0xffffffff;
     }
     return $hval & 0x7fffffff;
@@ -44,7 +35,7 @@ my $catalog = {};
 foreach my $filename (@ARGV) {
     my $href = Locale::PO->load_file_ashash($filename) ||
        die "unable to load '$filename'\n";
-    
+
     my $charset;
     my $hpo = $href->{'""'} || die "no header";
     my $header = $hpo->dequote($hpo->msgstr);
@@ -54,12 +45,15 @@ foreach my $filename (@ARGV) {
        die "unable to get charset\n" if !$charset;
     }
 
-
-    foreach my $k (keys %$href) {
+    for my $k (keys %$href) {
        my $po = $href->{$k};
        next if $po->fuzzy(); # skip fuzzy entries
-       my $ref = $po->reference();
-    
+       my $ref = $po->reference() or next; # skip unused entries
+
+       # skip entries if "t" is defined (pve/pmg) and the string is
+       # not used there or in the widget toolkit
+       next if $options->{t} && $ref !~ m/($options->{t}|proxmox)\-/;
+
        my $qmsgid = decode($charset, $po->msgid);
        my $msgid = $po->dequote($qmsgid);
 
@@ -67,12 +61,11 @@ foreach my $filename (@ARGV) {
        my $msgstr = $po->dequote($qmsgstr);
 
        next if !length($msgid); # skip header
-       
        next if !length($msgstr); # skip untranslated entries
 
        my $digest = fnv31a($msgid);
 
-       die "duplicate digest" if $catalog->{$digest};
+       die "duplicate digest '$digest' (msgid '$msgid')\n" if $catalog->{$digest};
 
        $catalog->{$digest} = [ $msgstr ];
        # later, we can add plural forms to the array
@@ -81,20 +74,19 @@ foreach my $filename (@ARGV) {
 
 my $json = to_json($catalog, {canonical => 1, utf8 => 1});
 
-my $content = '';
+my $version = $options->{v} // ("dev-build " . localtime());
+my $content = "// $version\n"; # write version to the beginning to better avoid stale cache
 
 my $outfile = $options->{o};
 
 $content .= "// Proxmox Message Catalog: $outfile\n" if $outfile;
 
 $content .= <<__EOD;
-Proxmox = { i18n_msgcat: $json }
+const __proxmox_i18n_msgcat__ = $json;
 
 function fnv31a(text) {
-    var len = text.length;
-    var hval = 0x811c9dc5;
-    for (var i = 0; i < len; i++) {
-       var c = text.charCodeAt(i);
+    let hval = 0x811c9dc5;
+    for (const c of text) {
        hval ^= c;
        hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24);
     }
@@ -103,12 +95,8 @@ function fnv31a(text) {
 }
 
 function gettext(buf) {
-    var digest = fnv31a(buf);
-    var data = Proxmox.i18n_msgcat[digest];
-    if (!data) {
-       return buf;
-    }
-    return data[0] || buf;
+    let digest = fnv31a(buf);
+    return __proxmox_i18n_msgcat__[digest]?.[0] ?? buf;
 }
 __EOD