summaryrefslogtreecommitdiff
path: root/lib/VNDB/L10N.pm
blob: d71a72dcfb5ff69db705200c2206bad17118af11 (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

use strict;
use warnings;

{
  package VNDB::L10N;
  use base 'Locale::Maketext';

  sub fallback_languages { ('en') };

  # used for the language switch interface, language tags must
  # be the same as in the languages hash in global.pl
  sub languages { ('en', 'ru') }

  sub maketext {
    my $r = eval { shift->SUPER::maketext(@_) };
    return $r if defined $r;
    warn "maketext failed for '@_': $@\n";
    return $_[0]||''; # not quite sure we want this
  }

  # can be called as either a subroutine or a method
  sub loadfile {
    my %lang = (
      en => \%VNDB::L10N::en::Lexicon,
      ru => \%VNDB::L10N::ru::Lexicon,
    );

    open my $F, '<:utf8', $VNDB::ROOT.'/data/lang.txt' or die "Opening language file: $!\n";
    my($empty, $line, $key, $lang) = (0, 0);
    while(<$F>) {
      chomp;
      $line++;

      # ignore intro
      if(!defined $key) {
        $key = 0 if /^\/intro$/;
        next;
      }
      # ignore comments
      next if /^#/;
      # key
      if(/^:(.+)$/) {
        $key = $1;
        $lang = undef;
        $empty = 0;
        next;
      }
      # locale string
      if(/^([a-z_-]{2,7})[ *]: (.+)$/) {
        $lang = $1;
        die "Unknown language on #$line: $lang\n" if !$lang{$lang};
        die "Unknown key for locale on #$line\n" if !$key;
        $lang{$lang}{$key} = $2;
        $empty = 0;
        next;
      }
      # multi-line locale string
      if($lang && /^\s+([^\s].*)$/) {
        $lang{$lang}{$key} .= ''.("\n"x$empty)."\n$1";
        $empty = 0;
        next;
      }
      # empty string (count them in case they're part of a multi-line locale string)
      if(/^\s*$/) {
        $empty++;
        next;
      }
      # something we didn't expect
      die "Don't know what to do with line $line\n" unless /^([a-z_-]{2,7})[ *]:/;
    }
    close $F;

    # dev.
    use Data::Dumper 'Dumper';
    warn Dumper \%lang;
  }
}


{
  package VNDB::L10N::en;
  use base 'VNDB::L10N';
  our %Lexicon = (
    _AUTO => 1
  );
}

{
  package VNDB::L10N::ru;
  use base 'VNDB::L10N::en';
  our %Lexicon;
}


1;