Merge the "Replaygain Off" option into the replaygain type; eliminate the "On/Off...
[kugel-rb/myfork.git] / tools / langtool.pl
blob59db1cf8a53bf73b73392c23334062affe9eea1b
1 #!/usr/bin/perl
2 # __________ __ ___.
3 # Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 # Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 # Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 # Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 # \/ \/ \/ \/ \/
8 # $Id$
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.
15 # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 # KIND, either express or implied.
18 sub usage {
19 print <<MOO
20 Usage langtool [--inplace] --options langfile1 [langfile2 ...]
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.
26 --deprecate --id ID_1,ID_2 --id ID_3 langfile
28 Deprecate a number of ids.
29 Example: langtool --id LANG_LEFT,LANG_RIGHT --LANG_BOTTOM english.lang
31 --changesource --id ID --target target --to string langfile
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
39 --changeid --from LANG_LFET --to LANG_LEFT langfile
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
45 --changedesc --to string --id LANG_LEFT langfile
47 Change the desc for an ID.
48 Example: langtool --changedesc --to "New desc" --id LANG_OK english.lang
50 --changetarget --from target --to target --id ID1 langfile
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
56 --inplace
58 Perform file operations in-place, instead of outputting the result to
59 stdout. With this option set, you can specify multiple langfiles for
60 all commands.
61 Example: langtool --deprecate --id LANG_ASK --inplace *.lang
62 MOO
65 use Getopt::Long;
66 use strict;
68 # Actions
69 my $deprecate = '';
70 my $changesource = '';
71 my $changeid = '';
72 my $changetarget = '';
73 my $changedesc = '';
74 my $inplace = '';
75 my $help = '';
76 # Parameters
77 my @ids = ();
78 my $from = '';
79 my $to = '';
80 my $s_target = '';
82 GetOptions(
83 'deprecate' => \$deprecate,
84 'changesource' => \$changesource,
85 'changeid' => \$changeid,
86 'changetarget' => \$changetarget,
87 'changedesc' => \$changedesc,
88 'help' => \$help,
89 'inplace' => \$inplace,
91 'ids=s' => \@ids,
92 'from=s' => \$from,
93 'to=s' => \$to,
94 'target=s' => \$s_target,
96 # Allow comma-separated ids as well as several --id switches
97 @ids = split(/,/,join(',',@ids));
98 my $numids = @ids;
99 my $numfiles = @ARGV;
101 # Show help if necessary
102 if (
103 $help
104 or # More than one option set
105 ($deprecate + $changesource + $changeid + $changetarget + $changedesc) != 1
106 or # Do changeid, but either from or to is empty
107 ($changeid and ($from eq "" or $to eq ""))
108 or # Do changedesc, but to isn't set
109 ($changedesc and $to eq "")
110 or # Do changetarget, but
111 ($changetarget and ($from eq "" or $to eq ""))
112 or # Do deprecate, but no ids set
113 ($deprecate and $numids < 1)
114 or # Do changesource, but either target or to not set
115 ($changesource and ($s_target eq "" or $to eq ""))
116 or # More than one file passed, but inplace isn't set
117 ($numfiles > 1 and not $inplace)
119 usage();
120 exit(1);
123 # Check that all supplied files exist before doing anything
124 foreach my $file (@ARGV) {
125 if (not -f $file) {
126 printf("File doesn't exist: %s\n", $file);
127 exit(2);
131 if ($changesource and not $to =~ /none|deprecated/) {
132 $to = sprintf('"%s"', $to);
135 foreach my $file (@ARGV) {
136 print(STDERR "$file\n");
137 open(LANGFILE, $file) or die(sprintf("Couldn't open file for reading: %s", $file));
138 my $id = "";
139 my $desc = "";
140 my $location = "";
141 my $target = "";
142 my $string = "";
143 my $open = 0;
144 my $output = "";
146 for (<LANGFILE>) {
147 my $line = $_;
149 ### Set up values when a tag starts or ends ###
150 if ($line =~ /^\s*<(\/?)([^>]+)>\s*$/) {
151 my $tag = $2;
152 $open = $1 eq "/" ? 0 : 1;
153 if ($open) {
154 $location = $tag;
155 ($target, $string) = ("", "");
157 if ($open and $tag eq "phrase") {
158 $id = "";
160 if (not $open) {
161 $location = "";
164 ### Set up values when a key: value pair is found ###
165 elsif ($line =~ /^\s*([^:]*?)\s*:\s*(.*?)\s*$/) {
166 my ($key, $val) = ($1, $2);
167 if ($location =~ /source|dest|voice/) {
168 ($target, $string) = ($key, $val);
170 if ($key eq "id") {
171 $id = $val;
173 elsif ($key eq "desc") {
174 $desc = $val;
178 if ($deprecate) {
179 if ($id ne "" and grep(/$id/, @ids)) {
180 # Set desc
181 $line =~ s/\s*desc:.*/ desc: deprecated/;
182 # Set user
183 $line =~ s/\s*user:.*/ user:/;
184 # Print an empty target line after opening tag (target isn't set)
185 if ($location =~ /source|dest|voice/ and $target eq "") {
186 $line .= " *: none\n";
188 # Do not print target: string lines
189 elsif ($location =~ /source|dest|voice/ and $target ne "") {
190 $line = "";
194 elsif ($changetarget) {
195 # Change target if set and it's the same as $from
196 if ($id ne "" and grep(/$id/, @ids) and $location =~ /source|dest|voice/ and $target eq $from) {
197 $line =~ s/\Q$from/$to/;
200 elsif ($changesource) {
201 # Change string if $target is set and matches $s_target
202 if ($id ne "" and grep(/$id/, @ids) and $target eq $s_target and $location eq "source") {
203 $line =~ s/\Q$string/$to/;
206 elsif ($changedesc) {
207 # Simply change the desc line if the id matches
208 if ($id ne "" and grep(/$id/, @ids)) {
209 $line =~ s/\s*desc:.*/ desc: $to/;
212 elsif ($changeid) {
213 $line =~ s/^\s*id:\s*$from.*$/ id: $to/;
215 else {
216 print("This should never happen.\n");
217 exit(3);
219 if ($inplace) {
220 $output .= $line;
222 else {
223 print($line);
226 close(LANGFILE);
227 if ($inplace) {
228 open(LANGFILE, ">", $file) or die(sprintf("Couldn't open file for writing: %s\n", $file));
229 print(LANGFILE $output);
230 close(LANGFILE);