]> git.proxmox.com Git - proxmox-i18n.git/blob - jsgettext.pl
fix license string
[proxmox-i18n.git] / jsgettext.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use Time::Local;
5 use PVE::Tools;
6 use Data::Dumper;
7 use Locale::PO;
8 use Getopt::Std;
9 use Encode;
10
11 my $options = {};
12
13 getopts('o:b:', $options) ||
14 die "unable to parse options\n";
15
16 my $dirs = [@ARGV];
17
18 die "no directory specified\n" if !scalar(@$dirs);
19
20 foreach my $dir (@$dirs) {
21 die "no such directory '$dir'\n" if ! -d $dir;
22 }
23
24 my $basehref = {};
25 if (my $base = $options->{b}) {
26 my $aref = Locale::PO->load_file_asarray($base) ||
27 die "unable to load '$base'\n";
28
29 my $charset;
30 my $hpo = $aref->[0] || die "no header";
31 my $header = $hpo->dequote($hpo->msgstr);
32 if ($header =~ m|^Content-Type:\s+text/plain;\s+charset=(\S+)$|im) {
33 $charset = $1;
34 } else {
35 die "unable to get charset\n" if !$charset;
36 }
37
38 foreach my $po (@$aref) {
39 my $qmsgid = decode($charset, $po->msgid);
40 my $msgid = $po->dequote($qmsgid);
41 $basehref->{$msgid} = $po;
42 }
43 }
44
45 my $sources = [];
46
47 my $findcmd = [['find', @$dirs, '-name', '*.js'],['sort']];
48 PVE::Tools::run_command($findcmd, outfunc => sub {
49 my $line = shift;
50 print "F: $line\n";
51 push @$sources, $line;
52 });
53
54 my $header = <<__EOD;
55 Proxmox message catalog.
56 Copyright (C) 2011-2017 Proxmox Server Solutions GmbH
57 This file is free software: you can redistribute it and\/or modify
58 it under the terms of the GNU Affero General Public License as published by
59 the Free Software Foundation, either version 3 of the License, or
60 (at your option) any later version.
61 Proxmox Support Team <support\@proxmox.com>, 2017.
62 __EOD
63
64 my $ctime = scalar localtime;
65
66 my $href = {};
67 my $po = new Locale::PO(-msgid=> '',
68 -comment=> $header,
69 -fuzzy=> 1,
70 -msgstr=>
71 "Project-Id-Version: proxmox-mailgateway 5.0\n" .
72 "Report-Msgid-Bugs-To: <support\@proxmox.com>\n" .
73 "POT-Creation-Date: $ctime\n" .
74 "PO-Revision-Date: YEAR-MO-DA HO:MI +ZONE\n" .
75 "Last-Translator: FULL NAME <EMAIL\@ADDRESS>\n" .
76 "Language-Team: LANGUAGE <support\@proxmox.com>\n" .
77 "MIME-Version: 1.0\n" .
78 "Content-Type: text/plain; charset=UTF-8\n" .
79 "Content-Transfer-Encoding: 8bit\n");
80
81 $href->{''} = $po;
82
83 sub extract_msg {
84 my ($filename, $linenr, $line) = @_;
85
86 my $count = 0;
87
88 while(1) {
89 my $text;
90 if ($line =~ m/\Wgettext\s*\((("((?:[^"\\]++|\\.)*+)")|('((?:[^'\\]++|\\.)*+)'))\)/g) {
91 $text = $3 || $5;
92 }
93
94 last if !$text;
95
96 if ($basehref->{$text}) {
97 return;
98 }
99
100 $count++;
101
102 my $ref = "$filename:$linenr";
103
104 if (my $po = $href->{$text}) {
105 $po->reference($po->reference() . " $ref");
106 } else {
107 my $po = new Locale::PO(-msgid=> $text, -reference=> $ref, -msgstr=> '');
108 $href->{$text} = $po;
109 }
110 };
111
112 die "can't extract gettext message in '$filename' line $linenr\n"
113 if !$count;
114 }
115
116 foreach my $s (@$sources) {
117 open(SRC, $s) || die "unable to open file '$s' - $!\n";
118 while(defined(my $line = <SRC>)) {
119 next if $line =~ m/^function gettext/;
120 if ($line =~ m/gettext\s*\(/) {
121 extract_msg($s, $., $line);
122 }
123 }
124 close(SRC);
125 }
126
127 my $filename = $options->{o} // "messages.pot";
128 Locale::PO->save_file_fromhash($filename, $href);
129