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

(-)a/C4/Templates.pm (+5 lines)
Lines 70-75 sub new { Link Here
70
70
71
    # Do not use template cache if script is called from commandline
71
    # Do not use template cache if script is called from commandline
72
    my $use_template_cache = C4::Context->config('template_cache_dir') && defined $ENV{GATEWAY_INTERFACE};
72
    my $use_template_cache = C4::Context->config('template_cache_dir') && defined $ENV{GATEWAY_INTERFACE};
73
    require Koha::Template::Filters::I18N;
73
    my $template = Template->new(
74
    my $template = Template->new(
74
        {   EVAL_PERL    => 1,
75
        {   EVAL_PERL    => 1,
75
            ABSOLUTE     => 1,
76
            ABSOLUTE     => 1,
Lines 78-83 sub new { Link Here
78
            COMPILE_DIR => $use_template_cache ? C4::Context->config('template_cache_dir') : '',
79
            COMPILE_DIR => $use_template_cache ? C4::Context->config('template_cache_dir') : '',
79
            INCLUDE_PATH => \@includes,
80
            INCLUDE_PATH => \@includes,
80
            FILTERS => {},
81
            FILTERS => {},
82
            LOAD_FILTERS => [
83
                Template::Filters->new(),
84
                Koha::Template::Filters::I18N->new(),
85
            ],
81
            ENCODING => 'UTF-8',
86
            ENCODING => 'UTF-8',
82
        }
87
        }
83
    ) or die Template->error();
88
    ) or die Template->error();
(-)a/Koha/Template/Filters/I18N.pm (+84 lines)
Line 0 Link Here
1
package Koha::Template::Filters::I18N;
2
3
use Modern::Perl;
4
use base 'Template::Base';
5
use Template::Constants;
6
use Koha::I18N;
7
8
our $STATIC_FILTERS = {
9
    't' => \&t,
10
};
11
12
our $DYNAMIC_FILTERS = {
13
    tx => \&tx_factory,
14
    tn => \&tn_factory,
15
    tnx => \&tnx_factory,
16
17
    # i18n functions that takes a context argument (tp, tpx, tnp, tnpx)
18
    # are deliberately not defined as filters.
19
    # Because context is the first argument, this would require writing:
20
    #   [% 'context' | tp('message') %]
21
    # Whereas we'd prefer to write:
22
    #   [% 'message' | tp('context') %]
23
    # There are two ways to fix this:
24
    # 1. Change the order of arguments in Koha::Template::Plugin::I18N and
25
    #    change the -k options in xgettext-tt2 (the msgid must be first), or
26
    # 2. Use different keywords, and add them as -k options in xgettext-tt2
27
};
28
29
sub fetch {
30
    my ($self, $name, $args, $context) = @_;
31
32
    return $STATIC_FILTERS->{$name} if $STATIC_FILTERS->{$name};
33
34
    if (my $factory = $DYNAMIC_FILTERS->{$name}) {
35
        return $factory->($context, $args ? @$args : ());
36
    }
37
38
    return (undef, Template::Constants::STATUS_DECLINED);
39
}
40
41
# This sub is never called in theory as Template::Filters::store is called
42
# first and accept all filters.
43
# We declare it anyway, just in case the order of filter providers is changed
44
sub store {
45
    return (undef, Template::Constants::STATUS_DECLINED);
46
}
47
48
sub t {
49
    my ($msgid) = @_;
50
51
    return __($msgid);
52
}
53
54
sub tx_factory {
55
    my ($context, $vars) = @_;
56
57
    return sub {
58
        my ($msgid) = @_;
59
60
        return __x($msgid, $vars ? %$vars : ());
61
    }
62
}
63
64
sub tn_factory {
65
    my ($context, $msgid_plural, $count) = @_;
66
67
    return sub {
68
        my ($msgid) = @_;
69
70
        return __n($msgid, $msgid_plural, $count);
71
    }
72
}
73
74
sub tnx_factory {
75
    my ($context, $msgid_plural, $count, $vars) = @_;
76
77
    return sub {
78
        my ($msgid) = @_;
79
80
        return __nx($msgid, $msgid_plural, $count, $vars ? %$vars : ());
81
    }
82
}
83
84
1;
(-)a/t/db_dependent/misc/translator/sample.tt (+4 lines)
Lines 34-36 Link Here
34
[% BLOCK %]
34
[% BLOCK %]
35
    [% t('Inside block') | $raw %]
35
    [% t('Inside block') | $raw %]
36
[% END %]
36
[% END %]
37
38
[% 'with filter' | t %]
39
40
[% 'filter singular' | tn('filter plural', 2) %]
(-)a/t/db_dependent/misc/translator/xgettext-tt2.t (-2 / +10 lines)
Lines 6-12 use File::Slurp; Link Here
6
use File::Temp qw(tempdir);
6
use File::Temp qw(tempdir);
7
use FindBin qw($Bin);
7
use FindBin qw($Bin);
8
use Locale::PO;
8
use Locale::PO;
9
use Test::More tests => 36;
9
use Test::More tests => 43;
10
10
11
my $tempdir = tempdir(CLEANUP => 1);
11
my $tempdir = tempdir(CLEANUP => 1);
12
12
Lines 63-68 my @expected = ( Link Here
63
    {
63
    {
64
        msgid => '"Inside block"',
64
        msgid => '"Inside block"',
65
    },
65
    },
66
    {
67
        msgid => '"with filter"',
68
    },
69
    {
70
        msgid => '"filter singular"',
71
        msgid_plural => '"filter plural"',
72
    },
66
);
73
);
67
74
68
for (my $i = 0; $i < @expected; $i++) {
75
for (my $i = 0; $i < @expected; $i++) {
Lines 72-74 for (my $i = 0; $i < @expected; $i++) { Link Here
72
        is($pot->[$i + 1]->$key, $expected, "$i: $key is $expected_str");
79
        is($pot->[$i + 1]->$key, $expected, "$i: $key is $expected_str");
73
    }
80
    }
74
}
81
}
75
- 
82
83
is(@$pot, 1 + @expected, "No more strings than expected");

Return to bug 36357