-
Notifications
You must be signed in to change notification settings - Fork 0
/
translator-lib.pl
executable file
·2104 lines (1834 loc) · 48.1 KB
/
translator-lib.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (C) 2004-2021
# Emmanuel Saracco <emmanuel@esaracco.fr>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330,
# Boston, MA 02111-1307, USA.
use WebminCore;
&init_config();
# to translate usermin
&foreign_require ("usermin", "usermin-lib.pl");
# to use mailboxes settings
if ($config{'trans_use_mailboxes'})
{
&foreign_require("mailboxes", "mailboxes-lib.pl");
}
%config_usermin = ();
&usermin::get_usermin_miniserv_config(\%config_usermin);
# global array for bad perl modules
# dependencies
my %deps = ();
eval'use File::Path qw(make_path)';$deps{'File::Path'} = 1 if ($@);
eval'use File::Copy';$deps{'File::Copy'} = 1 if ($@);
eval'use Encode';$deps{'Encode'} = 1 if ($@);
eval'use JSON';$deps{'JSON'} = 1 if ($@);
eval'use HTML::Entities';$deps{'HTML::Entities'} = 1 if ($@);
eval'use POSIX';$deps{'POSIX'} = 1 if ($@);
eval'use File::Basename';$deps{'File::Basename'} = 1 if ($@);
eval'use Digest::MD5 qw(md5_hex)';$deps{'Digest::MD5'} = 1 if ($@);
eval'use Date::Manip';$deps{'Date::Manip'} = 1 if ($@);
eval'use MIME::Lite';$deps{'MIME::Lite'} = 1 if ($@);
eval'use LWP::UserAgent';$deps{'LWP::UserAgent'} = 1 if ($@);
eval'use Mail::Sender';$deps{'Mail::Sender'} = 1 if ($@);
eval'use Digest::HMAC_MD5';$deps{'Digest::HMAC_MD5'} = 1 if ($@);
eval'use Authen::NTLM';$deps{'Authen::NTLM'} = 1 if ($@);
# all binaries paths are here
my $all_path = (); &trans_init_all_path ();
&ReadParse();
our $ref_lang = &trans_get_ref_lang ($in{'app'});
# trans_get_ref_lang ()
# IN: -
# OUT: -
#
# init the global var for current ref language
#
sub trans_get_ref_lang ( $ )
{
my $app = shift;
my $dir = ($config{'trans_webmin'}) ? 'lang' : 'ulang';
return (
$app &&
$current_lang =~ /\.(.+)$/ &&
-s &trans_get_path ($app, "$dir/en.$1")
) ? "en.$1" : 'en';
}
# trans_check_new_release ()
# OUT: New release version.
#
# Check if a new wbmtranslator release is available.
#
sub trans_check_new_release ()
{
my $r = (LWP::UserAgent->new())->get("https://wbmtranslator.esaracco.fr/VERSION");
my ($local_version) = $module_info{'version'} =~ /^([^g]+)/;
return ($r->is_success && $r->content =~ /^([\d\.]+)/ &&
$1 ne $local_version) ? $1 : '';
}
# clean config inputs
&trans_trim_config ();
# trans_check_config ()
# IN: -
# OUT: -
#
# check if the module config is ok. if not, display a warning
# and exit
#
sub trans_main_check_config ()
{
my $ok = 0;
my $msg = '';
if (!$config{'trans_webmin'} && !%config_usermin)
{
$msg = $text{'MSG_ERROR_USERMIN'};
}
elsif ($config{'trans_working_path'} eq '' ||
! -d $config{'trans_working_path'})
{
$msg = $text{'MSG_ERROR_WORKING_PATH'}
}
elsif ($config{'trans_email'} eq '')
{
$msg = $text{'MSG_ERROR_EMAIL'};
}
elsif (!$config{'trans_use_mailboxes'} && $config{'trans_smtp'} eq '')
{
$msg = $text{'MSG_ERROR_SMTP'};
}
else
{
$ok = 1
}
# if there was a problem, exit displaying
# a message
&trans_check_config_exit ($msg) if (!$ok);
# create config directory for the current user
make_path (
"/$config{'trans_working_path'}/.translator/$remote_user/archives",
"/$config{'trans_working_path'}/.translator/$remote_user/monitoring/usermin",
"/$config{'trans_working_path'}/.translator/$remote_user/monitoring/webmin",
"/$config{'trans_working_path'}/.translator/$remote_user/monitoring/usermin/fingerprints",
"/$config{'trans_working_path'}/.translator/$remote_user/monitoring/webmin/fingerprints",
{
'chmod' => 0700
}
);
# modify permissions
chmod (0700, "/$config{'trans_working_path'}/.translator");
}
# trans_trim_config ()
# IN: -
# OUT: -
#
# remove trailing and pending spaces from every module's
# config variable
#
sub trans_trim_config ()
{
while (my ($k, $dum) = each (%config))
{
$config{$k} =~ s/^\s+|\s+$//g;
}
}
# trans_check_config_exit ( $ )
# IN: Message to display
# OUT: -
#
# print a message and exit
#
sub trans_check_config_exit ( $ )
{
my $msg = shift;
print qq(<p>$msg</p>) if ($msg);
print qq(<hr>);
&footer("/", $text{'index'});
exit (1);
}
# trans_get_language_reference ( $ @ % )
# IN: 1 if there is reference language,
# array of files properties,
# reference on the files hash table
# OUT: -
#
# return the reference language
# -> arg 2 could be modified
#
sub trans_get_language_reference ( $ @ % )
{
#FIXME useful ?
## my ($have_ref, @rows, $p1) = @_;
## my (%file_ref) = %$p1;
##
## return if ($have_ref == 1);
##
## foreach my $item (@rows)
## {
## my %row = %$item;
##
## if ($row{'language'} eq $ref_lang)
## {
## $row{'reference'} = 1;
## %$item = %row;
## %file_ref = \$row{'file'};
##
## return;
## }
## }
}
# trans_get_diff_removed ( $ $ % % )
# IN: reference language,
# translation language,
# reference on the reference file,
# reference on the translation file
# OUT: number of items present in the translation
# file and removed in the reference file
#
# return the number of removed items
#
sub trans_get_diff_removed ( $ $ \% \% )
{
my ($ref, $lang, $p1, $p2) = @_;
my (%file_ref) = %$p1;
my (%file) = %$p2;
my $removed = 0;
return 0 if ($ref eq $lang);
while (my ($k, $dum) = each (%file))
{
++$removed if (!exists ($file_ref{$k}));
}
return $removed;
}
# trans_get_diff_new ( $ $ % % )
# IN: reference language,
# translation language,
# reference on the reference file,
# reference on the translation file
# OUT: number of items present in reference file and
# not in the translation file
#
# return the number of new items
#
sub trans_get_diff_new ( $ $ \% \% )
{
my ($ref, $lang, $p1, $p2) = @_;
my (%file_ref) = %$p1;
my (%file) = %$p2;
my $new = 0;
return 0 if ($ref eq $lang);
while (my ($kref, $dum) = each (%file_ref))
{
++$new if (!exists ($file{$kref}) || $file{$kref} eq '')
}
return $new;
}
# trans_get_hash_diff_new ( $ $ % % )
# IN: reference language,
# translation language,
# reference on the reference file,
# reference on the translation file
# OUT: hash table of items present in reference file and
# not in the translation file
#
# return a hash of the new items
#
sub trans_get_hash_diff_new ( $ $ \% \% )
{
my ($ref, $lang, $p1, $p2) = @_;
my (%file_ref) = %$p1;
my (%file) = %$p2;
my %new = ();
return 0 if ($ref eq $lang);
while (my ($k, $v) = each (%file_ref))
{
$new{$k} = $v if (!exists ($file{$k}) || $file{$k} eq '');
}
return %new;
}
# trans_get_hash_diff_removed ( $ $ % % )
# IN: reference language,
# translation language,
# reference on the reference file,
# reference on the translation file
# OUT: hash table of items present in reference file and
# not in the translation file
#
# return a hash of the removed items
#
sub trans_get_hash_diff_removed ( $ $ \% \% )
{
my ($ref, $lang, $p1, $p2) = @_;
my (%file_ref) = %$p1;
my (%file) = %$p2;
my %removed = ();
return 0 if ($ref eq $lang);
while (my ($k, $v) = each (%file))
{
$removed{$k} = $v if (!exists ($file_ref{$k}));
}
return %removed;
}
# trans_get_items ( $ )
# IN: path to the translation file
# OUT: a hash of key/value from the given translation file
#
# return a hash of key/value
#
sub trans_get_items ( $ )
{
my $path = shift;
my %file = ();
&trans_char2ent ($path, 'work');
open (FH, '<', $path);
foreach my $line (<FH>)
{
next if ($line =~ /^\s*#/);
if ($line =~ /=/)
{
my ($name, $value) = split (/=/, $line, 2);
$name =~ s/^\s+|\s+$//g;
$value =~ s/^\s+|\s+$//g;
next if ($name eq '' || $value eq '');
$file{"$name"} = $value;
}
elsif ($name ne '')
{
$file{"$name"} .= $line;
}
}
close (FH);
&trans_char2ent ($path, 'html');
return %file;
}
# trans_get_items_static ( $ )
# IN: path to the translation file
# OUT: a array of hash key/value from the given translation file
#
# same as trans_get_items() function, but return a array instead of a hash,
# to enable caller to keep the same order as in the origin file.
# each item of the array is a hash with key/value. the array order is the
# same as the file's lines order.
#
sub trans_get_items_static ( $ )
{
my $path = shift;
my $basen = basename ($path);
my @file = ();
my $count = 0;
&trans_char2ent ($path, 'work');
open (FH, '<', $path);
foreach my $line (<FH>)
{
next if ($line =~ /^\s*#/);
if ($line =~ /=/)
{
my %item = ();
my ($name, $value) = split (/=/, $line, 2);
$name =~ s/^\s+|\s+$//g;
$value =~ s/^\s+|\s+$//g;
next if ($name eq '' || $value eq '');
$item{$name} = $value;
$file[$count++] = \%item;
}
}
close (FH);
&trans_char2ent ($path, 'html');
return @file;
}
# trans_get_string_from_size ( $ )
# IN: a size
# OUT: the same size, but with unit
#
# return a string with the size within
#
sub trans_get_string_from_size ( $ )
{
my $size = shift;
my $ret = '';
if ($size < 1024)
{
$ret = "$size bytes";
}
elsif ($size < 1024 * 1024)
{
$ret = sprintf "%.2f KB", $size / 1024, $size;
}
else
{
$ret = sprintf "%.2f MB", $size / (1024 * 1024), $size;
}
return $ret
}
sub trans_get_cache_path ($ $ $ $ $)
{
my ($type, $lang, $path, $app, $basic) = @_;
my $wu = &trans_get_current_app ();
return
"$config{'trans_working_path'}/.translator/".
"$remote_user/monitoring/$wu/fingerprints/${app}".(($basic)?'_lang':'').
"-${type}_${lang}";
}
# trans_build_cache ($ $ $ $ $ )
# IN: type ('interface' or 'config')
# language
# path to the file to cache
# module's name
# if we must take the root lang directory of webmin
# OUT: -
#
# build a cache file with md5 sums of each value.
#
sub trans_build_cache ( $ $ $ $ $ $ )
{
my ($type, $lang, $path, $app, $basic, $notexists) = @_;
my $f = &trans_get_cache_path ($type, $lang, $path, $app, $basic);
return if ($notexists && -f $f);
open (my $fh, '>', $f);
my %hash = &trans_get_items ($path);
while (my ($k, $v) = each (%hash))
{
print $fh "$k=".(&md5_hex($v))."|#|$v\n";
}
close ($fh);
}
# trans_get_updated ( $ $ $ $ )
# IN: type ('interface' or 'config')
# language
# module's name
# use root webmin 'lang/' directory or not
# OUT: number of modified items in the reference language
#
# return the number of modified items
#
sub trans_get_updated ( $ $ $ $ )
{
my ($type, $lang, $app, $basic) = @_;
my $updated = 0;
my %hash_lang = ();
my $hash_md5 = ();
my $path_lang = '';
my $wu = &trans_get_current_app ();
my $path_md5 =
"/$config{'trans_working_path'}/.translator/$remote_user/monitoring/$wu/" .
"fingerprints/${app}" . (($basic) ? '_lang' : '') . "-${type}_${lang}";
return 0 if (! -f $path_md5);
if ($type eq 'interface')
{
$path_lang = ($basic) ?
"$root_directory/lang/$lang" : &trans_get_path ($app, "lang/$lang");
}
else
{
$path_lang = &trans_get_path ($app, "config.info.$ref");
$path_lang = &trans_get_path ($app, 'config.info') if (! -f $path_lang);
}
%hash_lang = &trans_get_items ($path_lang);
%hash_md5 = &trans_get_items ($path_md5);
foreach my $key (keys %hash_md5)
{
my $md5_old = '';
my $md5_new = '';
next if ($hash_lang{$key} eq '');
$md5_new = &md5_hex ($hash_lang{$key});
$md5_old = (split (/\|#\|/, $hash_md5{$key}))[0];
$updated++ if ($md5_new ne $md5_old);
}
return $updated;
}
#
# trans_get_array_updated ( $ $ $ )
# IN: type ('interface' or 'config')
# language
# module's name
# OUT: array with hash of modified items in the reference language
#
# same as trans_get_updated() function, but return a array instead of a hash.
# each item of the array is a hash with:
# key (translation key)
# old (old value)
# new (new value)
#
sub trans_get_array_updated ( $ $ $ )
{
my ($type, $lang, $app) = @_;
my @updated = ();
my %hash_lang = ();
my $hash_md5 = ();
my $path_lang = '';
my $wu = &trans_get_current_app ();
my $path_md5 =
"/$config{'trans_working_path'}/.translator/$remote_user/monitoring/$wu/fingerprints/${app}-${type}_${lang}";
return 0 if (! -f $path_md5);
if ($type eq 'interface')
{
$path_lang = &trans_get_path ($app,"lang/$lang");
}
else
{
$path_lang = &trans_get_path ($app, "config.info.$ref");
$path_lang = &trans_get_path ($app, 'config.info') if (! -f $path_lang);
}
%hash_lang = &trans_get_items ($path_lang);
%hash_md5 = &trans_get_items ($path_md5);
foreach my $key (keys %hash_md5)
{
next if ($hash_lang{$key} eq '');
my $md5_old = '';
my $md5_new = '';
my $string_old = '';
$md5_new = &md5_hex ($hash_lang{$key});
($md5_old, $string_old) = (split (/\|#\|/, $hash_md5{$key}))[0,1];
if ($md5_new ne $md5_old)
{
my %hash = (
'key' => $key,
'old' => $string_old,
'new' => $hash_lang{$key}
);
push @updated, \%hash;
}
}
return @updated;
}
# trans_get_item_updated ( \@ $)
# IN: array of hash
# key
# OUT: a hash with key, old and new value
#
# return a hash corresponding to the given key
#
sub trans_get_item_updated ( \@ $)
{
my ($array, $item) = @_;
foreach my $h (@$array)
{
my %hash = %$h;
return %hash if ($hash{'key'} eq $item);
}
}
# trans_get_unused ( $ $ $ )
# IN: type ('interface' or 'config')
# language
# module's name
# OUT: a hash with unused items
#
# return a hash table with items in the reference translation file that are not
# used in the module files
#
sub trans_get_unused ( $ $ $ )
{
my ($type, $lang, $app) = @_;
my %unused = ();
my %hash = ();
my $path_lang = '';
my $path_search = '';
my $grep_string = '';
if ($type eq 'interface')
{
$path_lang = &trans_get_path ($app, "lang/$lang");
}
else
{
$path_lang = &trans_get_path ($app, "config.info.$lang");
$path_lang = &trans_get_path ($app, "config.info") if (! -f $path_lang);
}
$path_search = (&trans_get_path ()) . "/$app/";
$grep_string = "$path_search/* -r";
%hash = &trans_get_items ($path_lang);
foreach $key (keys %hash)
{
next if ($type eq 'config' && $key =~ /^line\d*/);
my @array = split (/\n/, `$all_path{'grep'} "$key" $grep_string`);
if (!grep (/[\'|\"]$key[\'|\"]/, @array))
{
$unused{$key} = $hash{$key};
if ($type eq 'interface')
{
delete $unused{$key}
if (grep (/text\b.*[\(|\{][\'|\"]*$key[\'|\"]*/, @array));
}
else
{
delete $unused{$key}
if (grep (/config\b.*\{[\'|\"]*$key[\'|\"]*\}/, @array));
}
}
}
return %unused;
}
# trans_get_existing_translations ( \@ )
# IN: module names in a array
# OUT: a array with translations name
#
# return a array with strings of translations for a given
# module
#
sub trans_get_existing_translations ( \@ )
{
my $a = shift;
my @app = @$a;
my @array = ();
my %hash = ();
# list all languages for all modules
if (!@app)
{
foreach my $m (grep {&check_os_support($_)} &get_all_module_infos ())
{
my $dir = $m->{'dir'};
opendir (DH, &trans_get_path ($dir, 'lang/'));
foreach my $item (readdir (DH))
{
$hash{$item} = '' if &trans_is_language ($item, 1);
}
closedir (DH);
}
}
# just list all languages for a given module
else
{
foreach my $mod (@app)
{
opendir (DH, &trans_get_path ($mod, 'lang/'));
foreach my $item (readdir (DH))
{
$hash{$item} = '' if &trans_is_language ($item, 1);
}
closedir (DH);
}
}
delete $hash{$ref_lang};
@array = keys %hash;
return sort @array;
}
# get_languages_infos ( $ )
# IN: language
# OUT: language properties hashref + 'current_compat' key if the given language
# is compatible with the current webmin/usermin language
#
# get infos for a given language
#
sub get_languages_infos ( $ )
{
my $lang = shift;
my @langs = &list_languages();
my %data;
my $ret;
foreach my $l (@langs)
{
$data{$l->{'lang'}} = $l;
}
if (exists ($data{$lang}))
{
$ret = $data{$lang};
$ret->{'current_compat'} = (
$ret->{'charset'} eq $data{$current_lang}{'charset'}
);
}
return $ret;
}
# trans_create_translation ( $ $ )
# IN: language
# module's name
# OUT:
# 0: ok
# 1: this language already exists
# 2: bad input
#
# create all files and item for a new translation
#
sub trans_create_translation ( $ $ )
{
my ($lang, $app) = @_;
my $path = (&trans_get_path())."/$app";
my @content = ();
$lang =~ s/^\s+|\s+$//g;
# bad
return 2 if (!&trans_is_language ($lang));
# already exist
return 1 if (grep /^$lang$/, &trans_get_existing_translations ([$app]));
if ($config{'trans_webmin'})
{
# config.info
if (-f "$path/config.info" && ! -f "$path/config.info.$lang")
{
open (FH, '>', "$path/config.info.$lang") && close (FH);
}
# lang
if (! -f "$path/lang/$lang")
{
open (FH, '>', "$path/lang/$lang") && close (FH);
}
# lang - Webmin module exception (2 languages files)
if ($path =~ m|/webmin$| && ! -f "$root_directory/lang/$lang")
{
open (FH, '>', "$root_directory/lang/$lang") && close (FH)
}
}
else
{
# config.info
if (-f "$path/uconfig.info" && ! -f "$path/uconfig.info.$lang")
{
open (FH, '>', "$path/uconfig.info.$lang") && close (FH);
}
# lang
make_path ("$path/ulang", {'chmod' => 700});
if (! -f "$path/ulang/$lang")
{
open (FH, '>',"$path/ulang/$lang") && close (FH);
}
}
# theme.info (for theme)
if (&trans_is_theme ($app))
{
open (FH, '<', "$path/theme.info");
@content = <FH>;
close (FH);
if (!grep /^desc_$lang=/, @content)
{
open (FH, '>>', "$path/theme.info");
print FH "desc_$lang=\n";
close (FH);
}
}
# module.info
elsif (! -f "$path/module.info.$lang")
{
open (FH, '>', "$path/module.info.$lang");
print FH "desc_$lang=\n";
close (FH);
}
return 0;
}
# trans_is_theme ( $ )
# IN: module/theme name
# OUT:
# 0: is a module
# 1: is a theme
#
# check if a given module is a theme
#
sub trans_is_theme ( $ )
{
return (-f &trans_get_path (shift, 'theme.info'));
}
# trans_is_language ( $ $ )
# IN: language, 1 to not check for specific encoding
# OUT:
# 0: not valid
# 1: valid
#
# check if a given language code is valid
#
sub trans_is_language ( $ $ )
{
my ($lang, $nocheck) = @_;
my $len = length ($lang);
return 0 if (!$nocheck && $lang ne $ref_lang &&
(
$current_lang =~ /\.(.+)$/ && $lang !~ /\.$1$/ ||
$lang =~ /\.(.+)$/ && $current_lang !~ /\.$1$/
)
);
# FIXME
# bad bad check... must be optimized!
return !(
$len < 2 ||
($len > 2 && $lang !~ /[_\-\.]/) ||
($len > 5 && $lang !~ /\./) ||
$lang =~ /^\./ ||
$lang eq 'si' ||
$lang =~ /[\*,\|<>\{\}]/
);
}
# trans_header ()
# IN: - Page title
# - Specific help file (optional)
#
# Display header.
#
sub trans_header ( $ $ $ )
{
my ($title, $app, $lang, $help_file) = @_;
my $subtitle = '';
my $mainApp = ($config{'trans_webmin'}) ?
$text{'FORM_TITLE_W'} : $text{'FORM_TITLE_U'};
$lang = ($lang) ? "'$lang'":'';
$help_file ||= basename($scriptname, '.cgi');
&header (sprintf($text{'FORM_TITLE'}, $mainApp), undef, $help_file, 1, 0, 0,
qq(<button class="btn btn-primary" onclick="translate_console_open ($lang)"><span style="white-space:nowrap">$text{'TRANSLATE_CONSOLE_LINK'}</span>));
&trans_header_extra ();
if ($title =~ /^(.*)\|(.*)$/)
{
($title, $subtitle) = ($1, $2);
}
if ($app)
{
printf (qq(<div style="margin-top:5px">$text{'MODULE_TRANSLATION'} <div id="header-module">$app%s</div></div>), ($subtitle)?qq( (<span>$subtitle</span>)):'');
}
print qq(<h1>$title</h1>) if ($title);
}
sub trans_header_extra ()
{
my $module_version = $module_info{'version'};
print qq(<link rel="stylesheet" type="text/css" href="css/translator.css?$module_version"/>);
print qq(<script src="js/translator.js?$module_version"></script>);
print qq(<div id="alert-msg"></div>);
}
sub trans_display_msg ( $ $ $ )
{
my ($msg, $msg_type, $icon) = @_;
if ($icon)
{
$msg = qq(<i class='fa fa-fw fa-$icon'></i> ).$msg;
}
elsif ($msg_type eq 'success')
{
$msg = qq(<i class='fa fa-fw fa-info-circle'></i> ).$msg;
}
elsif ($msg_type eq 'danger')
{
$msg = qq(<i class='fa fa-fw fa-exclamation-circle'></i> $text{'WARNING'} : ).$msg;
}
elsif ($msg_type eq 'info')
{
$msg = qq(<i class='fa fa-fw fa-paperclip'></i> ).$msg;
}
$msg =~ s/"/\\"/g;
if ($msg)
{
print qq(
<script>
setTimeout(function(){displayMsg("$msg", "$msg_type")}, 250);
</script>
);
}
}
# trans_footer ()
# IN: - url to redirect to
# - text for return link button
# - msg success
# - msg error
#
# Display sucess or error mesage.
#
sub trans_footer ( $ $ $ $ $ $)
{
my ($url, $link_label, $success, $error, $info, $from_main_page) = @_;
my $msg;
if ($msg = $error)
{
$msg_type = 'danger';
}
elsif ($msg = $success)
{
$msg_type = 'success';
}
elsif ($msg = $info)
{
$msg_type = 'info';
}
print qq(<hr/>) if (!$from_main_page);
&trans_display_msg ($msg, $msg_type) if ($msg);
&footer ($url, $link_label);
}
# trans_archive_create ( $ $ \@ )
# IN: file name
# language ('' for all languages)
# modules names in a array
# OUT: -
#
# create a archive for a given or multiple modules translations
#
sub trans_archive_create ( $ $ \@)
{
my ($filename, $lang, $array) = @_;
my @app = @$array;
my $path = '';
my $path_lang = '';
my $path_module = '';
my $path_help = '';
my $path_config = '';
my $root = &trans_get_path ();
foreach my $mod (@app)
{
$path = $root;
$tmp_path_lang = '';
$tmp_path_module = '';
$tmp_path_help = '';
$tmp_path_config = '';