1) Исправления в связи со сменой API MySQL
[openlib.git] / www / tools / updateLang.pl
1 #!/usr/bin/perl
2
3 # Program :  COPS localization string generator 
4 # Version :  0.0.1
5 #
6 # Author  :  Sébastien Lucas
7 # License :  GPLv2
8 #
9
10 use strict;
11
12 my @strings = ();
13 my %values;
14 my %allstrings;
15
16 # Load php files looking for strings to localize
17
18 opendir (my($dirhandle), "../") or die ("Directory not found\n");
19 for (readdir ($dirhandle)) {
20     next if (-d $_ ); # skip directories
21     next if (/^[.]/); # skip dot-files
22     next if not (/(.+)[.]php$/);
23     
24     my $file = "../" . $_;
25     debug ("text file: " . $_ . "\n");
26     my $content = loadFile ($file);
27     
28     while ($content =~ /localize\s*\("([\w\.]*?)"\)/igs) {
29         $allstrings{$1} = "";
30         debug (" * $1 \n");
31     }
32     
33     while ($content =~ /localize\s*\("([\w\.]*?)"\s*,/igs) {
34         $allstrings{$1 . ".none"} = "";
35         $allstrings{$1 . ".one"} = "";
36         $allstrings{$1 . ".many"} = "";
37         debug (" *** $1 \n");
38     }
39 }
40 closedir $dirhandle;
41
42 @strings = sort (keys (%allstrings));
43
44 # Load existing json files with strings and values
45
46 opendir (my($dirhandle), "../lang") or die ("Directory not found\n");
47 for (readdir ($dirhandle)) {
48     next if (-d $_ ); # skip directories
49     next if (/^[.]/); # skip dot-files
50     next if not (/(.+)[.]json$/);
51     
52     my $file = "../lang/" . $_;    
53     (my $lang = $_) =~ s/Localization_(\w\w)\.json/$1/;
54     debug ("language file: $_ / $lang \n");
55     
56     my $content = loadFile ($file);
57     
58     while ($content =~ /"(.*?)"\:"(.*?)",/igs) {
59         #push @strings, $1;
60         $values{$lang}{$1} = $2;
61         #debug (" * $1 \n");
62     }
63     
64     open OUTPUT, ">$file.new";
65     
66     print OUTPUT "{\n";
67     foreach my $name (@strings) {
68         print OUTPUT "\"$name\":\"$values{$lang}{$name}\",\n";
69     }
70     print OUTPUT "\"end\":\"end\"\n";
71     print OUTPUT "}\n";
72     
73     close OUTPUT;
74 }
75 closedir $dirhandle;
76
77
78
79 sub loadFile {
80     my ($file) = @_;
81     my $save = $/;
82     $/ = undef;
83     
84     open INPUT, "<$file";
85     my $content = <INPUT>;
86     close INPUT;
87
88     $/ = $save;
89     
90     return $content;
91 }
92
93 sub debug {
94     #uncomment next line for debug messages
95     print @_;
96 }