Bug 20254: Set correct member in working_directory
[koha.git] / Koha / I18N.pm
blob72062239d846f29c12019245b2a42f60c4d18618
1 package Koha::I18N;
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>.
20 use Modern::Perl;
22 use CGI;
23 use C4::Languages;
24 use C4::Context;
26 use Encode;
27 use Locale::Messages qw(:locale_h LC_MESSAGES);
28 use POSIX qw( setlocale );
29 use Koha::Cache::Memory::Lite;
31 use parent 'Exporter';
32 our @EXPORT = qw(
34 __x
35 __n
36 __nx
37 __xn
38 __p
39 __px
40 __np
41 __npx
42 N__
43 N__n
44 N__p
45 N__np
48 our $textdomain = 'Koha';
50 sub init {
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;
68 if ($region) {
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);
78 } else {
79 warn "No locale installed. Localization cannot work and is therefore disabled";
82 $cache->set_in_cache($cache_key, 1);
86 sub __ {
87 my ($msgid) = @_;
89 $msgid = Encode::encode_utf8($msgid);
91 return _gettext(\&gettext, [ $msgid ]);
94 sub __x {
95 my ($msgid, %vars) = @_;
97 $msgid = Encode::encode_utf8($msgid);
99 return _gettext(\&gettext, [ $msgid ], %vars);
102 sub __n {
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 ]);
111 sub __nx {
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);
120 sub __xn {
121 return __nx(@_);
124 sub __p {
125 my ($msgctxt, $msgid) = @_;
127 $msgctxt = Encode::encode_utf8($msgctxt);
128 $msgid = Encode::encode_utf8($msgid);
130 return _gettext(\&pgettext, [ $msgctxt, $msgid ]);
133 sub __px {
134 my ($msgctxt, $msgid, %vars) = @_;
136 $msgctxt = Encode::encode_utf8($msgctxt);
137 $msgid = Encode::encode_utf8($msgid);
139 return _gettext(\&pgettext, [ $msgctxt, $msgid ], %vars);
142 sub __np {
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 ]);
152 sub __npx {
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);
162 sub N__ {
163 return @_;
166 sub N__n {
167 return @_;
170 sub N__p {
171 return @_;
174 sub N__np {
175 return @_;
178 sub _base_directory {
179 return C4::Context->config('intranetdir') . '/misc/translator/po';
182 sub _gettext {
183 my ($sub, $args, %vars) = @_;
185 init();
187 my $text = Encode::decode_utf8($sub->(@$args));
188 if (%vars) {
189 $text = _expand($text, %vars);
192 return $text;
195 sub _expand {
196 my ($text, %vars) = @_;
198 my $re = join '|', map { quotemeta $_ } keys %vars;
199 $text =~ s/\{($re)\}/defined $vars{$1} ? $vars{$1} : "{$1}"/ge;
201 return $text;