Lines 59-64
our @EXPORT_OK = qw(
Link Here
|
59 |
|
59 |
|
60 |
our $textdomain = 'Koha'; |
60 |
our $textdomain = 'Koha'; |
61 |
|
61 |
|
|
|
62 |
=head1 NAME |
63 |
|
64 |
Koha::I18N - Internationalization functions for Koha |
65 |
|
66 |
=head1 SYNOPSIS |
67 |
|
68 |
use Koha::I18N; |
69 |
|
70 |
# Basic translation functions |
71 |
my $translated = __('Hello world'); |
72 |
my $with_vars = __x('Hello {name}', name => 'World'); |
73 |
my $plural = __n('one item', '{count} items', $count, count => $count); |
74 |
|
75 |
# Context-aware translations |
76 |
my $context = __p('menu', 'File'); |
77 |
|
78 |
# Get available system locales (explicitly imported) |
79 |
use Koha::I18N qw(available_locales); |
80 |
my $locales = available_locales(); |
81 |
|
82 |
=head1 DESCRIPTION |
83 |
|
84 |
This module provides internationalization (i18n) functions for Koha using the |
85 |
GNU gettext system. It handles locale setup, message translation, and provides |
86 |
utility functions for working with system locales. |
87 |
|
88 |
The module automatically initializes the locale environment and provides a set |
89 |
of translation functions that support variable substitution, plural forms, and |
90 |
contextual translations. |
91 |
|
92 |
=head1 FUNCTIONS |
93 |
|
94 |
=head2 init |
95 |
|
96 |
Initializes the internationalization system by setting up locale environment |
97 |
variables and configuring gettext. This is called automatically when needed. |
98 |
|
99 |
=cut |
100 |
|
62 |
sub init { |
101 |
sub init { |
63 |
my $cache = Koha::Cache::Memory::Lite->get_instance(); |
102 |
my $cache = Koha::Cache::Memory::Lite->get_instance(); |
64 |
my $cache_key = 'i18n:initialized'; |
103 |
my $cache_key = 'i18n:initialized'; |
Lines 96-101
sub init {
Link Here
|
96 |
} |
135 |
} |
97 |
} |
136 |
} |
98 |
|
137 |
|
|
|
138 |
=head2 __ |
139 |
|
140 |
my $translated = __('Text to translate'); |
141 |
|
142 |
Basic translation function. Returns the translated text for the given message ID. |
143 |
|
144 |
=cut |
145 |
|
99 |
sub __ { |
146 |
sub __ { |
100 |
my ($msgid) = @_; |
147 |
my ($msgid) = @_; |
101 |
|
148 |
|
Lines 104-109
sub __ {
Link Here
|
104 |
return _gettext( \&gettext, [$msgid] ); |
151 |
return _gettext( \&gettext, [$msgid] ); |
105 |
} |
152 |
} |
106 |
|
153 |
|
|
|
154 |
=head2 __x |
155 |
|
156 |
my $translated = __x('Hello {name}', name => 'World'); |
157 |
|
158 |
Translation with variable substitution. Variables in {brackets} are replaced |
159 |
with the corresponding values from the provided hash. |
160 |
|
161 |
=cut |
162 |
|
107 |
sub __x { |
163 |
sub __x { |
108 |
my ( $msgid, %vars ) = @_; |
164 |
my ( $msgid, %vars ) = @_; |
109 |
|
165 |
|
Lines 112-117
sub __x {
Link Here
|
112 |
return _gettext( \&gettext, [$msgid], %vars ); |
168 |
return _gettext( \&gettext, [$msgid], %vars ); |
113 |
} |
169 |
} |
114 |
|
170 |
|
|
|
171 |
=head2 __n |
172 |
|
173 |
my $translated = __n('one item', '{count} items', $count); |
174 |
|
175 |
Plural-aware translation. Returns singular or plural form based on the count. |
176 |
|
177 |
=cut |
178 |
|
115 |
sub __n { |
179 |
sub __n { |
116 |
my ( $msgid, $msgid_plural, $count ) = @_; |
180 |
my ( $msgid, $msgid_plural, $count ) = @_; |
117 |
|
181 |
|
Lines 121-126
sub __n {
Link Here
|
121 |
return _gettext( \&ngettext, [ $msgid, $msgid_plural, $count ] ); |
185 |
return _gettext( \&ngettext, [ $msgid, $msgid_plural, $count ] ); |
122 |
} |
186 |
} |
123 |
|
187 |
|
|
|
188 |
=head2 __nx |
189 |
|
190 |
my $translated = __nx('one item', '{count} items', $count, count => $count); |
191 |
|
192 |
Plural-aware translation with variable substitution. |
193 |
|
194 |
=cut |
195 |
|
124 |
sub __nx { |
196 |
sub __nx { |
125 |
my ( $msgid, $msgid_plural, $count, %vars ) = @_; |
197 |
my ( $msgid, $msgid_plural, $count, %vars ) = @_; |
126 |
|
198 |
|
Lines 130-139
sub __nx {
Link Here
|
130 |
return _gettext( \&ngettext, [ $msgid, $msgid_plural, $count ], %vars ); |
202 |
return _gettext( \&ngettext, [ $msgid, $msgid_plural, $count ], %vars ); |
131 |
} |
203 |
} |
132 |
|
204 |
|
|
|
205 |
=head2 __xn |
206 |
|
207 |
Alias for __nx. |
208 |
|
209 |
=cut |
210 |
|
133 |
sub __xn { |
211 |
sub __xn { |
134 |
return __nx(@_); |
212 |
return __nx(@_); |
135 |
} |
213 |
} |
136 |
|
214 |
|
|
|
215 |
=head2 __p |
216 |
|
217 |
my $translated = __p('context', 'Text to translate'); |
218 |
|
219 |
Context-aware translation. Allows the same text to be translated differently |
220 |
based on context (e.g., 'File' in a menu vs 'File' as a document type). |
221 |
|
222 |
=cut |
223 |
|
137 |
sub __p { |
224 |
sub __p { |
138 |
my ( $msgctxt, $msgid ) = @_; |
225 |
my ( $msgctxt, $msgid ) = @_; |
139 |
|
226 |
|
Lines 143-148
sub __p {
Link Here
|
143 |
return _gettext( \&pgettext, [ $msgctxt, $msgid ] ); |
230 |
return _gettext( \&pgettext, [ $msgctxt, $msgid ] ); |
144 |
} |
231 |
} |
145 |
|
232 |
|
|
|
233 |
=head2 __px |
234 |
|
235 |
my $translated = __px('context', 'Hello {name}', name => 'World'); |
236 |
|
237 |
Context-aware translation with variable substitution. |
238 |
|
239 |
=cut |
240 |
|
146 |
sub __px { |
241 |
sub __px { |
147 |
my ( $msgctxt, $msgid, %vars ) = @_; |
242 |
my ( $msgctxt, $msgid, %vars ) = @_; |
148 |
|
243 |
|
Lines 152-157
sub __px {
Link Here
|
152 |
return _gettext( \&pgettext, [ $msgctxt, $msgid ], %vars ); |
247 |
return _gettext( \&pgettext, [ $msgctxt, $msgid ], %vars ); |
153 |
} |
248 |
} |
154 |
|
249 |
|
|
|
250 |
=head2 __np |
251 |
|
252 |
my $translated = __np('context', 'one item', '{count} items', $count); |
253 |
|
254 |
Context-aware plural translation. |
255 |
|
256 |
=cut |
257 |
|
155 |
sub __np { |
258 |
sub __np { |
156 |
my ( $msgctxt, $msgid, $msgid_plural, $count ) = @_; |
259 |
my ( $msgctxt, $msgid, $msgid_plural, $count ) = @_; |
157 |
|
260 |
|
Lines 162-167
sub __np {
Link Here
|
162 |
return _gettext( \&npgettext, [ $msgctxt, $msgid, $msgid_plural, $count ] ); |
265 |
return _gettext( \&npgettext, [ $msgctxt, $msgid, $msgid_plural, $count ] ); |
163 |
} |
266 |
} |
164 |
|
267 |
|
|
|
268 |
=head2 __npx |
269 |
|
270 |
my $translated = __npx('context', 'one item', '{count} items', $count, count => $count); |
271 |
|
272 |
Context-aware plural translation with variable substitution. |
273 |
|
274 |
=cut |
275 |
|
165 |
sub __npx { |
276 |
sub __npx { |
166 |
my ( $msgctxt, $msgid, $msgid_plural, $count, %vars ) = @_; |
277 |
my ( $msgctxt, $msgid, $msgid_plural, $count, %vars ) = @_; |
167 |
|
278 |
|
Lines 172-189
sub __npx {
Link Here
|
172 |
return _gettext( \&npgettext, [ $msgctxt, $msgid, $msgid_plural, $count ], %vars ); |
283 |
return _gettext( \&npgettext, [ $msgctxt, $msgid, $msgid_plural, $count ], %vars ); |
173 |
} |
284 |
} |
174 |
|
285 |
|
|
|
286 |
=head2 N__ |
287 |
|
288 |
my $msgid = N__('Text for later translation'); |
289 |
|
290 |
No-operation translation marker. Returns the original text unchanged but marks |
291 |
it for extraction by translation tools. |
292 |
|
293 |
=cut |
294 |
|
175 |
sub N__ { |
295 |
sub N__ { |
176 |
return $_[0]; |
296 |
return $_[0]; |
177 |
} |
297 |
} |
178 |
|
298 |
|
|
|
299 |
=head2 N__n |
300 |
|
301 |
my $msgid = N__n('singular', 'plural'); |
302 |
|
303 |
No-operation plural translation marker. |
304 |
|
305 |
=cut |
306 |
|
179 |
sub N__n { |
307 |
sub N__n { |
180 |
return $_[0]; |
308 |
return $_[0]; |
181 |
} |
309 |
} |
182 |
|
310 |
|
|
|
311 |
=head2 N__p |
312 |
|
313 |
my $msgid = N__p('context', 'Text for later translation'); |
314 |
|
315 |
No-operation context translation marker. |
316 |
|
317 |
=cut |
318 |
|
183 |
sub N__p { |
319 |
sub N__p { |
184 |
return $_[1]; |
320 |
return $_[1]; |
185 |
} |
321 |
} |
186 |
|
322 |
|
|
|
323 |
=head2 N__np |
324 |
|
325 |
my $msgid = N__np('context', 'singular', 'plural'); |
326 |
|
327 |
No-operation context plural translation marker. |
328 |
|
329 |
=cut |
330 |
|
187 |
sub N__np { |
331 |
sub N__np { |
188 |
return $_[1]; |
332 |
return $_[1]; |
189 |
} |
333 |
} |
Lines 235-241
sub _expand {
Link Here
|
235 |
Returns an arrayref of available system locales for use in system preferences. |
379 |
Returns an arrayref of available system locales for use in system preferences. |
236 |
|
380 |
|
237 |
Each locale is a hashref with: |
381 |
Each locale is a hashref with: |
238 |
- C<value>: The locale identifier (e.g., 'en_US.utf8', 'default') |
382 |
- C<value>: The locale identifier (e.g., 'en_US.utf8', 'default') |
239 |
- C<text>: Human-readable description (e.g., 'English (United States) - en_US.utf8') |
383 |
- C<text>: Human-readable description (e.g., 'English (United States) - en_US.utf8') |
240 |
|
384 |
|
241 |
Always includes 'default' as the first option. Additional locales are detected |
385 |
Always includes 'default' as the first option. Additional locales are detected |
Lines 323-326
sub available_locales {
Link Here
|
323 |
return \@available_locales; |
467 |
return \@available_locales; |
324 |
} |
468 |
} |
325 |
|
469 |
|
|
|
470 |
=head1 AUTHOR |
471 |
|
472 |
Koha Development Team |
473 |
|
474 |
=head1 COPYRIGHT |
475 |
|
476 |
Copyright 2012-2014 BibLibre |
477 |
|
478 |
=head1 LICENSE |
479 |
|
480 |
This file is part of Koha. |
481 |
|
482 |
Koha is free software; you can redistribute it and/or modify it under the |
483 |
terms of the GNU General Public License as published by the Free Software |
484 |
Foundation; either version 3 of the License, or (at your option) any later |
485 |
version. |
486 |
|
487 |
=cut |
488 |
|
326 |
1; |
489 |
1; |
327 |
- |
|
|