2013-02-21 22:01:25 +02:00
|
|
|
|
#!/usr/bin/perl
|
|
|
|
|
|
2015-11-03 15:38:21 +02:00
|
|
|
|
# Program : COPS localization string generator
|
2013-02-21 22:01:25 +02:00
|
|
|
|
# Version : 0.0.1
|
|
|
|
|
#
|
|
|
|
|
# Author : S<>bastien Lucas
|
|
|
|
|
# License : GPLv2
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
|
|
2013-09-15 10:20:02 +03:00
|
|
|
|
our @strings = ();
|
2013-02-21 22:01:25 +02:00
|
|
|
|
my %values;
|
|
|
|
|
my %allstrings;
|
|
|
|
|
|
|
|
|
|
# Load php files looking for strings to localize
|
|
|
|
|
|
|
|
|
|
opendir (my($dirhandle), "../") or die ("Directory not found\n");
|
|
|
|
|
for (readdir ($dirhandle)) {
|
|
|
|
|
next if (-d $_ ); # skip directories
|
|
|
|
|
next if (/^[.]/); # skip dot-files
|
|
|
|
|
next if not (/(.+)[.]php$/);
|
2015-11-03 15:38:21 +02:00
|
|
|
|
|
2013-02-21 22:01:25 +02:00
|
|
|
|
my $file = "../" . $_;
|
|
|
|
|
debug ("text file: " . $_ . "\n");
|
|
|
|
|
my $content = loadFile ($file);
|
2015-11-03 15:38:21 +02:00
|
|
|
|
|
2013-02-21 22:01:25 +02:00
|
|
|
|
while ($content =~ /localize\s*\("([\w\.]*?)"\)/igs) {
|
|
|
|
|
$allstrings{$1} = "";
|
|
|
|
|
debug (" * $1 \n");
|
|
|
|
|
}
|
2015-11-03 15:38:21 +02:00
|
|
|
|
|
2013-02-21 22:01:25 +02:00
|
|
|
|
while ($content =~ /localize\s*\("([\w\.]*?)"\s*,/igs) {
|
|
|
|
|
$allstrings{$1 . ".none"} = "";
|
|
|
|
|
$allstrings{$1 . ".one"} = "";
|
|
|
|
|
$allstrings{$1 . ".many"} = "";
|
|
|
|
|
debug (" *** $1 \n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
closedir $dirhandle;
|
|
|
|
|
|
|
|
|
|
@strings = sort (keys (%allstrings));
|
|
|
|
|
|
|
|
|
|
# Load existing json files with strings and values
|
|
|
|
|
|
2013-09-15 10:20:02 +03:00
|
|
|
|
handleLanguageFile ("Localization_en.json");
|
|
|
|
|
|
2013-02-21 22:01:25 +02:00
|
|
|
|
opendir (my($dirhandle), "../lang") or die ("Directory not found\n");
|
|
|
|
|
for (readdir ($dirhandle)) {
|
|
|
|
|
next if (-d $_ ); # skip directories
|
|
|
|
|
next if (/^[.]/); # skip dot-files
|
|
|
|
|
next if not (/(.+)[.]json$/);
|
2013-09-15 10:20:02 +03:00
|
|
|
|
next if (/en\.json$/);
|
2015-11-03 15:38:21 +02:00
|
|
|
|
|
2013-09-15 10:20:02 +03:00
|
|
|
|
handleLanguageFile ($_);
|
|
|
|
|
}
|
|
|
|
|
closedir $dirhandle;
|
|
|
|
|
|
|
|
|
|
sub handleLanguageFile {
|
|
|
|
|
my ($file) = @_;
|
|
|
|
|
(my $lang = $file) =~ s/Localization_(\w\w)\.json/$1/;
|
2015-11-03 15:38:21 +02:00
|
|
|
|
my $file = "../lang/" . $file;
|
2013-09-30 12:53:46 +03:00
|
|
|
|
my $total = 0;
|
|
|
|
|
my $translated = 0;
|
2015-11-03 15:38:21 +02:00
|
|
|
|
|
2013-09-15 10:20:02 +03:00
|
|
|
|
debug ("language file: $file / $lang \n");
|
2015-11-03 15:38:21 +02:00
|
|
|
|
|
2013-02-21 22:01:25 +02:00
|
|
|
|
my $content = loadFile ($file);
|
2015-11-03 15:38:21 +02:00
|
|
|
|
|
2015-11-03 15:37:46 +02:00
|
|
|
|
while ($content =~ /"(.*?)"\:\s*"(.*?)",/igs) {
|
2013-09-15 10:20:02 +03:00
|
|
|
|
my $key = $1;
|
|
|
|
|
my $value = $2;
|
|
|
|
|
next if ($key =~ /^##TODO##/);
|
2013-09-16 21:33:46 +03:00
|
|
|
|
if ($lang eq "en" && $key =~ /^languages\.\w{3}$/) {
|
2013-09-15 10:20:02 +03:00
|
|
|
|
push (@strings, $key);
|
|
|
|
|
}
|
|
|
|
|
$values{$lang}{$key} = $value;
|
2013-02-21 22:01:25 +02:00
|
|
|
|
#debug (" * $1 \n");
|
|
|
|
|
}
|
2015-11-03 15:38:21 +02:00
|
|
|
|
|
2013-09-15 10:20:02 +03:00
|
|
|
|
open OUTPUT, ">$file";
|
2015-11-03 15:38:21 +02:00
|
|
|
|
|
2013-02-21 22:01:25 +02:00
|
|
|
|
print OUTPUT "{\n";
|
|
|
|
|
foreach my $name (@strings) {
|
2013-09-30 12:53:46 +03:00
|
|
|
|
$total++ if ($name !~ /^languages\.\w{3}$/);
|
2013-09-15 10:20:02 +03:00
|
|
|
|
if (not exists ($values{$lang}{$name})) {
|
|
|
|
|
print OUTPUT "\"##TODO##$name\":\"$values{en}{$name}\",\n";
|
|
|
|
|
} else {
|
2013-09-30 12:53:46 +03:00
|
|
|
|
$translated++ if ($name !~ /^languages\.\w{3}$/);
|
2013-09-15 10:20:02 +03:00
|
|
|
|
print OUTPUT "\"$name\":\"$values{$lang}{$name}\",\n";
|
|
|
|
|
}
|
2013-02-21 22:01:25 +02:00
|
|
|
|
}
|
2013-09-30 12:53:46 +03:00
|
|
|
|
my $percentage = ($translated * 100) / $total;
|
|
|
|
|
debug (" $translated / $total ($percentage %) \n");
|
2013-09-16 21:33:46 +03:00
|
|
|
|
print OUTPUT "\"DO_NOT_TRANSLATE\":\"end\"\n";
|
2013-02-21 22:01:25 +02:00
|
|
|
|
print OUTPUT "}\n";
|
2015-11-03 15:38:21 +02:00
|
|
|
|
|
2013-02-21 22:01:25 +02:00
|
|
|
|
close OUTPUT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub loadFile {
|
|
|
|
|
my ($file) = @_;
|
|
|
|
|
my $save = $/;
|
|
|
|
|
$/ = undef;
|
2015-11-03 15:38:21 +02:00
|
|
|
|
|
2013-02-21 22:01:25 +02:00
|
|
|
|
open INPUT, "<$file";
|
|
|
|
|
my $content = <INPUT>;
|
|
|
|
|
close INPUT;
|
|
|
|
|
|
|
|
|
|
$/ = $save;
|
2015-11-03 15:38:21 +02:00
|
|
|
|
|
2013-02-21 22:01:25 +02:00
|
|
|
|
return $content;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub debug {
|
|
|
|
|
#uncomment next line for debug messages
|
|
|
|
|
print @_;
|
|
|
|
|
}
|