]> git.proxmox.com Git - proxmox-i18n.git/blame - jsgettext.pl
update traditional Chinese translations
[proxmox-i18n.git] / jsgettext.pl
CommitLineData
3f7b1143
DM
1#!/usr/bin/perl
2
3use strict;
4use Time::Local;
5use PVE::Tools;
6use Data::Dumper;
7use Locale::PO;
8use Getopt::Std;
9use Encode;
10
11my $options = {};
12
1bddddef 13getopts('o:b:p:', $options) ||
3f7b1143
DM
14 die "unable to parse options\n";
15
16my $dirs = [@ARGV];
17
18die "no directory specified\n" if !scalar(@$dirs);
19
20foreach my $dir (@$dirs) {
21 die "no such directory '$dir'\n" if ! -d $dir;
22}
23
1bddddef
DM
24my $projectId = $options->{p} || die "missing project ID\n";
25
3f7b1143
DM
26my $basehref = {};
27if (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
47my $sources = [];
48
49my $findcmd = [['find', @$dirs, '-name', '*.js'],['sort']];
50PVE::Tools::run_command($findcmd, outfunc => sub {
51 my $line = shift;
52 print "F: $line\n";
53 push @$sources, $line;
54});
55
56my $header = <<__EOD;
57Proxmox message catalog.
04b8b6c6 58Copyright (C) 2011-2020 Proxmox Server Solutions GmbH
5a4f7717
DM
59This file is free software: you can redistribute it and\/or modify
60it under the terms of the GNU Affero General Public License as published by
61the Free Software Foundation, either version 3 of the License, or
62(at your option) any later version.
04b8b6c6 63Proxmox Support Team <support\@proxmox.com>, 2020.
3f7b1143
DM
64__EOD
65
66my $ctime = scalar localtime;
67
68my $href = {};
69my $po = new Locale::PO(-msgid=> '',
70 -comment=> $header,
71 -fuzzy=> 1,
72 -msgstr=>
1bddddef 73 "Project-Id-Version: $projectId\n" .
3f7b1143
DM
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
85sub 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
118foreach my $s (@$sources) {
119 open(SRC, $s) || die "unable to open file '$s' - $!\n";
120 while(defined(my $line = <SRC>)) {
1c532813 121 next if $line =~ m/^\s*function gettext/;
3f7b1143
DM
122 if ($line =~ m/gettext\s*\(/) {
123 extract_msg($s, $., $line);
124 }
125 }
126 close(SRC);
127}
128
129my $filename = $options->{o} // "messages.pot";
130Locale::PO->save_file_fromhash($filename, $href);
131