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 nl_putenv setlocale LC_MESSAGES);
28 use Koha::Cache::Memory::Lite;
30 use parent 'Exporter';
47 our $textdomain = 'Koha';
50 my $cache = Koha::Cache::Memory::Lite->get_instance();
51 my $cache_key = 'i18n:initialized';
52 unless ($cache->get_from_cache($cache_key)) {
53 my @system_locales = grep { chomp; not (/^C/ || $_ eq 'POSIX') } qx/locale -a/;
54 if (@system_locales) {
55 # LANG needs to be set to a valid locale,
56 # otherwise LANGUAGE is ignored
57 nl_putenv('LANG=' . $system_locales[0]);
58 setlocale(LC_MESSAGES, '');
60 my $langtag = C4::Languages::getlanguage;
61 my @subtags = split /-/, $langtag;
62 my ($language, $region) = @subtags;
63 if ($region && length $region == 4) {
64 $region = $subtags[2];
66 my $locale = $language;
68 $locale .= '_' . $region;
71 nl_putenv("LANGUAGE=$locale");
72 nl_putenv('OUTPUT_CHARSET=UTF-8');
74 my $directory = _base_directory();
75 textdomain($textdomain);
76 bindtextdomain($textdomain, $directory);
78 warn "No locale installed. Localization cannot work and is therefore disabled";
81 $cache->set_in_cache($cache_key, 1);
88 $msgid = Encode::encode_utf8($msgid);
90 return _gettext(\&gettext, [ $msgid ]);
94 my ($msgid, %vars) = @_;
96 $msgid = Encode::encode_utf8($msgid);
98 return _gettext(\&gettext, [ $msgid ], %vars);
102 my ($msgid, $msgid_plural, $count) = @_;
104 $msgid = Encode::encode_utf8($msgid);
105 $msgid_plural = Encode::encode_utf8($msgid_plural);
107 return _gettext(\&ngettext, [ $msgid, $msgid_plural, $count ]);
111 my ($msgid, $msgid_plural, $count, %vars) = @_;
113 $msgid = Encode::encode_utf8($msgid);
114 $msgid_plural = Encode::encode_utf8($msgid_plural);
116 return _gettext(\&ngettext, [ $msgid, $msgid_plural, $count ], %vars);
124 my ($msgctxt, $msgid) = @_;
126 $msgctxt = Encode::encode_utf8($msgctxt);
127 $msgid = Encode::encode_utf8($msgid);
129 return _gettext(\&pgettext, [ $msgctxt, $msgid ]);
133 my ($msgctxt, $msgid, %vars) = @_;
135 $msgctxt = Encode::encode_utf8($msgctxt);
136 $msgid = Encode::encode_utf8($msgid);
138 return _gettext(\&pgettext, [ $msgctxt, $msgid ], %vars);
142 my ($msgctxt, $msgid, $msgid_plural, $count) = @_;
144 $msgctxt = Encode::encode_utf8($msgctxt);
145 $msgid = Encode::encode_utf8($msgid);
146 $msgid_plural = Encode::encode_utf8($msgid_plural);
148 return _gettext(\&npgettext, [ $msgctxt, $msgid, $msgid_plural, $count ]);
152 my ($msgctxt, $msgid, $msgid_plural, $count, %vars) = @_;
154 $msgctxt = Encode::encode_utf8($msgctxt);
155 $msgid = Encode::encode_utf8($msgid);
156 $msgid_plural = Encode::encode_utf8($msgid_plural);
158 return _gettext(\&npgettext, [ $msgctxt, $msgid, $msgid_plural, $count], %vars);
177 sub _base_directory {
178 return C4::Context->config('intranetdir') . '/misc/translator/po';
182 my ($sub, $args, %vars) = @_;
186 my $text = Encode::decode_utf8($sub->(@$args));
188 $text = _expand($text, %vars);
195 my ($text, %vars) = @_;
197 my $re = join '|', map { quotemeta $_ } keys %vars;
198 $text =~ s/\{($re)\}/defined $vars{$1} ? $vars{$1} : "{$1}"/ge;