Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 224291dd

Von Thomas Heck vor mehr als 11 Jahren hinzugefügt

  • ID 224291ddfd1633e2e3a47632cca24c1039fcb105
  • Vorgänger c6ab4d99
  • Nachfolger 6061749d

scripts/locales.pl um JS-Übersetzung erweitert

Unterschiede anzeigen:

SL/Layout/None.pm
13 13
  qw(
14 14
    js/jquery.js
15 15
    js/common.js
16
    js/namespace.js
16 17
  ),
17 18
  $self->SUPER::use_javascript(@_);
18 19
}
js/kivi.js
1
namespace("kivi", function(ns) {
2

  
3
  ns._localeLang = false;
4
  ns._locales = {};
5

  
6
  ns.t8 = function(text, params) {
7
    if( ns._localeLang ) {
8
      if( !ns._locales[ns._localeLang] ) {
9
        jQuery.ajax({
10
          url: "locale/"+ ns._localeLang +"/js.js",
11
          async: false,
12
          dataType: "json",
13
          success: function(res) {
14
            ns._locales[ns._localeLang] = res;
15
          },
16
          error: function(xhr, textStatus, errorThrown) {
17
            alert(textStatus +": "+ errorThrown);
18
          },
19
        });
20
      }
21

  
22
      text = ns._locales[ns._localeLang][text] || text;
23
    }
24

  
25
    if( Object.prototype.toString.call( params ) === '[object Array]' ) {
26
      var len = params.length;
27

  
28
      for(var i=0; i<len; ++i) {
29
        var key = i + 1;
30
        var value = params[i];
31
        text = text.split("#"+ key).join(value);
32
      }
33
    }
34
    else if( typeof params == 'object' ) {
35
      for(var key in params) {
36
        var value = params[key];
37
        text = text.split("#{"+ key +"}").join(value);
38
      }
39
    }
40

  
41
    return text;
42
  };
43

  
44
  ns.initLocale = function(localeLang) {
45
    ns._localeLang = localeLang;
46
  };
47

  
48
});
js/namespace.js
1
var namespace = (function() {
2
  var namespace = function(nsString, callback) {
3
    var nsParts = nsString.split(namespace.namespaceDelimiter);
4

  
5
    var ns = namespace.root;
6

  
7
    var len = nsParts.length;
8
    for(var i=0; i<len; ++i)
9
    {
10
      if( !ns[nsParts[i]] )
11
        ns[nsParts[i]] = {__namespaceAutoCreated: true};
12

  
13
      ns = ns[nsParts[i]];
14
    }
15

  
16
    if( callback )
17
    {
18
      var nsExt = callback.call(ns, ns);
19

  
20
      if( nsExt )
21
      {
22
        if( !ns )
23
          ns = {};
24

  
25
        for(var key in nsExt)
26
          ns[key] = nsExt[key];
27
      }
28

  
29
      ns.__namespaceAutoCreated = false;
30
    }
31
    else if( namespace.loadNamespace && ns.__namespaceAutoCreated )
32
    {
33
      var url;
34

  
35
      var len = namespace.namespaceLocations.length;
36
      for(var i=0; i<len; ++i)
37
      {
38
        var entry = namespace.namespaceLocations[i];
39
        if( nsString.indexOf(entry.namespace) === 0 )
40
        {
41
          url = entry.location;
42
          break;
43
        }
44
      }
45

  
46
      url += "/"+ nsString +".js";
47

  
48
      jQuery.ajax({
49
        url: url,
50
        async: false,
51
        dataType: "text",
52
        success: function(res) {
53
          eval(res);
54
          /*
55
          var script = window.document.createElement("script");
56
          script.type = "text/javascript";
57
          script.text = res;
58
          window.document.body.appendChild(script);
59
          */
60
        },
61
        error: function(xhr, textStatus, errorThrown) {
62
          alert(textStatus +": "+ errorThrown);
63
        },
64
      });
65
    }
66

  
67
    return ns;
68
  };
69

  
70
  return namespace;
71
})();
72

  
73
window.namespaceRoot = {};
74
namespace.root = window.namespaceRoot;
75
namespace.namespaceDelimiter = ".";
76
namespace.namespaceLocations = [{namespace: "", location: "js"}];
77
namespace.loadNamespace = true;
locale/de/js.js
1
{
2
}
scripts/locales.pl
36 36
my $dbupdir      = "$basedir/sql/Pg-upgrade";
37 37
my $dbupdir2     = "$basedir/sql/Pg-upgrade2";
38 38
my $menufile     = "menu.ini";
39
my @javascript_dirs = ($basedir .'/js', $basedir .'/templates/webpages');
39 40
my $submitsearch = qr/type\s*=\s*[\"\']?submit/i;
40 41
our $self        = {};
41 42
our $missing     = {};
42 43
our @lost        = ();
43 44

  
44
my (%referenced_html_files, %locale, %htmllocale, %alllocales, %cached, %submit);
45
my (%referenced_html_files, %locale, %htmllocale, %alllocales, %cached, %submit, %jslocale);
45 46
my ($ALL_HEADER, $MISSING_HEADER, $LOST_HEADER);
46 47

  
47 48
init();
......
123 124
handle_file($_, $dbupdir2) for @dbplfiles2;
124 125
scanmenu($_)               for @menufiles;
125 126

  
127
for my $file_name (map({find_files($_)} @javascript_dirs)) {
128
  scan_javascript_file($file_name);
129
}
130

  
126 131
# merge entries to translate with entries from files 'missing' and 'lost'
127 132
merge_texts();
128 133

  
......
134 139
  data_sub  => sub { _print_line($_, $self->{texts}{$_}, @_) for sort keys %alllocales },
135 140
);
136 141

  
142
open(my $js_file, '>:encoding(utf8)', $locales_dir .'/js.js') || die;
143
print $js_file '{'."\n";
144
my $first_entry = 1;
145
for my $key (sort(keys(%jslocale))) {
146
  print $js_file (!$first_entry ? ',' : '') . _double_quote($key) .':'. _double_quote($self->{texts}{$key}) ."\n";
147
  $first_entry = 0;
148
}
149
print $js_file '}'."\n";
150
close($js_file);
151

  
137 152
  foreach my $text (keys %$missing) {
138 153
    if ($locale{$text} || $htmllocale{$text}) {
139 154
      unless ($self->{texts}{$text}) {
......
385 400
      while ($rc) {
386 401
        if (/Locale/) {
387 402
          unless (/^use /) {
388
            my ($null, $country) = split /,/;
403
            my ($null, $country) = split(/,/);
389 404
            $country =~ s/^ +[\"\']//;
390 405
            $country =~ s/[\"\'].*//;
391 406
          }
......
577 592
  $referenced_html_files{$_} = 1 for keys %{$cached{$file}{scanh}};
578 593
}
579 594

  
595
sub scan_javascript_file {
596
  my ($file) = @_;
597

  
598
  open(my $fh, $file) || die('can not open file: '. $file);
599

  
600
  while( my $line = readline($fh) ) {
601
    while( $line =~ m/
602
                    kivi.t8
603
                    \s*
604
                    \(
605
                    \s*
606
                    ([\'\"])
607
                    (.*?)
608
                    (?<!\\)\1
609
                    /ixg )
610
    {
611
      my $text = unescape_template_string($2);
612

  
613
      $jslocale{$text} = 1;
614
      $alllocales{$text} = 1;
615
    }
616
  }
617

  
618
  close($fh);
619
}
580 620
sub search_unused_htmlfiles {
581 621
  my @unscanned_dirs = ('../../templates/webpages');
582 622

  
......
610 650
  return  "'" . $val .  "'";
611 651
}
612 652

  
653
sub _double_quote {
654
  my $val = shift;
655
  $val =~ s/(\"|\\$)/\\$1/g;
656
  return  '"'. $val .'"';
657
}
658

  
613 659
sub _print_line {
614 660
  my $key      = _single_quote(shift);
615 661
  my $text     = _single_quote(shift);

Auch abrufbar als: Unified diff