summaryrefslogtreecommitdiff
path: root/util/jsgen.pl
diff options
context:
space:
mode:
Diffstat (limited to 'util/jsgen.pl')
-rwxr-xr-xutil/jsgen.pl135
1 files changed, 86 insertions, 49 deletions
diff --git a/util/jsgen.pl b/util/jsgen.pl
index 27cbc6da..03b79ab5 100755
--- a/util/jsgen.pl
+++ b/util/jsgen.pl
@@ -22,74 +22,111 @@ use LangFile;
use VNDB::L10N;
-sub l10n {
- # parse the .js code to find the l10n keys to use
- my $js = shift;
- my @keys;
- push @keys, $1 ? quotemeta($1) : qr/$2/ while($js =~ m{(?:mt\('([a-z0-9_]+)'[,\)]|l10n /([^/]+)/)}g);
- # also add the _lang_* for all languages for which we have a translation
- my $jskeys_lang = join '|', VNDB::L10N::languages();
- push @keys, qr/_lang_(?:$jskeys_lang)/;
- # fetch the corresponding text from lang.txt
- my %lang; # key1 => { lang1 => .., lang2 => .. }, key2 => { .. }
+my %lang; # lang1 => { key1 => .., key22 => .. }, lang2 => { .. }
+
+sub l10n_load {
+ # fetch all text from lang.txt
my $lang = LangFile->new(read => "$ROOT/data/lang.txt");
- my $cur; # 0 = none/excluded, 1 = TL lines
my $key;
while((my $l = $lang->read())) {
my $type = shift @$l;
- if($type eq 'key') {
- my $k = shift @$l;
- $cur = grep $k =~ /$_/, @keys;
- $key = $k;
- }
- if($type eq 'tl' && $cur) {
- my($lang, $sync, $val) = @$l;
- next if !$val;
- $lang{$key}{$lang} = $val;
- }
+ $key = shift @$l if $type eq 'key';
+ $lang{$l->[0]}{$key} = $l->[2] if $type eq 'tl';
}
+}
+
- # generate JS code
+sub l10n {
+ my($lang, $js) = @_;
+
+ # parse the .js code and replace mt()'s that can be modified in-place, otherwise add to the @keys
+ my @keys;
+ $js =~ s{(?:mt\('([a-z0-9_]+)'([,\)])|l10n /([^/]+)/)}#
+ my($k, $s, $q) = ($1, $2, $3);
+ my $v = $k ? $lang{$lang}{$k} || $lang{'en'}{$k} : '';
+ if($q) { $q ne '<perl regex>' && push @keys, qr/$q/; '' }
+ elsif($s eq ')' && $v && $v !~ /[\~\[\]]/) {
+ $v =~ s/"/\\"/g;
+ $v =~ s/\n/\\n/g;
+ qq{"$v"}
+ } else {
+ push @keys, quotemeta($k);
+ "mt('$k'$s"
+ }
+ #eg;
+ # also add the _lang_* for all languages for which we have a translation
+ my $jskeys_lang = join '|', VNDB::L10N::languages();
+ push @keys, qr/_lang_(?:$jskeys_lang)/;
+
+ # generate header
my $r = "L10N_STR = {\n";
my $first = 1;
- for my $key (sort keys %lang) {
+ for my $key (sort keys %{$lang{$lang}}) {
+ next if !grep $key =~ /$_/, @keys;
$r .= ",\n" if !$first;
$first = 0;
- # let's assume all L10N keys are valid JS variable names
- $r .= sprintf qq| "%s": {\n|, $key;
- my $firstk = 1;
- for (sort keys %{$lang{$key}}) {
- $r .= ",\n" if !$firstk;
- $firstk = 0;
- my $lang = $_;
- $lang = qq{"$lang"} if $lang =~ /^(?:as|do|if|in|is)$/; # reserved two-char words
- my $val = $lang{$key}{$_};
- $val =~ s/"/\\"/g;
- $val =~ s/\n/\\n/g;
- $r .= sprintf qq| %s: "%s"|, $lang, $val;
+ my $val = $lang{$lang}{$key} || $lang{'en'}{$key};
+ $val =~ s/"/\\"/g;
+ $val =~ s/\n/\\n/g;
+ $val =~ s/\[index,.+$// if $key =~ /^_vnlength_/; # special casing the VN lengths, since the JS mt() doesn't handle [index]
+ $r .= sprintf qq| %s: "%s"|, $key !~ /^[a-z0-9_]+$/ ? "'$key'" : $key, $val;
+ }
+ $r .= "\n};";
+ return ("$r\n", $js);
+}
+
+
+# screen resolution information, suitable for usage in filFSelect()
+sub resolutions {
+ my $res_cat = '';
+ my $resolutions = '';
+ my $comma = 0;
+ for my $i (0..$#{$S{resolutions}}) {
+ my $r = $S{resolutions}[$i];
+ if($res_cat ne $r->[1]) {
+ $resolutions .= ']' if $res_cat;
+ $resolutions .= ",['$r->[1]',";
+ $res_cat = $r->[1];
+ $comma = 0;
}
- $r .= "\n }";
+ $resolutions .= ($comma ? ',' : '')."[$i,'$r->[0]']";
+ $comma = 1;
}
- $r .= "\n};\n";
- $r .= 'L10N_LANG = [ '.join(', ', map qq{"$_"}, VNDB::L10N::languages()).' ];';
- return "$r\n";
+ $resolutions .= ']' if $res_cat;
+ return "resolutions = [ $resolutions ];\n";
}
sub jsgen {
- # JavaScript::Minifier::XS doesn't correctly handle perl's unicode,
- # so just do everything in raw bytes instead.
- open my $JS, '<', "$ROOT/data/script.js" or die $!;
+ l10n_load();
+ my $common = '';
+ $common .= resolutions();
+ $common .= sprintf "rlst_rstat = [ %s ];\n", join ', ', map qq{"$_"}, @{$S{rlst_rstat}};
+ $common .= sprintf "rlst_vstat = [ %s ];\n", join ', ', map qq{"$_"}, @{$S{rlst_vstat}};
+ $common .= sprintf "cookie_prefix = '%s';\n", $S{cookie_prefix};
+ $common .= sprintf "age_ratings = [ %s ];\n", join ',', map !defined $_ ? -1 : $_, @{$S{age_ratings}};
+ $common .= sprintf "languages = [ %s ];\n", join ', ', map qq{"$_"}, @{$S{languages}};
+ $common .= sprintf "platforms = [ %s ];\n", join ', ', map qq{"$_"}, @{$S{platforms}};
+ $common .= sprintf "media = [ %s ];\n", join ', ', map qq{"$_"}, sort keys %{$S{media}};
+ $common .= sprintf "release_types = [ %s ];\n", join ', ', map qq{"$_"}, @{$S{release_types}};
+ $common .= sprintf "animated = [ %s ];\n", join ', ', @{$S{animated}};
+ $common .= sprintf "voiced = [ %s ];\n", join ', ', @{$S{voiced}};
+ $common .= sprintf "vn_lengths = [ %s ];\n", join ', ', @{$S{vn_lengths}};
+ $common .= sprintf "L10N_LANG = [ %s ];\n", join(', ', map qq{"$_"}, VNDB::L10N::languages());
+
+ open my $JS, '<:utf8', "$ROOT/data/script.js" or die $!;
my $js .= join '', <$JS>;
close $JS;
- my $head = encode_utf8(l10n($js)) . "\n";
- $head .= sprintf "rlst_rstat = [ %s ];\n", join ', ', map qq{"$_"}, @{$S{rlst_rstat}};
- $head .= sprintf "rlst_vstat = [ %s ];\n", join ', ', map qq{"$_"}, @{$S{rlst_vstat}};
- $head .= sprintf "cookie_prefix = '%s';\n", $S{cookie_prefix};
- open my $NEWJS, '>', "$ROOT/static/f/script.js" or die $!;
- print $NEWJS $JavaScript::Minifier::XS::VERSION ? JavaScript::Minifier::XS::minify($head.$js) : $head.$js;
- close $NEWJS;
+
+ for my $l (VNDB::L10N::languages()) {
+ my($head, $body) = l10n($l, $js);
+ # JavaScript::Minifier::XS doesn't correctly handle perl's unicode, so manually encode
+ my $content = encode_utf8($head . $common . $body);
+ open my $NEWJS, '>', "$ROOT/static/f/js/$l.js" or die $!;
+ print $NEWJS $JavaScript::Minifier::XS::VERSION ? JavaScript::Minifier::XS::minify($content) : $content;
+ close $NEWJS;
+ }
}
jsgen;