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