| 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; |