From e4cf6311cbaecdd6a46de36575a4efe543273277 Mon Sep 17 00:00:00 2001 From: Jonas Häggqvist Date: Mon, 15 Dec 2008 01:12:22 +0000 Subject: Commit langtool - a small perlscript to do some translation file manipulations. If you're modifying english.lang, stop for a moment to think if you could maybe use this script instead, and apply your change to all langfiles at once to avoid bothering our translators needlessly. Also, check the diffs if you do use this script, because it might not be Bug Free[tm]. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@19442 a1c6a512-1295-4272-9138-f99709370657 --- tools/langtool.pl | 207 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 207 insertions(+) create mode 100755 tools/langtool.pl 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 @@ +#!/usr/bin/perl +# __________ __ ___. +# Open \______ \ ____ ____ | | _\_ |__ _______ ___ +# Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / +# Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < +# Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ +# \/ \/ \/ \/ \/ +# $Id$ +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY +# KIND, either express or implied. + +sub usage { + print < \$deprecate, + 'changesource' => \$changesource, + 'changeid' => \$changeid, + 'changetarget' => \$changetarget, + 'changedesc' => \$changedesc, + 'help' => \$help, + + 'ids=s' => \@ids, + 'from=s' => \$from, + 'to=s' => \$to, + 'target=s' => \$s_target, +); +# Allow comma-separated ids as well as several --id switches +@ids = split(/,/,join(',',@ids)); +my $numids = @ids; +my $numfiles = @ARGV; + +# Show help if necessary +if ( + $help + or # More than one option set + ($deprecate + $changesource + $changeid + $changetarget + $changedesc) != 1 + or # Do changeid, but either from or to is empty + ($changeid and ($from eq "" or $to eq "")) + or # Do changedesc, but to isn't set + ($changedesc and $to eq "") + or # Do changetarget, but + ($changetarget and ($from eq "" or $to eq "")) + or # Do deprecate, but no ids set + ($deprecate and $numids < 1) + or # Do changesource, but either target or to not set + ($changesource and ($s_target eq "" or $to eq "")) + ) { + usage(); + exit(1); +} + +# Check that all supplied files exist before doing anything +foreach my $file (@ARGV) { + if (not -f $file) { + printf("File doesn't exist: %s\n", $file); + exit(2); + } +} + +if ($changesource and not $to =~ /none|deprecated/) { + $to = sprintf('"%s"', $to); +} + +open(LANGFILE, $ARGV[0]); +my $id = ""; +my $desc = ""; +my $location = ""; +my $target = ""; +my $string = ""; +my $open = 0; + +for () { + my $line = $_; + + if ($line =~ /^\s*<(\/?)([^>]+)>\s*$/) { + my $tag = $2; + $open = $1 eq "/" ? 0 : 1; + if ($open) { + $location = $tag; + ($target, $string) = ("", ""); + } + if ($open and $tag eq "phrase") { + $id = ""; + } + if (not $open) { + $location = ""; + } + } + elsif ($line =~ /^\s*([^:]*?)\s*:\s*(.*?)\s*$/) { + my ($key, $val) = ($1, $2); + if ($location =~ /source|dest|voice/) { + ($target, $string) = ($key, $val); + } + if ($key eq "id") { + $id = $val; + } + elsif ($key eq "desc") { + $desc = $val; + } + } + + if ($deprecate) { + if ($id ne "" and grep(/$id/, @ids)) { + # Set desc + $line =~ s/\s*desc:.*/ desc: deprecated/; + # Set user + $line =~ s/\s*user:.*/ user:/; + # Print an empty target line after opening tag (target isn't set) + if ($location =~ /source|dest|voice/ and $target eq "") { + $line .= " *: none\n"; + } + # Do not print target: string lines + elsif ($location =~ /source|dest|voice/ and $target ne "") { + $line = ""; + } + } + print($line); + } + elsif ($changetarget) { + # Change target if set and it's the same as $from + if ($id ne "" and grep(/$id/, @ids) and $location =~ /source|dest|voice/ and $target eq $from) { + $line =~ s/$from/$to/; + } + print($line); + } + elsif ($changesource) { + # Change string if $target is set and matches $s_target + if ($id ne "" and grep(/$id/, @ids) and $target eq $s_target and $location eq "source") { + $line =~ s/$string/$to/; + } + print($line); + } + elsif ($changedesc) { + # Simply change the desc line if the id matches + if ($id ne "" and grep(/$id/, @ids)) { + $line =~ s/\s*desc:.*/ desc: $to/; + } + print($line); + } + elsif ($changeid) { + $line =~ s/^\s*id:\s*$from.*$/ id: $to/; + print($line); + } + else { + print("wut wut\n"); + } +} -- cgit v1.2.3