]> git.proxmox.com Git - proxmox-i18n.git/blob - jsgettext.pl
js generator: avoid dependency on lipve-common-perl for just run_command
[proxmox-i18n.git] / jsgettext.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Encode;
7 use Getopt::Std;
8 use Locale::PO;
9 use Time::Local;
10
11 my $options = {};
12
13 getopts('o:b:p:', $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 $projectId = $options->{p} || die "missing project ID\n";
25
26 my $basehref = {};
27 if (my $base = $options->{b}) {
28 my $aref = Locale::PO->load_file_asarray($base) ||
29 die "unable to load '$base'\n";
30
31 my $charset;
32 my $hpo = $aref->[0] || die "no header";
33 my $header = $hpo->dequote($hpo->msgstr);
34 if ($header =~ m|^Content-Type:\s+text/plain;\s+charset=(\S+)$|im) {
35 $charset = $1;
36 } else {
37 die "unable to get charset\n" if !$charset;
38 }
39
40 foreach my $po (@$aref) {
41 my $qmsgid = decode($charset, $po->msgid);
42 my $msgid = $po->dequote($qmsgid);
43 $basehref->{$msgid} = $po;
44 }
45 }
46
47 my $sources = [];
48
49 my $find_cmd = 'find ';
50 # shell quote heuristic, with the (here safe) assumption that the dirs don't contain single-quotes
51 $find_cmd .= join(' ', map { "'$_'" } $dirs->@*);
52 $find_cmd .= ' -name "*.js"';
53 open(my $find_cmd_output, '-|', "$find_cmd | sort") or die "Failed to execute command: $!";
54
55 # Iterate through the sorted output line by line
56 while (my $line = <$find_cmd_output>) {
57 chomp $line;
58 print "F: $line\n";
59 push @$sources, $line;
60 }
61 close($find_cmd_output);
62
63
64 my $header = <<__EOD;
65 Proxmox message catalog.
66
67 Copyright (C) Proxmox Server Solutions GmbH
68
69 This file is free software: you can redistribute it and\/or modify it under the terms of the GNU
70 Affero General Public License as published by the Free Software Foundation, either version 3 of the
71 License, or (at your option) any later version.
72 -- Proxmox Support Team <support\@proxmox.com>
73 __EOD
74
75 my $ctime = scalar localtime;
76
77 my $href = {};
78 my $po = Locale::PO->new(
79 -msgid => '',
80 -comment => $header,
81 -fuzzy => 1,
82 -msgstr => "Project-Id-Version: $projectId\n"
83 ."Report-Msgid-Bugs-To: <support\@proxmox.com>\n"
84 ."POT-Creation-Date: $ctime\n"
85 ."PO-Revision-Date: YEAR-MO-DA HO:MI +ZONE\n"
86 ."Last-Translator: FULL NAME <EMAIL\@ADDRESS>\n"
87 ."Language-Team: LANGUAGE <support\@proxmox.com>\n"
88 ."MIME-Version: 1.0\n"
89 ."Content-Type: text/plain; charset=UTF-8\n"
90 ."Content-Transfer-Encoding: 8bit\n",
91 );
92
93 $href->{''} = $po;
94
95 sub extract_msg {
96 my ($filename, $linenr, $line) = @_;
97
98 my $count = 0;
99
100 while(1) {
101 my $text;
102 if ($line =~ m/\bgettext\s*\((("((?:[^"\\]++|\\.)*+)")|('((?:[^'\\]++|\\.)*+)'))\)/g) {
103 $text = $3 || $5;
104 }
105
106 last if !$text;
107
108 if ($basehref->{$text}) {
109 return;
110 }
111
112 $count++;
113
114 my $ref = "$filename:$linenr";
115
116 if (my $po = $href->{$text}) {
117 $po->reference($po->reference() . " $ref");
118 } else {
119 my $po = Locale::PO->new(-msgid=> $text, -reference=> $ref, -msgstr=> '');
120 $href->{$text} = $po;
121 }
122 };
123
124 die "can't extract gettext message in '$filename' line $linenr\n"
125 if !$count;
126 }
127
128 foreach my $s (@$sources) {
129 open(my $SRC_FH, '<', $s) || die "unable to open file '$s' - $!\n";
130 while(defined(my $line = <$SRC_FH>)) {
131 if ($line =~ m/gettext\s*\(/ && $line !~ m/^\s*function gettext/) {
132 extract_msg($s, $., $line);
133 }
134 }
135 close($SRC_FH);
136 }
137
138 my $filename = $options->{o} // "messages.pot";
139 Locale::PO->save_file_fromhash($filename, $href);
140