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