3 # This file is part of Koha.
5 # Copyright 2012-2014 BibLibre
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.
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.
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>.
27 use Locale
::Messages
qw(:locale_h LC_MESSAGES);
28 use POSIX
qw( setlocale );
29 use Koha
::Cache
::Memory
::Lite
;
31 use parent
'Exporter';
48 our $textdomain = 'Koha';
51 my $cache = Koha
::Cache
::Memory
::Lite
->get_instance();
52 my $cache_key = 'i18n:initialized';
53 unless ($cache->get_from_cache($cache_key)) {
54 my @system_locales = grep { chomp; not (/^C/ || $_ eq 'POSIX') } qx/locale -a/;
55 if (@system_locales) {
56 # LANG needs to be set to a valid locale,
57 # otherwise LANGUAGE is ignored
58 $ENV{LANG
} = $system_locales[0];
59 POSIX
::setlocale
(LC_MESSAGES
, '');
61 my $langtag = C4
::Languages
::getlanguage
;
62 my @subtags = split /-/, $langtag;
63 my ($language, $region) = @subtags;
64 if ($region && length $region == 4) {
65 $region = $subtags[2];
67 my $locale = $language;
69 $locale .= '_' . $region;
72 $ENV{LANGUAGE
} = $locale;
73 $ENV{OUTPUT_CHARSET
} = 'UTF-8';
75 my $directory = _base_directory
();
76 textdomain
($textdomain);
77 bindtextdomain
($textdomain, $directory);
79 warn "No locale installed. Localization cannot work and is therefore disabled";
82 $cache->set_in_cache($cache_key, 1);
89 $msgid = Encode
::encode_utf8
($msgid);
91 return _gettext
(\
&gettext
, [ $msgid ]);
95 my ($msgid, %vars) = @_;
97 $msgid = Encode
::encode_utf8
($msgid);
99 return _gettext
(\
&gettext
, [ $msgid ], %vars);
103 my ($msgid, $msgid_plural, $count) = @_;
105 $msgid = Encode
::encode_utf8
($msgid);
106 $msgid_plural = Encode
::encode_utf8
($msgid_plural);
108 return _gettext
(\
&ngettext
, [ $msgid, $msgid_plural, $count ]);
112 my ($msgid, $msgid_plural, $count, %vars) = @_;
114 $msgid = Encode
::encode_utf8
($msgid);
115 $msgid_plural = Encode
::encode_utf8
($msgid_plural);
117 return _gettext
(\
&ngettext
, [ $msgid, $msgid_plural, $count ], %vars);
125 my ($msgctxt, $msgid) = @_;
127 $msgctxt = Encode
::encode_utf8
($msgctxt);
128 $msgid = Encode
::encode_utf8
($msgid);
130 return _gettext
(\
&pgettext
, [ $msgctxt, $msgid ]);
134 my ($msgctxt, $msgid, %vars) = @_;
136 $msgctxt = Encode
::encode_utf8
($msgctxt);
137 $msgid = Encode
::encode_utf8
($msgid);
139 return _gettext
(\
&pgettext
, [ $msgctxt, $msgid ], %vars);
143 my ($msgctxt, $msgid, $msgid_plural, $count) = @_;
145 $msgctxt = Encode
::encode_utf8
($msgctxt);
146 $msgid = Encode
::encode_utf8
($msgid);
147 $msgid_plural = Encode
::encode_utf8
($msgid_plural);
149 return _gettext
(\
&npgettext
, [ $msgctxt, $msgid, $msgid_plural, $count ]);
153 my ($msgctxt, $msgid, $msgid_plural, $count, %vars) = @_;
155 $msgctxt = Encode
::encode_utf8
($msgctxt);
156 $msgid = Encode
::encode_utf8
($msgid);
157 $msgid_plural = Encode
::encode_utf8
($msgid_plural);
159 return _gettext
(\
&npgettext
, [ $msgctxt, $msgid, $msgid_plural, $count], %vars);
178 sub _base_directory
{
179 return C4
::Context
->config('intranetdir') . '/misc/translator/po';
183 my ($sub, $args, %vars) = @_;
187 my $text = Encode
::decode_utf8
($sub->(@
$args));
189 $text = _expand
($text, %vars);
196 my ($text, %vars) = @_;
198 my $re = join '|', map { quotemeta $_ } keys %vars;
199 $text =~ s/\{($re)\}/defined $vars{$1} ? $vars{$1} : "{$1}"/ge;