Download this file

localize.pl    132 lines (111 with data), 2.4 kB

  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
#!/usr/bin/perl
# In Excel: save as "Windows-kommagetrennt (.csv)
# iconv -f ISO-8859-1 -t UTF-8 App_Localization.csv > App_Localization2.csv
use strict;
my $VALUES = "../app/src/main/res/values";
open IN, "< App_Localization2.csv";
# read titles
my %trans;
my @langs = split /;/, <IN>;
for my $lng (@langs)
{
$trans{$lng} = ();
}
# skip lines for other apps
my $line;
do
{
$line = <IN>;
chomp $line;
} while ($line !~ /^Socialgroups App;;;;/);
my $idctr = 0;
while ($line = <IN>)
{
chomp $line;
last if ($line =~ /Profile Editor;;;;/);
my @cols = split /;/, $line;
for my $c (0..$#langs)
{
my $lng = $langs[$c];
# print "$idctr - $c - $cols[$c]\n";
$trans{$lng}->{$idctr} = $cols[$c];
}
$idctr++;
}
close IN;
sub pickId($)
{
my $orig = lc(shift());
for my $k (keys %{$trans{'English'}})
{
my $tv = lc($trans{'English'}->{$k});
# print "COMPARE $k - '$orig' eq '$tv'\n";
return $k if ($orig eq $tv);
}
return undef;
}
my $tlang = { 'fr' => 'French', 'nl' => 'Dutch', 'de' => 'German', 'se' => 'Swedish' };
for my $lk (keys %{$tlang})
{
open OUT, "> ${VALUES}-$lk/strings.xml";
close OUT;
}
sub appendAll($)
{
my $line = shift;
for my $lk (keys %{$tlang})
{
open OUT, ">> ${VALUES}-$lk/strings.xml" || die;
print OUT "$line\n";
close OUT;
}
}
open IN, "< $VALUES/strings.xml";
while ($line = <IN>)
{
chomp $line;
if ($line =~ /<string name="(\w+)">([^<]+)<\/string>/)
{
my $key = $1;
my $val = $2;
my $id = &pickId($val);
if ($id == undef)
{
print "no value $key = \"$val\"\n";
&appendAll($line);
}
else
{
for my $lk (keys %{$tlang})
{
# print "$lk " . $trans{$tlang->{$lk}}->{$id} . "\n";
my $tr = $trans{$tlang->{$lk}}->{$id};
if ($tr eq "")
{
my $lng = $tlang->{$lk};
print "no $lng translation for $key = \"$val\"\n";
$tr = $val;
}
$tr =~ s/'/\\'/g;
open OUT, ">> ${VALUES}-$lk/strings.xml" || die;
print OUT " <string name=\"$key\">$tr</string>\n";
close OUT;
}
}
}
elsif ($line =~ /<!-- \w+ -->/)
{
for my $lk (keys %{$tlang})
{
my $lng = $tlang->{$lk};
open OUT, ">> ${VALUES}-$lk/strings.xml" || die;
print OUT " <!-- $lng -->\n";
close OUT;
}
}
else
{
&appendAll($line);
}
}
close IN;