Funktionsnamensauflösung gefixt
[kivitendo-erp.git] / SL / Locale.pm
1 #====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (C) 1998-2002
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 # Contributors: Thomas Bayen <bayen@gmx.de>
16 #               Antti Kaihola <akaihola@siba.fi>
17 #               Moritz Bunkus (tex code)
18 #
19 # This program is free software; you can redistribute it and/or modify
20 # it under the terms of the GNU General Public License as published by
21 # the Free Software Foundation; either version 2 of the License, or
22 # (at your option) any later version.
23 #
24 # This program is distributed in the hope that it will be useful,
25 # but WITHOUT ANY WARRANTY; without even the implied warranty of
26 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27 # GNU General Public License for more details.
28 # You should have received a copy of the GNU General Public License
29 # along with this program; if not, write to the Free Software
30 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 #======================================================================
32 #
33 # Translations and number/date formatting
34 #
35 #======================================================================
36
37 package Locale;
38
39 use Encode;
40 use List::Util qw(first);
41 use List::MoreUtils qw(any);
42
43 use SL::LXDebug;
44 use SL::Common;
45 use SL::Iconv;
46 use SL::Inifile;
47
48 use strict;
49
50 sub new {
51   $main::lxdebug->enter_sub();
52
53   my ($type, $country) = @_;
54
55   $country ||= $::language;
56   $country   =~ s|.*/||;
57   $country   =~ s|\.||g;
58
59   my $self = {};
60   bless $self, $type;
61
62   $self->_init($country);
63
64   $main::lxdebug->leave_sub();
65
66   return $self;
67 }
68
69 sub _init {
70   my $self     = shift;
71   my $country  = shift;
72
73   $self->{charset}     = Common::DEFAULT_CHARSET;
74   $self->{countrycode} = $country;
75
76   if ($country && -d "locale/$country") {
77     local *IN;
78     if (open(IN, "<", "locale/$country/all")) {
79       my $code = join("", <IN>);
80       eval($code);
81       close(IN);
82     }
83
84     if (open IN, "<", "locale/$country/charset") {
85       $self->{charset} = <IN>;
86       close IN;
87
88       chomp $self->{charset};
89     }
90   }
91
92   my $db_charset            = $main::dbcharset || Common::DEFAULT_CHARSET;
93   $self->{is_utf8}          = (any { lc($::dbcharset || '') eq $_ } qw(utf8 utf-8 unicode)) ? 1 : 0;
94
95   if ($self->{is_utf8}) {
96     binmode STDOUT, ":utf8";
97     binmode STDERR, ":utf8";
98   }
99
100   $self->{iconv}            = SL::Iconv->new($self->{charset}, $db_charset);
101   $self->{iconv_reverse}    = SL::Iconv->new($db_charset,      $self->{charset});
102   $self->{iconv_english}    = SL::Iconv->new('ASCII',          $db_charset);
103   $self->{iconv_iso8859}    = SL::Iconv->new('ISO-8859-15',    $db_charset);
104   $self->{iconv_to_iso8859} = SL::Iconv->new($db_charset,      'ISO-8859-15');
105
106   $self->_read_special_chars_file($country);
107
108   push @{ $self->{LONG_MONTH} },
109     ("January",   "February", "March",    "April",
110      "May ",      "June",     "July",     "August",
111      "September", "October",  "November", "December");
112   push @{ $self->{SHORT_MONTH} },
113     (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
114 }
115
116 sub is_utf8 {
117   my $self   = shift;
118   my $handle = shift;
119   return $self->{is_utf8} && (!$handle || $handle->is_utf8);
120 }
121
122 sub _handle_markup {
123   my $self    = shift;
124   my $str     = shift;
125
126   my $escaped = 0;
127   my $new_str = '';
128
129   for (my $i = 0; $i < length $str; $i++) {
130     my $char = substr $str, $i, 1;
131
132     if ($escaped) {
133       if ($char eq 'n') {
134         $new_str .= "\n";
135
136       } elsif ($char eq 'r') {
137         $new_str .= "\r";
138
139       } elsif ($char eq 's') {
140         $new_str .= ' ';
141
142       } elsif ($char eq 'x') {
143         $new_str .= chr(hex(substr($str, $i + 1, 2)));
144         $i       += 2;
145
146       } else {
147         $new_str .= $char;
148       }
149
150       $escaped  = 0;
151
152     } elsif ($char eq '\\') {
153       $escaped  = 1;
154
155     } else {
156       $new_str .= $char;
157     }
158   }
159
160   return $new_str;
161 }
162
163 sub _read_special_chars_file {
164   my $self    = shift;
165   my $country = shift;
166
167   if (! -f "locale/$country/special_chars") {
168     $self->{special_chars_map} = {};
169     return;
170   }
171
172   $self->{special_chars_map} = Inifile->new("locale/$country/special_chars", 'verbatim' => 1);
173
174   foreach my $format (keys %{ $self->{special_chars_map} }) {
175     next if (($format eq 'FILE') || ($format eq 'ORDER') || (ref $self->{special_chars_map}->{$format} ne 'HASH'));
176
177     if ($format ne lc $format) {
178       $self->{special_chars_map}->{lc $format} = $self->{special_chars_map}->{$format};
179       delete $self->{special_chars_map}->{$format};
180       $format = lc $format;
181     }
182
183     my $scmap = $self->{special_chars_map}->{$format};
184     my $order = $self->{iconv}->convert($scmap->{order});
185     delete $scmap->{order};
186
187     foreach my $key (keys %{ $scmap }) {
188       $scmap->{$key} = $self->_handle_markup($self->{iconv}->convert($scmap->{$key}));
189
190       my $new_key    = $self->_handle_markup($self->{iconv}->convert($key));
191
192       if ($key ne $new_key) {
193         $scmap->{$new_key} = $scmap->{$key};
194         delete $scmap->{$key};
195       }
196     }
197
198     $self->{special_chars_map}->{"${format}-reverse"}          = { reverse %{ $scmap } };
199
200     $scmap->{order}                                            = [ map { $self->_handle_markup($_) } split m/\s+/, $order ];
201     $self->{special_chars_map}->{"${format}-reverse"}->{order} = [ grep { $_ } map { $scmap->{$_} } reverse @{ $scmap->{order} } ];
202   }
203 }
204
205 sub text {
206   my $self = shift;
207   my $text = shift;
208
209   if (exists $self->{texts}->{$text}) {
210     $text = $self->{iconv}->convert($self->{texts}->{$text});
211   } else {
212     $text = $self->{iconv_english}->convert($text);
213   }
214
215   if (@_) {
216     $text = Form->format_string($text, @_);
217   }
218
219   return $text;
220 }
221
222 sub findsub {
223   $main::lxdebug->enter_sub();
224
225   my ($self, $text) = @_;
226   my $text_rev      = lc $self->{iconv_reverse}->convert($text);
227
228   if (!$self->{texts_reverse}) {
229     $self->{texts_reverse} = { };
230     while (my ($original, $translation) = each %{ $self->{texts} }) {
231       $original    =  lc $original;
232       $original    =~ s/[^a-z0-9]/_/g;
233       $original    =~ s/_+/_/g;
234
235       $translation =  lc $translation;
236
237       $self->{texts_reverse}->{$translation} ||= [ ];
238       push @{ $self->{texts_reverse}->{$translation} }, $original;
239     }
240   }
241
242   my $sub_name;
243   $sub_name   = first { defined(&{ "::${_}" }) } @{ $self->{texts_reverse}->{$text_rev} } if $self->{texts_reverse}->{$text_rev};
244   $sub_name ||= $text_rev if ($text_rev =~ m/^[a-z][a-z0-9_]+$/) && defined &{ "::${text_rev}" };
245
246   $main::form->error("$text not defined in locale/$self->{countrycode}/all") if !$sub_name;
247
248   $main::lxdebug->leave_sub();
249
250   return $sub_name;
251 }
252
253 sub date {
254   $main::lxdebug->enter_sub();
255
256   my ($self, $myconfig, $date, $longformat) = @_;
257
258   my $longdate  = "";
259   my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
260
261   my ($spc, $yy, $mm, $dd);
262
263   if ($date) {
264
265     # get separator
266     $spc = $myconfig->{dateformat};
267     $spc =~ s/\w//g;
268     $spc = substr($spc, 1, 1);
269
270     if ($date =~ /\D/) {
271       if ($myconfig->{dateformat} =~ /^yy/) {
272         ($yy, $mm, $dd) = split /\D/, $date;
273       }
274       if ($myconfig->{dateformat} =~ /^mm/) {
275         ($mm, $dd, $yy) = split /\D/, $date;
276       }
277       if ($myconfig->{dateformat} =~ /^dd/) {
278         ($dd, $mm, $yy) = split /\D/, $date;
279       }
280     } else {
281       $date = substr($date, 2);
282       ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
283     }
284
285     $dd *= 1;
286     $mm--;
287     $yy = ($yy < 70) ? $yy + 2000 : $yy;
288     $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
289
290     if ($myconfig->{dateformat} =~ /^dd/) {
291       if (defined $longformat && $longformat == 0) {
292         $mm++;
293         $dd = "0$dd" if ($dd < 10);
294         $mm = "0$mm" if ($mm < 10);
295         $longdate = "$dd$spc$mm$spc$yy";
296       } else {
297         $longdate = "$dd";
298         $longdate .= ($spc eq '.') ? ". " : " ";
299         $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
300       }
301     } elsif ($myconfig->{dateformat} eq "yyyy-mm-dd") {
302
303       # Use German syntax with the ISO date style "yyyy-mm-dd" because
304       # Lx-Office is mainly used in Germany or German speaking countries.
305       if (defined $longformat && $longformat == 0) {
306         $mm++;
307         $dd = "0$dd" if ($dd < 10);
308         $mm = "0$mm" if ($mm < 10);
309         $longdate = "$yy-$mm-$dd";
310       } else {
311         $longdate = "$dd. ";
312         $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
313       }
314     } else {
315       if (defined $longformat && $longformat == 0) {
316         $mm++;
317         $dd = "0$dd" if ($dd < 10);
318         $mm = "0$mm" if ($mm < 10);
319         $longdate = "$mm$spc$dd$spc$yy";
320       } else {
321         $longdate = &text($self, $self->{$longmonth}[$mm]) . " $dd, $yy";
322       }
323     }
324
325   }
326
327   $main::lxdebug->leave_sub();
328
329   return $longdate;
330 }
331
332 sub parse_date {
333   $main::lxdebug->enter_sub();
334
335   my ($self, $myconfig, $date, $longformat) = @_;
336   my ($spc, $yy, $mm, $dd);
337
338   unless ($date) {
339     $main::lxdebug->leave_sub();
340     return ();
341   }
342
343   # get separator
344   $spc = $myconfig->{dateformat};
345   $spc =~ s/\w//g;
346   $spc = substr($spc, 1, 1);
347
348   if ($date =~ /\D/) {
349     if ($myconfig->{dateformat} =~ /^yy/) {
350       ($yy, $mm, $dd) = split /\D/, $date;
351     } elsif ($myconfig->{dateformat} =~ /^mm/) {
352       ($mm, $dd, $yy) = split /\D/, $date;
353     } elsif ($myconfig->{dateformat} =~ /^dd/) {
354       ($dd, $mm, $yy) = split /\D/, $date;
355     }
356   } else {
357     $date = substr($date, 2);
358     ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
359   }
360
361   $dd *= 1;
362   $mm *= 1;
363   $yy = ($yy < 70) ? $yy + 2000 : $yy;
364   $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
365
366   $main::lxdebug->leave_sub();
367   return ($yy, $mm, $dd);
368 }
369
370 sub reformat_date {
371   $main::lxdebug->enter_sub();
372
373   my ($self, $myconfig, $date, $output_format, $longformat) = @_;
374
375   $main::lxdebug->leave_sub() and return "" unless ($date);
376
377   my ($yy, $mm, $dd) = $self->parse_date($myconfig, $date);
378
379   $output_format =~ /d+/;
380   substr($output_format, $-[0], $+[0] - $-[0]) =
381     sprintf("%0" . (length($&)) . "d", $dd);
382
383   $output_format =~ /m+/;
384   substr($output_format, $-[0], $+[0] - $-[0]) =
385     sprintf("%0" . (length($&)) . "d", $mm);
386
387   $output_format =~ /y+/;
388   substr($output_format, $-[0], $+[0] - $-[0]) = $yy;
389
390   $main::lxdebug->leave_sub();
391
392   return $output_format;
393 }
394
395 sub quote_special_chars {
396   my $self   = shift;
397   my $format = lc shift;
398   my $string = shift;
399
400   if ($self->{special_chars_map} && $self->{special_chars_map}->{$format} && $self->{special_chars_map}->{$format}->{order}) {
401     my $scmap = $self->{special_chars_map}->{$format};
402
403     map { $string =~ s/\Q${_}\E/$scmap->{$_}/g } @{ $scmap->{order} };
404   }
405
406   return $string;
407 }
408
409 sub unquote_special_chars {
410   my $self    = shift;
411   my $format  = shift;
412
413   return $self->quote_special_chars("${format}-reverse", shift);
414 }
415
416 sub remap_special_chars {
417   my $self       = shift;
418   my $src_format = shift;
419   my $dst_format = shift;
420
421   return $self->quote_special_chars($dst_format, $self->quote_special_chars("${src_format}-reverse", shift));
422 }
423
424 sub with_raw_io {
425   my $self = shift;
426   my $fh   = shift;
427   my $code = shift;
428
429   binmode $fh, ":raw";
430   $code->();
431   binmode $fh, ":utf8" if $self->is_utf8;
432 }
433
434 1;