2017-10-02 08:31:35 +03:00
#!/usr/bin/perl
use strict ;
use Time::Local ;
use PVE::Tools ;
use Data::Dumper ;
use Locale::PO ;
use Getopt::Std ;
use Encode ;
my $ options = { } ;
2017-10-02 13:46:36 +03:00
getopts ( 'o:b:p:' , $ options ) ||
2017-10-02 08:31:35 +03:00
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 ;
}
2017-10-02 13:46:36 +03:00
my $ projectId = $ options - > { p } || die "missing project ID\n" ;
2017-10-02 08:31:35 +03:00
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 - 2017 Proxmox Server Solutions GmbH
2017-10-02 13:30:17 +03:00
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 .
2017-10-02 08:31:35 +03:00
Proxmox Support Team <support\@proxmox.com> , 2017 .
__EOD
my $ ctime = scalar localtime ;
my $ href = { } ;
my $ po = new Locale:: PO ( - msgid = > '' ,
- comment = > $ header ,
- fuzzy = > 1 ,
- msgstr = >
2017-10-02 13:46:36 +03:00
"Project-Id-Version: $projectId\n" .
2017-10-02 08:31:35 +03:00
"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> ) ) {
2017-12-14 15:44:48 +03:00
next if $ line =~ m/^\s*function gettext/ ;
2017-10-02 08:31:35 +03:00
if ( $ line =~ m/gettext\s*\(/ ) {
extract_msg ( $ s , $. , $ line ) ;
}
}
close ( SRC ) ;
}
my $ filename = $ options - > { o } // "messages.pot" ;
Locale::PO - > save_file_fromhash ( $ filename , $ href ) ;