|
Lines 26-31
use C4::Context;
Link Here
|
| 26 |
use Encode; |
26 |
use Encode; |
| 27 |
use Locale::Util qw(set_locale); |
27 |
use Locale::Util qw(set_locale); |
| 28 |
use Locale::Messages qw(:locale_h :libintl_h nl_putenv); |
28 |
use Locale::Messages qw(:locale_h :libintl_h nl_putenv); |
|
|
29 |
use Koha::Cache::Memory::Lite; |
| 29 |
|
30 |
|
| 30 |
use parent 'Exporter'; |
31 |
use parent 'Exporter'; |
| 31 |
our @EXPORT = qw( |
32 |
our @EXPORT = qw( |
|
Lines 44-98
our @EXPORT = qw(
Link Here
|
| 44 |
N__np |
45 |
N__np |
| 45 |
); |
46 |
); |
| 46 |
|
47 |
|
| 47 |
my $textdomain; |
48 |
our $textdomain = 'Koha'; |
|
|
49 |
|
| 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 $langtag = C4::Languages::getlanguage; |
| 55 |
my @subtags = split /-/, $langtag; |
| 56 |
my ($language, $region) = @subtags; |
| 57 |
if ($region && length $region == 4) { |
| 58 |
$region = $subtags[2]; |
| 59 |
} |
| 60 |
my $locale = set_locale(LC_ALL, $language, $region, 'utf-8'); |
| 61 |
unless ($locale) { |
| 62 |
set_locale(LC_MESSAGES, ''); |
| 63 |
Locale::Messages->select_package('gettext_pp'); |
| 64 |
$locale = $language; |
| 65 |
if ($region) { |
| 66 |
$locale .= '_' . $region; |
| 67 |
} |
| 68 |
nl_putenv("LANGUAGE=$locale"); |
| 69 |
nl_putenv("LANG=$locale"); |
| 70 |
nl_putenv('OUTPUT_CHARSET=utf-8'); |
| 71 |
} |
| 48 |
|
72 |
|
| 49 |
BEGIN { |
73 |
my $directory = _base_directory(); |
| 50 |
$textdomain = 'Koha'; |
74 |
textdomain($textdomain); |
|
|
75 |
bindtextdomain($textdomain, $directory); |
| 51 |
|
76 |
|
| 52 |
my $langtag = C4::Languages::getlanguage; |
77 |
$cache->set_in_cache($cache_key, 1); |
| 53 |
my @subtags = split /-/, $langtag; |
|
|
| 54 |
my ($language, $region) = @subtags; |
| 55 |
if ($region && length $region == 4) { |
| 56 |
$region = $subtags[2]; |
| 57 |
} |
| 58 |
my $locale = set_locale(LC_ALL, $language, $region, 'utf-8'); |
| 59 |
unless ($locale) { |
| 60 |
set_locale(LC_MESSAGES, 'C'); |
| 61 |
Locale::Messages->select_package('gettext_pp'); |
| 62 |
$locale = $language; |
| 63 |
if ($region) { |
| 64 |
$locale .= '_' . $region; |
| 65 |
} |
| 66 |
nl_putenv("LANGUAGE=$locale"); |
| 67 |
nl_putenv("LANG=$locale"); |
| 68 |
nl_putenv('OUTPUT_CHARSET=utf-8'); |
| 69 |
} |
78 |
} |
| 70 |
|
|
|
| 71 |
my $directory = C4::Context->config('intranetdir') . '/misc/translator/po'; |
| 72 |
textdomain($textdomain); |
| 73 |
bindtextdomain($textdomain, $directory); |
| 74 |
} |
79 |
} |
| 75 |
|
80 |
|
| 76 |
sub __ { |
81 |
sub __ { |
| 77 |
my ($msgid) = @_; |
82 |
my ($msgid) = @_; |
| 78 |
my $text = dgettext($textdomain, $msgid); |
83 |
|
| 79 |
return __decode($text); |
84 |
return _gettext(\&gettext, [ $msgid ]); |
| 80 |
} |
85 |
} |
| 81 |
|
86 |
|
| 82 |
sub __x { |
87 |
sub __x { |
| 83 |
my ($msgid, %vars) = @_; |
88 |
my ($msgid, %vars) = @_; |
| 84 |
return __expand(__($msgid), %vars); |
89 |
|
|
|
90 |
return _gettext(\&gettext, [ $msgid ], %vars); |
| 85 |
} |
91 |
} |
| 86 |
|
92 |
|
| 87 |
sub __n { |
93 |
sub __n { |
| 88 |
my ($msgid, $msgid_plural, $count) = @_; |
94 |
my ($msgid, $msgid_plural, $count) = @_; |
| 89 |
my $text = dngettext($textdomain, $msgid, $msgid_plural, $count); |
95 |
|
| 90 |
return __decode($text); |
96 |
return _gettext(\&ngettext, [ $msgid, $msgid_plural, $count ]); |
| 91 |
} |
97 |
} |
| 92 |
|
98 |
|
| 93 |
sub __nx { |
99 |
sub __nx { |
| 94 |
my ($msgid, $msgid_plural, $count, %vars) = @_; |
100 |
my ($msgid, $msgid_plural, $count, %vars) = @_; |
| 95 |
return __expand(__n($msgid, $msgid_plural, $count), %vars); |
101 |
|
|
|
102 |
return _gettext(\&ngettext, [ $msgid, $msgid_plural, $count ], %vars); |
| 96 |
} |
103 |
} |
| 97 |
|
104 |
|
| 98 |
sub __xn { |
105 |
sub __xn { |
|
Lines 101-124
sub __xn {
Link Here
|
| 101 |
|
108 |
|
| 102 |
sub __p { |
109 |
sub __p { |
| 103 |
my ($msgctxt, $msgid) = @_; |
110 |
my ($msgctxt, $msgid) = @_; |
| 104 |
my $text = dpgettext($textdomain, $msgctxt, $msgid); |
111 |
|
| 105 |
return __decode($text); |
112 |
return _gettext(\&pgettext, [ $msgctxt, $msgid ]); |
| 106 |
} |
113 |
} |
| 107 |
|
114 |
|
| 108 |
sub __px { |
115 |
sub __px { |
| 109 |
my ($msgctxt, $msgid, %vars) = @_; |
116 |
my ($msgctxt, $msgid, %vars) = @_; |
| 110 |
return __expand(__p($msgctxt, $msgid), %vars); |
117 |
|
|
|
118 |
return _gettext(\&pgettext, [ $msgctxt, $msgid ], %vars); |
| 111 |
} |
119 |
} |
| 112 |
|
120 |
|
| 113 |
sub __np { |
121 |
sub __np { |
| 114 |
my ($msgctxt, $msgid, $msgid_plural, $count) = @_; |
122 |
my ($msgctxt, $msgid, $msgid_plural, $count) = @_; |
| 115 |
my $text = dnpgettext($textdomain, $msgctxt, $msgid, $msgid_plural, $count); |
123 |
|
| 116 |
return __decode($text); |
124 |
return _gettext(\&npgettext, [ $msgctxt, $msgid, $msgid_plural, $count ]); |
| 117 |
} |
125 |
} |
| 118 |
|
126 |
|
| 119 |
sub __npx { |
127 |
sub __npx { |
| 120 |
my ($msgctxt, $msgid, $msgid_plural, $count, %vars) = @_; |
128 |
my ($msgctxt, $msgid, $msgid_plural, $count, %vars) = @_; |
| 121 |
return __expand(__np($msgctxt, $msgid, $msgid_plural, $count), %vars); |
129 |
|
|
|
130 |
return _gettext(\&npgettext, [ $msgctxt, $msgid, $msgid_plural, $count], %vars); |
| 122 |
} |
131 |
} |
| 123 |
|
132 |
|
| 124 |
sub N__ { |
133 |
sub N__ { |
|
Lines 137-143
sub N__np {
Link Here
|
| 137 |
return @_; |
146 |
return @_; |
| 138 |
} |
147 |
} |
| 139 |
|
148 |
|
| 140 |
sub __expand { |
149 |
sub _base_directory { |
|
|
150 |
return C4::Context->config('intranetdir') . '/misc/translator/po'; |
| 151 |
} |
| 152 |
|
| 153 |
sub _gettext { |
| 154 |
my ($sub, $args, %vars) = @_; |
| 155 |
|
| 156 |
init(); |
| 157 |
|
| 158 |
my $text = Encode::decode_utf8($sub->(@$args)); |
| 159 |
if (%vars) { |
| 160 |
$text = _expand($text, %vars); |
| 161 |
} |
| 162 |
|
| 163 |
return $text; |
| 164 |
} |
| 165 |
|
| 166 |
sub _expand { |
| 141 |
my ($text, %vars) = @_; |
167 |
my ($text, %vars) = @_; |
| 142 |
|
168 |
|
| 143 |
my $re = join '|', map { quotemeta $_ } keys %vars; |
169 |
my $re = join '|', map { quotemeta $_ } keys %vars; |
|
Lines 146-153
sub __expand {
Link Here
|
| 146 |
return $text; |
172 |
return $text; |
| 147 |
} |
173 |
} |
| 148 |
|
174 |
|
| 149 |
sub __decode { |
|
|
| 150 |
return Encode::decode_utf8(shift); |
| 151 |
} |
| 152 |
|
| 153 |
1; |
175 |
1; |