source: trunk/tools/missing_keys.pl @ 5286

Last change on this file since 5286 was 5286, checked in by plg, 14 years ago

new script to detect missing language keys

  • Property svn:eol-style set to LF
File size: 3.4 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use File::Find;
7
8our %used_keys = ();
9our %registered_keys = ();
10
11my $piwigo_dir = '/home/pierrick/public_html/piwigo/dev/trunk';
12my $type = $ARGV[0]; # common, admin, install, upgrade
13
14find(\&used_keys, $piwigo_dir);
15load_registered_keys($type);
16
17foreach my $key (sort keys %used_keys) {
18    # print "{".$key."}", ' is used', "\n";
19
20    if (not defined $registered_keys{$key}) {
21        print "{".$key."}", ' is missing', "\n";
22        # print '$lang[\''.$key.'\'] = \''.$key.'\';', "\n";
23    }
24}
25
26# foreach my $key (sort keys %registered_keys) {
27#     if (not defined $used_keys{$key}) {
28#         print "{".$key."}", ' is not used anywhere', "\n";
29#     }
30# }
31
32sub used_keys {
33    if ($File::Find::name !~ m/(tpl|php)$/) {
34        return 0;
35    }
36
37    if ($File::Find::name =~ m{/(plugins|language)/}) {
38        return 0;
39    }
40
41    if ('upgrade' eq $type) {
42        if ($File::Find::name !~ m{upgrade\.(tpl|php)$}) {
43            return 0;
44        }
45    }
46
47    if ('install' eq $type) {
48        if ($File::Find::name =~ m{upgrade\.(tpl|php)$}) {
49            return 0;
50        }
51        if ($File::Find::name !~ m{/install(\.tpl|\.php|/)}) {
52            return 0;
53        }
54    }
55
56    if ('admin' eq $type) {
57        if ($File::Find::name =~ m{upgrade\.(tpl|php)$}) {
58            return 0;
59        }
60        if ($File::Find::name =~ m{/install(\.tpl|\.php|/)}) {
61            return 0;
62        }
63
64        my $is_admin = 0;
65
66        if ($File::Find::name =~ m{themes/default/template/mail}) {
67            $is_admin = 1;
68        }
69        if ($File::Find::name =~ m{/admin/}) {
70            $is_admin = 1;
71        }
72
73        if (not $is_admin) {
74            return 0;
75        }
76    }
77
78    if ('common' eq $type) {
79        if ($File::Find::name =~ m{upgrade\.(tpl|php)$}) {
80            return 0;
81        }
82        if ($File::Find::name =~ m{/install(\.tpl|\.php|/)}) {
83            return 0;
84        }
85        if ($File::Find::name =~ m{/admin/} or $File::Find::name =~ m{themes/default/template/mail}) {
86            return 0;
87        }
88    }
89
90    if (-f) {
91        open(my $fhi, '<', $File::Find::name);
92        while (<$fhi>) {
93            if ($File::Find::name =~ m/tpl$/) {
94                while (m/\{(['"])(.+?)\1\|\@translate/g) {
95                    $used_keys{$2}++;
96                }
97            }
98
99            if ($File::Find::name =~ m/php$/) {
100                while (m/l10n \s* \( \s* (['"]) (.+?) \1 \s* \)/xg) {
101                    $used_keys{$2}++;
102                }
103
104                while (m/l10n_args \s* \( \s* (['"]) (.+?) \1 \s* ,/xg) {
105                    $used_keys{$2}++;
106                }
107
108                while (m/l10n_dec \s* \( \s* (['"]) (.+?) \1 \s* ,\s* (['"]) (.+?) \3 \s* ,/xg) {
109                    $used_keys{$2}++;
110                    $used_keys{$4}++;
111                }
112            }
113        }
114    }
115}
116
117sub load_registered_keys {
118    my ($type) = @_;
119
120    my %files_for_type = (
121        common  => [qw/common/],
122        admin   => [qw/common admin/],
123        install => [qw/common admin install/],
124        upgrade => [qw/common admin install upgrade/],
125    );
126
127    foreach my $file_code (@{$files_for_type{$type}}) {
128        my $filepath = $piwigo_dir.'/language/en_UK/'.$file_code.'.lang.php';
129
130        open(my $fhi, '<', $filepath);
131        while (<$fhi>) {
132            if (m/\$lang\[ \s* (['"]) (.+?) \1 \s* \]/x) {
133                $registered_keys{$2}++;
134            }
135        }
136    }
137}
Note: See TracBrowser for help on using the repository browser.