View | Details | Raw Unified | Return to bug 15395
Collapse All | Expand All

(-)a/misc/translator/LangInstaller.pm (-1 / +6 lines)
Lines 579-585 sub extract_messages_from_templates { Link Here
579
                my $keyword = $node->content;
579
                my $keyword = $node->content;
580
                $keyword =~ s/^'t(.*)'$/__$1/;
580
                $keyword =~ s/^'t(.*)'$/__$1/;
581
581
582
                say $fh "$keyword(" . join(', ', @args) . ");";
582
                # Only keep required args to have a clean output
583
                my @required_args = shift @args;
584
                push @required_args, shift @args if $keyword =~ /n/;
585
                push @required_args, shift @args if $keyword =~ /p/;
586
587
                say $fh "$keyword(" . join(', ', @required_args) . ");";
583
            }
588
            }
584
589
585
        }
590
        }
(-)a/t/LangInstaller.t (+108 lines)
Line 0 Link Here
1
use Modern::Perl;
2
3
use FindBin '$Bin';
4
use lib "$Bin/../misc/translator";
5
6
use Test::More tests => 39;
7
use File::Temp qw(tempdir);
8
use File::Slurp;
9
use Locale::PO;
10
11
use t::lib::Mocks;
12
13
use_ok('LangInstaller');
14
15
my $installer = LangInstaller->new();
16
17
my $tempdir = tempdir(CLEANUP => 0);
18
t::lib::Mocks::mock_config('intranetdir', "$Bin/LangInstaller/templates");
19
my @files = ('simple.tt');
20
$installer->extract_messages_from_templates($tempdir, @files);
21
22
ok(-e "$tempdir/simple.tt", 'it has created a temporary file simple.tt');
23
SKIP: {
24
    skip "simple.tt does not exist", 37 unless -e "$tempdir/simple.tt";
25
26
    my $output = read_file("$tempdir/simple.tt");
27
    my $expected_output = <<'EOF';
28
__('hello');
29
__x('hello {name}');
30
__n('item', 'items');
31
__nx('{count} item', '{count} items');
32
__p('context', 'hello');
33
__px('context', 'hello {name}');
34
__np('context', 'item', 'items');
35
__npx('context', '{count} item', '{count} items');
36
__npx('context', '{count} item', '{count} items');
37
__x('status is {status}');
38
__('active');
39
__('inactive');
40
__('Inside block');
41
EOF
42
43
    is($output, $expected_output, "Output of extract_messages_from_templates is as expected");
44
45
    my $xgettext_cmd = "xgettext -L Perl --from-code=UTF-8 "
46
        . "--package-name=Koha --package-version='' "
47
        . "-k -k__ -k__x -k__n:1,2 -k__nx:1,2 -k__xn:1,2 -k__p:1c,2 "
48
        . "-k__px:1c,2 -k__np:1c,2,3 -k__npx:1c,2,3 "
49
        . "-o $tempdir/Koha.pot -D $tempdir simple.tt";
50
51
    system($xgettext_cmd);
52
    my $pot = Locale::PO->load_file_asarray("$tempdir/Koha.pot");
53
54
    my @expected = (
55
        {
56
            msgid => '"hello"',
57
        },
58
        {
59
            msgid => '"hello {name}"',
60
        },
61
        {
62
            msgid => '"item"',
63
            msgid_plural => '"items"',
64
        },
65
        {
66
            msgid => '"{count} item"',
67
            msgid_plural => '"{count} items"',
68
        },
69
        {
70
            msgid => '"hello"',
71
            msgctxt => '"context"',
72
        },
73
        {
74
            msgid => '"hello {name}"',
75
            msgctxt => '"context"',
76
        },
77
        {
78
            msgid => '"item"',
79
            msgid_plural => '"items"',
80
            msgctxt => '"context"',
81
        },
82
        {
83
            msgid => '"{count} item"',
84
            msgid_plural => '"{count} items"',
85
            msgctxt => '"context"',
86
        },
87
        {
88
            msgid => '"status is {status}"',
89
        },
90
        {
91
            msgid => '"active"',
92
        },
93
        {
94
            msgid => '"inactive"',
95
        },
96
        {
97
            msgid => '"Inside block"',
98
        },
99
    );
100
101
    for (my $i = 0; $i < @expected; $i++) {
102
        for my $key (qw(msgid msgid_plural msgctxt)) {
103
            my $expected = $expected[$i]->{$key};
104
            my $expected_str = defined $expected ? $expected : 'not defined';
105
            is($pot->[$i + 1]->$key, $expected, "$i: $key is $expected_str");
106
        }
107
    }
108
}
(-)a/t/LangInstaller/templates/simple.tt (-1 / +34 lines)
Line 0 Link Here
0
- 
1
[% t('hello') %]
2
[% tx('hello {name}', { name = 'Bob' }) %]
3
[% tn('item', 'items', count) %]
4
[% tnx('{count} item', '{count} items', count, { count = count }) %]
5
[% tp('context', 'hello') %]
6
[% tpx('context', 'hello {name}', { name = 'Bob' }) %]
7
[% tnp('context', 'item', 'items', count) %]
8
[% tnpx('context', '{count} item', '{count} items', count, { count = count }) %]
9
10
[% # it also works on multiple lines
11
    tnpx (
12
        'context',
13
        '{count} item',
14
        '{count} items',
15
        count,
16
        {
17
            count = count,
18
        }
19
    )
20
%]
21
22
[% # and t* calls can be nested
23
    tx('status is {status}', {
24
        status = active ? t('active') : t('inactive')
25
    })
26
%]
27
28
[%# but a TT comment won't get picked
29
    t('not translatable')
30
%]
31
32
[% BLOCK %]
33
    [% t('Inside block') %]
34
[% END %]

Return to bug 15395