summaryrefslogtreecommitdiff
path: root/lpgen.pl
blob: d1626d4bd8a07900af81ae17748892f2e40c69fe (plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/perl
#
# Script generating translation templates from sources
#
# Miranda IM: the free IM client for Microsoft* Windows*
# 
# Copyright 2000-2008 Miranda ICQ/IM project,
# all portions of this codebase are copyrighted to the people
# listed in contributors.txt.
# 
# 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 program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

use POSIX;
use File::Find;
use strict;

my $rootdir = '';
my %hash = ();
my $clines = 0;

my $version = '0.8';
my $time = localtime();

if (@ARGV && $ARGV[0] eq "version") {
	shift @ARGV;
	# 1st arg version major i.e. 07 for "0.7"
	if (@ARGV) {
		my $vermaj = shift @ARGV;
		if ($vermaj =~ /^([0-9]*)([0-9])$/) {
			if ($1 eq "") {
				$version = $2;
			} else {
				$version = "$1.$2";
			}
		} else {
			$version = $vermaj;
		}
	}
	# 2nd arg version minor i.e. 1 for "0.7.1"
	if (@ARGV) {
		my $vermin = shift @ARGV;
		if ($vermin =~ /^[0-9]+$/) {
			$version .= ".$vermin";
		} else {
			$version .= $vermin;
		}
	}
	# 3rd arg build number i.e. 36 for "0.7.1 build 36"
	if (@ARGV) {
		my $build = shift @ARGV;
		if ($build =~ /^[0-9]+$/) {
			$version .= " build $build";
		} else {
			$version .= $build;
		}
	}
}

#Language Files
if (!@ARGV) {
	create_langfile(
		'./MirOTR',
		'./MirOTR/Win32/langpack_english.txt',
		'English (US)', '0809',
		'ProgAndy',
		'admin@progandy.co.cc',
		'MirOTR');
}
else {
	print "Error: Unknown module $ARGV[0]\n";
	exit 1;
}

sub create_langfile {
	$rootdir = shift(@_);
	my $outfile = shift(@_);
	my $lang = shift(@_);
	my $locale = shift(@_);
	my $author = shift(@_);
	my $email = shift(@_);
	my $plugin = @_ ? shift(@_) : '';
	%hash = ();
	my %common = ();
	$clines = 0;
	print "Building language file for $rootdir:\n";
	find({ wanted => \&csearch, preprocess => \&pre_dir }, $rootdir);
	find({ wanted => \&rcsearch, preprocess => \&pre_dir }, $rootdir);
	open(WRITE, "> $outfile") or die;
	print WRITE <<HEADER;
Miranda Language Pack Version 1
Locale: $locale
Authors: $author
Author-email: $email
Last-Modified-Using: $plugin $version
; Generated by lpgen on $time
; Translations: $clines

HEADER
	foreach my $trans (keys %hash) {
		if ($hash{$trans} =~ /^\d+$/) {
			$common{$trans} = $hash{$trans};
			delete $hash{$trans};
		}
	}

	my @sorted = sort { (floor($common{$b}/20) == floor($common{$a}/20)) ? ($a cmp $b) : ($common{$b} <=> $common{$a}) } keys %common;
	print WRITE "; Common strings that belong to many files\n;[";
	print WRITE join "]\n;[", @sorted;
	print WRITE "]\n";

	my $file = '';
	foreach my $trans (sort { ($hash{$a} eq $hash{$b}) ? ($a cmp $b) : ($hash{$a} cmp $hash{$b}) } keys %hash) {
		if ($hash{$trans} ne $file) {
			$file = $hash{$trans};
			print WRITE "\n; $file\n";
		}
		print WRITE ";[$trans]\n";
	}

	close(WRITE);
	print "  $outfile is complete ($clines)\n\n";
}

sub pre_dir {
	# avoid parsing some ext SDKs
	my @files = ();
	return @files if (
		$File::Find::name =~/..\/extplugins/ or
		$File::Find::name =~/freeimage\/Source/ or
		$File::Find::name =~/dbrw\/sqlite3/);
	@files = grep { not /^\.\.?$/ } @_;
	return sort @files;
}

sub append_str {
	my $str = shift(@_);
	my $found = shift(@_);
	$str = substr($str, 1, length($str) - 2);
	if (length($str) gt 0 and $str ne "List1" and $str ne "Tree1" and $str =~ /[a-zA-Z]+/g) {
		my $path = $File::Find::name;
		$path =~ s/(\.\.\/)+miranda\///;
		if (!$hash{$str}) {
			$hash{$str} = $path;
			$clines ++;
			return 1;
		} elsif ($hash{$str} ne $path) {
			if ($hash{$str} =~ /^\d+$/) {
				$hash{$str} ++;
			} else {
				$hash{$str} = 1;
			}
		}
	}
	return 0;
}

sub csearch {
	if (-f $_ and ($_ =~ m/\.c(pp)?$/i or $_ =~ m/\.h(pp)?$/i)) {
		my $found = 0;
		my $file = $_;
		print "  Processing $_ ";
		open(READ, "< $_") or return;
		my $all = '';
		while (my $lines = <READ>) {
			$all = $all.$lines;
		}
		close(READ);
		$_ = $all;
		while (/(?:Button_SetIcon_IcoLib|Translate[A-Z]{0,2}|LPGENT?|ICQTranslateUtfStatic)\s*\(\s*(\".*?\")\s*[,\)]/g) {
			$found += append_str($1, $found);
		}
		print "($found)\n";
	}
}

sub rcsearch {
	if ( -f $_ and $_ =~ m/\.rc$/i) {
		my $found = 0;
		my $file = $_;
		print "  Processing $_ ";
		open(READ, "< $_") or return;
		my $all = '';
		while (my $lines = <READ>) {
			$all = $all.$lines;
		}
		close(READ);
		$_ = $all;
		while (/\s*(?:CONTROL|(?:DEF)?PUSHBUTTON|[LRC]TEXT|GROUPBOX|CAPTION|MENUITEM|POPUP)\s*(\".*?\")/g) {
			$found += append_str($1, $found);
		}
		print "($found)\n";
	}
}