summaryrefslogtreecommitdiffstats
path: root/tools/updateLang.pl
blob: 6e7feafd679127c1e6429c6a73f8fb9b24e783e2 (plain)
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
#!/usr/bin/perl

# Program :  COPS localization string generator
# Version :  0.0.1
#
# Author  :  Sébastien Lucas
# License :  GPLv2
#

use strict;

our @strings = ();
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$/);

    my $file = "../" . $_;
    debug ("text file: " . $_ . "\n");
    my $content = loadFile ($file);

    while ($content =~ /localize\s*\("([\w\.]*?)"\)/igs) {
        $allstrings{$1} = "";
        debug (" * $1 \n");
    }

    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

handleLanguageFile ("Localization_en.json");

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$/);
    next if (/en\.json$/);

    handleLanguageFile ($_);
}
closedir $dirhandle;

sub handleLanguageFile {
    my ($file) = @_;
    (my $lang = $file) =~ s/Localization_(\w\w)\.json/$1/;
    my $file = "../lang/" . $file;
    my $total = 0;
    my $translated = 0;

    debug ("language file: $file / $lang \n");

    my $content = loadFile ($file);

    while ($content =~ /"\s*(.*?)"\:\s*"(.*?)",/igs) {
        my $key = $1;
        my $value = $2;
        next if ($key =~ /^##TODO##/);
        if ($lang eq "en" && $key =~ /^languages\.\w{3}$/) {
            push (@strings, $key);
        }
        $values{$lang}{$key} = $value;
        #debug (" * $1 \n");
    }

    open OUTPUT, ">$file";

    print OUTPUT "{\n";
    foreach my $name (@strings) {
        $total++ if ($name !~ /^languages\.\w{3}$/);
        if (not exists ($values{$lang}{$name})) {
            print OUTPUT "    \"##TODO##$name\": \"$values{en}{$name}\",\n";
        } else {
            $translated++  if ($name !~ /^languages\.\w{3}$/);
            print OUTPUT "    \"$name\": \"$values{$lang}{$name}\",\n";
        }
    }
    my $percentage = ($translated * 100) / $total;
    debug ("  $translated / $total ($percentage %) \n");
    print OUTPUT "    \"DO_NOT_TRANSLATE\": \"end\"\n";
    print OUTPUT "}\n";

    close OUTPUT;
}

sub loadFile {
    my ($file) = @_;
    my $save = $/;
    $/ = undef;

    open INPUT, "<$file";
    my $content = <INPUT>;
    close INPUT;

    $/ = $save;

    return $content;
}

sub debug {
    #uncomment next line for debug messages
    print @_;
}