Bug 17600: Standardize our EXPORT_OK
[srvgit] / Koha / Template / Plugin / I18N.pm
1 package Koha::Template::Plugin::I18N;
2
3 # Copyright BibLibre 2015
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use base qw( Template::Plugin );
23
24 use C4::Context;
25 use Koha::I18N qw( __ __n __np __npx __nx __p __px __x __xn );
26
27 =head1 NAME
28
29 Koha::Template::Plugin::I18N - Translate strings in templates
30
31 =head1 SYNOPSIS
32
33     [% PROCESS 'i18n.inc' %]
34
35     . . .
36
37     [% I18N.t("Hello!") %]
38     [% I18N.tx("Hello {name}", { name = name }) %]
39     [% I18N.tn("Hello friend", "Hello friends", count) %]
40     [% I18N.tnx("Hello my {count} friend", "Hello my {count} friends", count, { count = count }) %]
41     [% I18N.tp('verb', 'Item') # to order %]
42     [% I18N.tnp('bibliographic material', "item", "items", count) %]
43     [% I18N.tnpx('bibliographic material', "{count} item", "{count} items", count, { count = count }) %]
44
45 Do not use this plugin directly. Add the following directive
46
47     [% PROCESS 'i18n.inc' %]
48
49 and use the macros defined.
50
51 See: https://wiki.koha-community.org/wiki/Internationalization,_plural_forms,_context,_and_more_(RFC)
52 for further context.
53
54 =head1 METHODS
55
56 =head2 t
57
58     [% I18N.t("hello") %]
59
60 Translate - The simplest type of translatable string where
61 there are no variables and not pluralisations to consider.
62
63 =cut
64
65 sub t {
66     my ($self, $msgid) = @_;
67     return __($msgid);
68 }
69
70 =head2 tx
71
72     [% I18N.tx("hello {name}", { name = name }) %]
73
74 Translate with variable - A translatable string that
75 includes a variable
76
77 =cut
78
79 sub tx {
80     my ($self, $msgid, $vars) = @_;
81     return __x($msgid, %$vars);
82 }
83
84 =head2 tn
85
86     [% I18N.tn("item", "items", count) %]
87
88 Translate with plural - A translatable string that needs
89 singular and plural forms
90
91 =cut
92
93 sub tn {
94     my ($self, $msgid, $msgid_plural, $count) = @_;
95     return __n($msgid, $msgid_plural, $count);
96 }
97
98 =head2 tnx
99
100     [% I18N.tnx("{count} item", "{count} items", count, { count = count }) %]
101
102 Translate with plural and variable - A translatable string
103 that needs singular and plural forms and includes a variable
104
105 =cut
106
107 sub tnx {
108     my ($self, $msgid, $msgid_plural, $count, $vars) = @_;
109     return __nx($msgid, $msgid_plural, $count, %$vars);
110 }
111
112 =head2 txn
113
114 Alias of tnx
115
116 =cut
117
118 sub txn {
119     my ($self, $msgid, $msgid_plural, $count, $vars) = @_;
120     return __xn($msgid, $msgid_plural, $count, %$vars);
121 }
122
123 =head2 tp
124
125     [% I18N.tp("context", "hello") %]
126
127 Translate with context - A translatable string where a
128 context hint would be helpful to translators.
129
130 An example would be where in english a single word may be
131 be used as both a verb and a noun. You may want to add a
132 note to distinguish this particular use case so translators
133 can understand the context correctly.
134
135 =cut
136
137 sub tp {
138     my ($self, $msgctxt, $msgid) = @_;
139     return __p($msgctxt, $msgid);
140 }
141
142 =head2 tpx
143
144     [% I18N.tpx("context", "hello {name}", { name = name }) %]
145
146 Translate with context and variable - A translatable string
147 that needs both a contextual hint and includes a variable.
148
149 =cut
150
151 sub tpx {
152     my ($self, $msgctxt, $msgid, $vars) = @_;
153     return __px($msgctxt, $msgid, %$vars);
154 }
155
156 =head2 tnp
157
158     [% I18N.tnp("context", "item", "items", count) %]
159
160 Translate with context and plural - A translatable string
161 that needs both a contextual hints and singular and plural
162 forms.
163
164 =cut
165
166 sub tnp {
167     my ($self, $msgctxt, $msgid, $msgid_plural, $count) = @_;
168     return __np($msgctxt, $msgid, $msgid_plural, $count);
169 }
170
171 =head2 tnpx
172
173     [% I18N.tnpx("context", "{count} item", "{count} items", count, { count = count }) %]
174
175 Translate with context, plural and variables - A translatable
176 string that needs contextual hints, singular and plural forms
177 and also includes variables.
178
179 =cut
180
181 sub tnpx {
182     my ($self, $msgctxt, $msgid, $msgid_plural, $count, $vars) = @_;
183     return __npx($msgctxt, $msgid, $msgid_plural, $count, %$vars);
184 }
185
186 1;