diff options
-rwxr-xr-x | tools/langtool.pl | 207 |
1 files changed, 207 insertions, 0 deletions
diff --git a/tools/langtool.pl b/tools/langtool.pl new file mode 100755 index 0000000000..79fedf5a0e --- /dev/null +++ b/tools/langtool.pl | |||
@@ -0,0 +1,207 @@ | |||
1 | #!/usr/bin/perl | ||
2 | # __________ __ ___. | ||
3 | # Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | # Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | # Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | # Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | # \/ \/ \/ \/ \/ | ||
8 | # $Id$ | ||
9 | # | ||
10 | # This program is free software; you can redistribute it and/or | ||
11 | # modify it under the terms of the GNU General Public License | ||
12 | # as published by the Free Software Foundation; either version 2 | ||
13 | # of the License, or (at your option) any later version. | ||
14 | # | ||
15 | # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
16 | # KIND, either express or implied. | ||
17 | |||
18 | sub usage { | ||
19 | print <<MOO | ||
20 | Usage langtool --options langfile | ||
21 | |||
22 | For all actions, the modified langfile will be output on stdout. When doing | ||
23 | stuff to english.lang, you should almost always apply the same change to all | ||
24 | other languages to avoid bothering translators. | ||
25 | |||
26 | --deprecate --id ID_1,ID_2 --id ID_3 langfile | ||
27 | |||
28 | Deprecate a number of ids. | ||
29 | Example: langtool --id LANG_LEFT,LANG_RIGHT --LANG_BOTTOM english.lang | ||
30 | |||
31 | --changesource --id ID --target target --to string langfile | ||
32 | |||
33 | Change the source text of a specific string specified by id and target. Use | ||
34 | this on all langfiles if you're changing a source in english.lang that | ||
35 | doesn't need attention from translators (case changes etc.). | ||
36 | Example: | ||
37 | langtool --changesource --id LANG_OK --target e200 --to "OK" english.lang | ||
38 | |||
39 | --changeid --from LANG_LFET --to LANG_LEFT langfile | ||
40 | |||
41 | Change the name of an ID. THIS WILL BREAK BACKWARDS COMPATIBILITY. Use with | ||
42 | extreme caution. | ||
43 | Example: langtool --changeid --from LANG_OK --to LANG_YES english.lang | ||
44 | |||
45 | --changedesc --to string --id LANG_LEFT langfile | ||
46 | |||
47 | Change the desc for an ID. | ||
48 | Example: langtool --changedesc --to "New desc" --id LANG_OK english.lang | ||
49 | |||
50 | --changetarget --from target --to target --id ID1 langfile | ||
51 | |||
52 | Change the target for the specified id from one value to another | ||
53 | Example: | ||
54 | langtool --changetarget --from e200 --to e200,c200 --id LANG_ON dansk.lang | ||
55 | MOO | ||
56 | } | ||
57 | |||
58 | use Getopt::Long; | ||
59 | use strict; | ||
60 | |||
61 | # Actions | ||
62 | my $deprecate = ''; | ||
63 | my $changesource = ''; | ||
64 | my $changeid = ''; | ||
65 | my $changetarget = ''; | ||
66 | my $changedesc = ''; | ||
67 | my $help = ''; | ||
68 | # Parameters | ||
69 | my @ids = (); | ||
70 | my $from = ''; | ||
71 | my $to = ''; | ||
72 | my $s_target = ''; | ||
73 | |||
74 | GetOptions( | ||
75 | 'deprecate' => \$deprecate, | ||
76 | 'changesource' => \$changesource, | ||
77 | 'changeid' => \$changeid, | ||
78 | 'changetarget' => \$changetarget, | ||
79 | 'changedesc' => \$changedesc, | ||
80 | 'help' => \$help, | ||
81 | |||
82 | 'ids=s' => \@ids, | ||
83 | 'from=s' => \$from, | ||
84 | 'to=s' => \$to, | ||
85 | 'target=s' => \$s_target, | ||
86 | ); | ||
87 | # Allow comma-separated ids as well as several --id switches | ||
88 | @ids = split(/,/,join(',',@ids)); | ||
89 | my $numids = @ids; | ||
90 | my $numfiles = @ARGV; | ||
91 | |||
92 | # Show help if necessary | ||
93 | if ( | ||
94 | $help | ||
95 | or # More than one option set | ||
96 | ($deprecate + $changesource + $changeid + $changetarget + $changedesc) != 1 | ||
97 | or # Do changeid, but either from or to is empty | ||
98 | ($changeid and ($from eq "" or $to eq "")) | ||
99 | or # Do changedesc, but to isn't set | ||
100 | ($changedesc and $to eq "") | ||
101 | or # Do changetarget, but | ||
102 | ($changetarget and ($from eq "" or $to eq "")) | ||
103 | or # Do deprecate, but no ids set | ||
104 | ($deprecate and $numids < 1) | ||
105 | or # Do changesource, but either target or to not set | ||
106 | ($changesource and ($s_target eq "" or $to eq "")) | ||
107 | ) { | ||
108 | usage(); | ||
109 | exit(1); | ||
110 | } | ||
111 | |||
112 | # Check that all supplied files exist before doing anything | ||
113 | foreach my $file (@ARGV) { | ||
114 | if (not -f $file) { | ||
115 | printf("File doesn't exist: %s\n", $file); | ||
116 | exit(2); | ||
117 | } | ||
118 | } | ||
119 | |||
120 | if ($changesource and not $to =~ /none|deprecated/) { | ||
121 | $to = sprintf('"%s"', $to); | ||
122 | } | ||
123 | |||
124 | open(LANGFILE, $ARGV[0]); | ||
125 | my $id = ""; | ||
126 | my $desc = ""; | ||
127 | my $location = ""; | ||
128 | my $target = ""; | ||
129 | my $string = ""; | ||
130 | my $open = 0; | ||
131 | |||
132 | for (<LANGFILE>) { | ||
133 | my $line = $_; | ||
134 | |||
135 | if ($line =~ /^\s*<(\/?)([^>]+)>\s*$/) { | ||
136 | my $tag = $2; | ||
137 | $open = $1 eq "/" ? 0 : 1; | ||
138 | if ($open) { | ||
139 | $location = $tag; | ||
140 | ($target, $string) = ("", ""); | ||
141 | } | ||
142 | if ($open and $tag eq "phrase") { | ||
143 | $id = ""; | ||
144 | } | ||
145 | if (not $open) { | ||
146 | $location = ""; | ||
147 | } | ||
148 | } | ||
149 | elsif ($line =~ /^\s*([^:]*?)\s*:\s*(.*?)\s*$/) { | ||
150 | my ($key, $val) = ($1, $2); | ||
151 | if ($location =~ /source|dest|voice/) { | ||
152 | ($target, $string) = ($key, $val); | ||
153 | } | ||
154 | if ($key eq "id") { | ||
155 | $id = $val; | ||
156 | } | ||
157 | elsif ($key eq "desc") { | ||
158 | $desc = $val; | ||
159 | } | ||
160 | } | ||
161 | |||
162 | if ($deprecate) { | ||
163 | if ($id ne "" and grep(/$id/, @ids)) { | ||
164 | # Set desc | ||
165 | $line =~ s/\s*desc:.*/ desc: deprecated/; | ||
166 | # Set user | ||
167 | $line =~ s/\s*user:.*/ user:/; | ||
168 | # Print an empty target line after opening tag (target isn't set) | ||
169 | if ($location =~ /source|dest|voice/ and $target eq "") { | ||
170 | $line .= " *: none\n"; | ||
171 | } | ||
172 | # Do not print target: string lines | ||
173 | elsif ($location =~ /source|dest|voice/ and $target ne "") { | ||
174 | $line = ""; | ||
175 | } | ||
176 | } | ||
177 | print($line); | ||
178 | } | ||
179 | elsif ($changetarget) { | ||
180 | # Change target if set and it's the same as $from | ||
181 | if ($id ne "" and grep(/$id/, @ids) and $location =~ /source|dest|voice/ and $target eq $from) { | ||
182 | $line =~ s/$from/$to/; | ||
183 | } | ||
184 | print($line); | ||
185 | } | ||
186 | elsif ($changesource) { | ||
187 | # Change string if $target is set and matches $s_target | ||
188 | if ($id ne "" and grep(/$id/, @ids) and $target eq $s_target and $location eq "source") { | ||
189 | $line =~ s/$string/$to/; | ||
190 | } | ||
191 | print($line); | ||
192 | } | ||
193 | elsif ($changedesc) { | ||
194 | # Simply change the desc line if the id matches | ||
195 | if ($id ne "" and grep(/$id/, @ids)) { | ||
196 | $line =~ s/\s*desc:.*/ desc: $to/; | ||
197 | } | ||
198 | print($line); | ||
199 | } | ||
200 | elsif ($changeid) { | ||
201 | $line =~ s/^\s*id:\s*$from.*$/ id: $to/; | ||
202 | print($line); | ||
203 | } | ||
204 | else { | ||
205 | print("wut wut\n"); | ||
206 | } | ||
207 | } | ||