File: update_strings.pl

package info (click to toggle)
slimp3 4.2.6-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 5,720 kB
  • ctags: 1,310
  • sloc: perl: 25,569; sh: 499; java: 56; makefile: 41
file content (97 lines) | stat: -rw-r--r-- 2,634 bytes parent folder | download | duplicates (2)
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
#! /usr/bin/perl -w

# Update the strings.txt file from the SLIMP3 project.
#
# Reads the strings.txt file and checks all items for the 
# presence of an entry defined in $language.
# If such an entry is not present, it displays entries that are
# defined in @display and asks the input for $language.
#
# An additional sort is done on all translation strings 
# (e.g. DE, FR, EN --> DE, EN, FR) 
#
# L. Hollevoet
#

$language = "NL";                  # language to update
@display  = ("EN", "DE", "FR");    # display these languages as 'hints'

# Open input and output files
open (INPUT, "<strings.txt") || die "Could not open strings.txt";
open (OUTPUT,">out.txt") || die "Could not open output.txt";

# Process the input file
while ($line = <INPUT>){

    # copy comments blindly, they begin with #
    if ($line =~ /^\#/){ 
	print OUTPUT $line;
	next; 
    }

    # search for an item, this begins with a capital
    if ($line =~ /^([A-Z].+)/){
	$item = $1;
    }

    # grab the contents of an entry, format: tab, XX, tab, string with XX=language 
    if ($line =~ /^\s([A-Z]{2})\s(.+)/){
	# language identifier in $1
	# translated string in $2

	# Do a check on multiple declarations of a language string under one item
	# In case it occurs, print a warning and keep the last occurrence
	if (defined $entries{$1}){
	    print "WARNING: more than one '$1' in $item\n";
	}

	$entries{$1} = $2;
    }

    # if an empty line is encountered, check if something has to be written to the
    # output file. Make sure to also recognise empty lines containing multiple tabs
    if ($line =~ /^\s+$/){
	if (defined %entries){
	    update_item();
	    print_item();
	    undef %entries;
	} else {
	    print OUTPUT "\n";
	}
    }
}

# Close files
close(INPUT);
close(OUTPUT);

# This sub will display the defined languages in @display and ask for 
# the translated input for language $language 
# If the user gives an empty line, then skip the addition
sub update_item {
    if (not defined $entries{$language}){
	# if we get here the $language entry was not defined in the item
	print "Item: $item\n";
	foreach $lang (@display){
	    if (defined $entries{$lang}){
		print "\t$lang\t$entries{$lang}\n";
	    }
	}
	print "\t$language\t";
	chomp ($input = <STDIN>);

	if ($input ne ""){
	    $entries{$language} = $input;
	}

    }
}

# This sub writes an item to the output file
sub print_item {
    print OUTPUT "$item\n";
    foreach $entry (sort keys %entries){
	print OUTPUT "\t$entry\t$entries{$entry}\n";
    }
    print OUTPUT "\n";
}