26
26
@ISA = qw(Exporter);
27
@EXPORT = qw(&gettext);
32
use POSIX qw (setlocale);
33
use Locale::Messages qw (:locale_h :libintl_h !gettext);
36
## Using the Pure Perl implementation of gettext
37
## This is required on Solaris : native implementation of gettext does not map ll_CC with ll
38
Locale::Messages->select_package ('gettext_pp');
33
41
my %msghash; # Hash organization is like Messages file: File>>Sections>>Messages
34
42
my %set_comment; #sets-of-messages comment
44
## The lang is the NLS catalogue name ; locale is the locale preference
45
## Ex: lang = fr ; locale = fr_FR
46
my ($current_lang, $current_locale, @previous_locale);
40
sub GetHash { return %msghash;}
44
#To Load all files in MsgHash
47
&do_log('debug', "Language::LoadLang(%s)", $msgdir);
49
unless (-d $msgdir && -r $msgdir){
51
&do_log('err','Cannot read Locale directory %s', $msgdir);
55
unless (opendir MSGDIR, $msgdir) {
56
&do_log('err','Unable to open directory %s', $msgdir);
60
foreach my $file (grep /\.msg$/, readdir(MSGDIR)) {
62
$file =~ /^([\w-]+)\.msg$/;
66
unless (Msg_file_open($msgdir.'/'.$file,$lang_name)) {
67
&do_log('err','Error while calling Msg_file_Open(%s, %s)', $msgdir.'/'.$file,$lang_name);
80
my $msgfile = shift; #Messages File name
81
my $lang = shift; #Language
82
my $set_num; # Section Number
83
my $msg_num; # Message Number in Section
84
my $msg_value; # Message content
88
&do_log('debug', 'Loading locale file %s version', $msgfile);
89
unless (-r $msgfile) { #check if file exists
90
&do_log('err','Cannot read file %s', $msgfile);
94
unless (open(MSGFILE,$msgfile)) {
95
&do_log('err','Cannot open message File %s', $msgfile);
101
my $current_line = $_;
102
chomp($current_line);
104
next if ($current_line =~ /^\s*$/) || ($current_line =~ /^\$(\s+|quote)/); # for empty or comments Lines
106
if ($current_line =~ /^\$set\s+(\d+)\s+(.+)$/i){ # When it's a Section-separation Line
108
$set_comment{$set_num} = $2;
110
}elsif ($current_line =~ /^(\d+)\s+\"(.*)(\\|\")\s*$/i){ # When it's a Begin of Message
112
$msghash{$lang}{$set_num}{$msg_num} = $2;
114
}elsif ($current_line =~ /^(.+)(\\|\")\s*$/i){ # When it's the follow or End of a message
115
$msghash{$lang}{$set_num}{$msg_num} .= $1;
118
$msghash{$lang}{$set_num}{$msg_num} =~ s/(\\n)/\n/g; #some sequences need to be substitute:
119
$msghash{$lang}{$set_num}{$msg_num} =~ s/(\\t)/\t/g; # \n, \t
120
$msghash{$lang}{$set_num}{$msg_num} =~ s/(\\\\)/\\/g;
48
## This was the old style locale naming, used for templates, nls, scenario
49
my %language_equiv = ( 'zh_CN' => 'cn',
55
## Supported languages are defined by 'supported_lang' sympa.conf parameter
57
my %lang2locale = ('cz' => 'cs_CZ',
80
## Used to perform setlocale on FreeBSD / Solaris
81
my %locale2charset = ('cs_CZ' => 'utf-8',
82
'de_DE' => 'iso8859-1',
85
'es_ES' => 'iso8859-1',
86
'et_EE' => 'iso8859-4',
87
'fi_FI' => 'iso8859-1',
88
'fr_FR' => 'iso8859-1',
89
'hu_HU' => 'iso8859-2',
90
'it_IT' => 'iso8859-1',
93
'nl_NL' => 'iso8859-1',
94
'oc_FR' => 'iso8859-1',
95
'pl_PL' => 'iso8859-2',
97
'pt_PT' => 'iso8859-1',
98
'ro_RO' => 'iso8859-2',
107
sub GetSupportedLanguages {
111
foreach my $l (split /,/,&Conf::get_robot_conf($robot, 'supported_lang')) {
112
push @lang_list, $lang2locale{$l}||$l;
117
## Keep the previous lang ; can be restored with PopLang
120
&do_log('debug', 'Language::PushLang(%s)', $locale);
122
push @previous_locale, $current_locale;
129
&do_log('debug', 'Language::PopLang(%s)');
131
my $locale = pop @previous_locale;
132
&do_log('debug3', 'Language::SetLang(%s)', $lang);
140
&do_log('debug2', 'Language::SetLang(%s)', $locale);
135
145
&do_log('err','Language::SetLang(), missing locale parameter');
139
unless (defined ($msghash{$lang})) {
140
&do_log('err','unknown Locale %s, maybe sub LoadLang not Loaded before', $lang);
149
if (length($lang) == 2) {
150
$locale = $lang2locale{$lang};
152
## uppercase the country part if needed
153
my @items = split /_/, $locale;
154
$items[1] = uc($items[1]);
155
$locale = join '_', @items;
157
## Get the NLS equivalent for the lang
158
$lang = &Locale2Lang($locale);
161
## Set Locale::Messages context
162
my $locale_dashless = $locale.'.'.$locale2charset{$locale};
163
$locale_dashless =~ s/-//g;
164
foreach my $type (&POSIX::LC_ALL, &POSIX::LC_TIME) {
166
foreach my $try ($locale.'.'.$locale2charset{$locale},
167
$locale.'.'.uc($locale2charset{$locale}), ## UpperCase required for FreeBSD
168
$locale_dashless, ## Required on HPUX
172
if (&setlocale($type, $try)) {
178
&do_log('err','Failed to setlocale(%s) ; you either have a problem with the catalogue .mo files or you should extend available locales in your /etc/locale.gen (or /etc/sysconfig/i18n) file', $locale);
183
## Define what catalog is used
184
&Locale::Messages::textdomain("sympa");
185
&Locale::Messages::bindtextdomain('sympa','--LOCALEDIR--');
186
&Locale::Messages::bind_textdomain_codeset('sympa',$recode) if $recode;
187
#bind_textdomain_codeset sympa => 'iso-8859-1';
144
189
$current_lang = $lang;
146
&POSIX::setlocale(&POSIX::LC_ALL, Msg(14, 1, 'en_US'));
190
$current_locale = $locale;
156
my $msg_default = shift;
158
unless (defined($msghash{$current_lang}{$set}{$msg})) {
160
&do_log('info','%s-Message %d of set %d not found, using user Message : %s ',$current_lang, $msg, $set, $msg_default);
163
return $msghash{$current_lang}{$set}{$msg};
170
202
return $current_lang;
209
if (defined $language_equiv{$locale}) {
210
$lang = $language_equiv{$locale};
212
## remove the country part
214
$lang =~ s/_\w{2}$//;
223
return $lang2locale{$lang} || $lang;
229
# &do_log('notice','Maketext: %s', $msg);
231
my $translation = &gettext ($msg);
233
## replace parameters in string
234
$translation =~ s/\%\%/'_ESCAPED_'.'%_'/eg; ## First escape '%%'
235
$translation =~ s/\%(\d+)/$_[$1-1]/eg;
236
$translation =~ s/_ESCAPED_%\_/'%'/eg; ## Unescape '%%'
242
&do_log('debug3', 'Language::gettext(%s)', $_[0]);
244
## This prevents meta information to be returned if the string to translate is empty
248
## return meta information on the catalogue (language, charset, encoding,...)
249
}elsif ($_[0] =~ '^_(\w+)_$') {
251
foreach (split /\n/,&Locale::Messages::gettext('')) {
252
if ($var eq 'language') {
253
if (/^Language-Team:\s*(.+)$/i) {
255
$language =~ s/\<\S+\>//;
258
}elsif ($var eq 'charset') {
261
} elsif (/^Content-Type:\s*.*charset=(\S+)$/i) {
264
}elsif ($var eq 'encoding') {
265
if (/^Content-Transfer-Encoding:\s*(.+)$/i) {
273
&Locale::Messages::gettext(@_);