]> git.proxmox.com Git - proxmox-i18n.git/blob - jsgettext.pl
bump version to 3.2.2
[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: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 $findcmd = [['find', @$dirs, '-name', '*.js'],['sort']];
50 PVE::Tools::run_command($findcmd, outfunc => sub {
51 my $line = shift;
52 print "F: $line\n";
53 push @$sources, $line;
54 });
55
56 my $header = <<__EOD;
57 Proxmox message catalog.
58 Copyright (C) 2011-2020 Proxmox Server Solutions GmbH
59 This file is free software: you can redistribute it and\/or modify
60 it under the terms of the GNU Affero General Public License as published by
61 the Free Software Foundation, either version 3 of the License, or
62 (at your option) any later version.
63 Proxmox Support Team <support\@proxmox.com>, 2020.
64 __EOD
65
66 my $ctime = scalar localtime;
67
68 my $href = {};
69 my $po = new Locale::PO(-msgid=> '',
70 -comment=> $header,
71 -fuzzy=> 1,
72 -msgstr=>
73 "Project-Id-Version: $projectId\n" .
74 "Report-Msgid-Bugs-To: <support\@proxmox.com>\n" .
75 "POT-Creation-Date: $ctime\n" .
76 "PO-Revision-Date: YEAR-MO-DA HO:MI +ZONE\n" .
77 "Last-Translator: FULL NAME <EMAIL\@ADDRESS>\n" .
78 "Language-Team: LANGUAGE <support\@proxmox.com>\n" .
79 "MIME-Version: 1.0\n" .
80 "Content-Type: text/plain; charset=UTF-8\n" .
81 "Content-Transfer-Encoding: 8bit\n");
82
83 $href->{''} = $po;
84
85 sub extract_msg {
86 my ($filename, $linenr, $line) = @_;
87
88 my $count = 0;
89
90 while(1) {
91 my $text;
92 if ($line =~ m/\Wgettext\s*\((("((?:[^"\\]++|\\.)*+)")|('((?:[^'\\]++|\\.)*+)'))\)/g) {
93 $text = $3 || $5;
94 }
95
96 last if !$text;
97
98 if ($basehref->{$text}) {
99 return;
100 }
101
102 $count++;
103
104 my $ref = "$filename:$linenr";
105
106 if (my $po = $href->{$text}) {
107 $po->reference($po->reference() . " $ref");
108 } else {
109 my $po = new Locale::PO(-msgid=> $text, -reference=> $ref, -msgstr=> '');
110 $href->{$text} = $po;
111 }
112 };
113
114 die "can't extract gettext message in '$filename' line $linenr\n"
115 if !$count;
116 }
117
118 foreach my $s (@$sources) {
119 open(SRC, $s) || die "unable to open file '$s' - $!\n";
120 while(defined(my $line = <SRC>)) {
121 next if $line =~ m/^\s*function gettext/;
122 if ($line =~ m/gettext\s*\(/) {
123 extract_msg($s, $., $line);
124 }
125 }
126 close(SRC);
127 }
128
129 my $filename = $options->{o} // "messages.pot";
130 Locale::PO->save_file_fromhash($filename, $href);
131