Zahlen beim CSV-Export ohne Tausendertrennzeichen formatieren
[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 DateTime;
40 use Encode;
41 use List::Util qw(first);
42 use List::MoreUtils qw(any);
43
44 use SL::LXDebug;
45 use SL::Common;
46 use SL::Iconv;
47 use SL::Inifile;
48
49 use strict;
50
51 my %locales_by_country;
52
53 sub new {
54   $main::lxdebug->enter_sub();
55
56   my ($type, $country) = @_;
57
58   $country ||= $::language;
59   $country   =~ s|.*/||;
60   $country   =~ s|\.||g;
61
62   if (!$locales_by_country{$country}) {
63     my $self = {};
64     bless $self, $type;
65
66     $self->_init($country);
67
68     $locales_by_country{$country} = $self;
69   }
70
71   $main::lxdebug->leave_sub();
72
73   return $locales_by_country{$country}
74 }
75
76 sub _init {
77   my $self     = shift;
78   my $country  = shift;
79
80   $self->{charset}     = Common::DEFAULT_CHARSET;
81   $self->{countrycode} = $country;
82
83   if ($country && -d "locale/$country") {
84     local *IN;
85     if (open(IN, "<", "locale/$country/all")) {
86       my $code = join("", <IN>);
87       eval($code);
88       close(IN);
89     }
90
91     if (open IN, "<", "locale/$country/charset") {
92       $self->{charset} = <IN>;
93       close IN;
94
95       chomp $self->{charset};
96     }
97   }
98
99   my $db_charset            = $main::dbcharset || Common::DEFAULT_CHARSET;
100   $self->{is_utf8}          = (any { lc($::dbcharset || '') eq $_ } qw(utf8 utf-8 unicode)) ? 1 : 0;
101
102   if ($self->{is_utf8}) {
103     binmode STDOUT, ":utf8";
104     binmode STDERR, ":utf8";
105   }
106
107   $self->{iconv}            = SL::Iconv->new($self->{charset}, $db_charset);
108   $self->{iconv_reverse}    = SL::Iconv->new($db_charset,      $self->{charset});
109   $self->{iconv_english}    = SL::Iconv->new('ASCII',          $db_charset);
110   $self->{iconv_iso8859}    = SL::Iconv->new('ISO-8859-15',    $db_charset);
111   $self->{iconv_to_iso8859} = SL::Iconv->new($db_charset,      'ISO-8859-15');
112
113   $self->_read_special_chars_file($country);
114
115   push @{ $self->{LONG_MONTH} },
116     ("January",   "February", "March",    "April",
117      "May ",      "June",     "July",     "August",
118      "September", "October",  "November", "December");
119   push @{ $self->{SHORT_MONTH} },
120     (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
121 }
122
123 sub is_utf8 {
124   my $self   = shift;
125   my $handle = shift;
126   return $self->{is_utf8} && (!$handle || $handle->is_utf8);
127 }
128
129 sub _handle_markup {
130   my $self    = shift;
131   my $str     = shift;
132
133   my $escaped = 0;
134   my $new_str = '';
135
136   for (my $i = 0; $i < length $str; $i++) {
137     my $char = substr $str, $i, 1;
138
139     if ($escaped) {
140       if ($char eq 'n') {
141         $new_str .= "\n";
142
143       } elsif ($char eq 'r') {
144         $new_str .= "\r";
145
146       } elsif ($char eq 's') {
147         $new_str .= ' ';
148
149       } elsif ($char eq 'x') {
150         $new_str .= chr(hex(substr($str, $i + 1, 2)));
151         $i       += 2;
152
153       } else {
154         $new_str .= $char;
155       }
156
157       $escaped  = 0;
158
159     } elsif ($char eq '\\') {
160       $escaped  = 1;
161
162     } else {
163       $new_str .= $char;
164     }
165   }
166
167   return $new_str;
168 }
169
170 sub _read_special_chars_file {
171   my $self    = shift;
172   my $country = shift;
173
174   if (! -f "locale/$country/special_chars") {
175     $self->{special_chars_map} = {};
176     return;
177   }
178
179   $self->{special_chars_map} = Inifile->new("locale/$country/special_chars", 'verbatim' => 1);
180
181   foreach my $format (keys %{ $self->{special_chars_map} }) {
182     next if (($format eq 'FILE') || ($format eq 'ORDER') || (ref $self->{special_chars_map}->{$format} ne 'HASH'));
183
184     if ($format ne lc $format) {
185       $self->{special_chars_map}->{lc $format} = $self->{special_chars_map}->{$format};
186       delete $self->{special_chars_map}->{$format};
187       $format = lc $format;
188     }
189
190     my $scmap = $self->{special_chars_map}->{$format};
191     my $order = $self->{iconv}->convert($scmap->{order});
192     delete $scmap->{order};
193
194     foreach my $key (keys %{ $scmap }) {
195       $scmap->{$key} = $self->_handle_markup($self->{iconv}->convert($scmap->{$key}));
196
197       my $new_key    = $self->_handle_markup($self->{iconv}->convert($key));
198
199       if ($key ne $new_key) {
200         $scmap->{$new_key} = $scmap->{$key};
201         delete $scmap->{$key};
202       }
203     }
204
205     $self->{special_chars_map}->{"${format}-reverse"}          = { reverse %{ $scmap } };
206
207     $scmap->{order}                                            = [ map { $self->_handle_markup($_) } split m/\s+/, $order ];
208     $self->{special_chars_map}->{"${format}-reverse"}->{order} = [ grep { $_ } map { $scmap->{$_} } reverse @{ $scmap->{order} } ];
209   }
210 }
211
212 sub text {
213   my $self = shift;
214   my $text = shift;
215
216   if ($self->{texts}->{$text}) {
217     $text = $self->{iconv}->convert($self->{texts}->{$text});
218   } else {
219     $text = $self->{iconv_english}->convert($text);
220   }
221
222   if (@_) {
223     $text = Form->format_string($text, @_);
224   }
225
226   return $text;
227 }
228
229 sub findsub {
230   $main::lxdebug->enter_sub();
231
232   my ($self, $text) = @_;
233   my $text_rev      = lc $self->{iconv_reverse}->convert($text);
234
235   if (!$self->{texts_reverse}) {
236     $self->{texts_reverse} = { };
237     while (my ($original, $translation) = each %{ $self->{texts} }) {
238       $original    =  lc $original;
239       $original    =~ s/[^a-z0-9]/_/g;
240       $original    =~ s/_+/_/g;
241
242       $translation =  lc $translation;
243       $translation =~ s/\s+/_/g;
244
245       $self->{texts_reverse}->{$translation} ||= [ ];
246       push @{ $self->{texts_reverse}->{$translation} }, $original;
247     }
248   }
249
250   my $sub_name;
251   $sub_name   = first { defined(&{ "::${_}" }) } @{ $self->{texts_reverse}->{$text_rev} } if $self->{texts_reverse}->{$text_rev};
252   $sub_name ||= $text_rev if ($text_rev =~ m/^[a-z][a-z0-9_]+$/) && defined &{ "::${text_rev}" };
253
254   $main::form->error("$text not defined in locale/$self->{countrycode}/all") if !$sub_name;
255
256   $main::lxdebug->leave_sub();
257
258   return $sub_name;
259 }
260
261 sub date {
262   $main::lxdebug->enter_sub();
263
264   my ($self, $myconfig, $date, $longformat) = @_;
265
266   my $longdate  = "";
267   my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
268
269   my ($spc, $yy, $mm, $dd);
270
271   if ($date) {
272
273     # get separator
274     $spc = $myconfig->{dateformat};
275     $spc =~ s/\w//g;
276     $spc = substr($spc, 1, 1);
277
278     if ($date =~ /\D/) {
279       if ($myconfig->{dateformat} =~ /^yy/) {
280         ($yy, $mm, $dd) = split /\D/, $date;
281       }
282       if ($myconfig->{dateformat} =~ /^mm/) {
283         ($mm, $dd, $yy) = split /\D/, $date;
284       }
285       if ($myconfig->{dateformat} =~ /^dd/) {
286         ($dd, $mm, $yy) = split /\D/, $date;
287       }
288     } else {
289       $date = substr($date, 2);
290       ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
291     }
292
293     $dd *= 1;
294     $mm--;
295     $yy = ($yy < 70) ? $yy + 2000 : $yy;
296     $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
297
298     if ($myconfig->{dateformat} =~ /^dd/) {
299       if (defined $longformat && $longformat == 0) {
300         $mm++;
301         $dd = "0$dd" if ($dd < 10);
302         $mm = "0$mm" if ($mm < 10);
303         $longdate = "$dd$spc$mm$spc$yy";
304       } else {
305         $longdate = "$dd";
306         $longdate .= ($spc eq '.') ? ". " : " ";
307         $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
308       }
309     } elsif ($myconfig->{dateformat} eq "yyyy-mm-dd") {
310
311       # Use German syntax with the ISO date style "yyyy-mm-dd" because
312       # Lx-Office is mainly used in Germany or German speaking countries.
313       if (defined $longformat && $longformat == 0) {
314         $mm++;
315         $dd = "0$dd" if ($dd < 10);
316         $mm = "0$mm" if ($mm < 10);
317         $longdate = "$yy-$mm-$dd";
318       } else {
319         $longdate = "$dd. ";
320         $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
321       }
322     } else {
323       if (defined $longformat && $longformat == 0) {
324         $mm++;
325         $dd = "0$dd" if ($dd < 10);
326         $mm = "0$mm" if ($mm < 10);
327         $longdate = "$mm$spc$dd$spc$yy";
328       } else {
329         $longdate = &text($self, $self->{$longmonth}[$mm]) . " $dd, $yy";
330       }
331     }
332
333   }
334
335   $main::lxdebug->leave_sub();
336
337   return $longdate;
338 }
339
340 sub parse_date {
341   $main::lxdebug->enter_sub();
342
343   my ($self, $myconfig, $date, $longformat) = @_;
344   my ($spc, $yy, $mm, $dd);
345
346   unless ($date) {
347     $main::lxdebug->leave_sub();
348     return ();
349   }
350
351   # get separator
352   $spc = $myconfig->{dateformat};
353   $spc =~ s/\w//g;
354   $spc = substr($spc, 1, 1);
355
356   if ($date =~ /\D/) {
357     if ($myconfig->{dateformat} =~ /^yy/) {
358       ($yy, $mm, $dd) = split /\D/, $date;
359     } elsif ($myconfig->{dateformat} =~ /^mm/) {
360       ($mm, $dd, $yy) = split /\D/, $date;
361     } elsif ($myconfig->{dateformat} =~ /^dd/) {
362       ($dd, $mm, $yy) = split /\D/, $date;
363     }
364   } else {
365     $date = substr($date, 2);
366     ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
367   }
368
369   $dd *= 1;
370   $mm *= 1;
371   $yy = ($yy < 70) ? $yy + 2000 : $yy;
372   $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
373
374   $main::lxdebug->leave_sub();
375   return ($yy, $mm, $dd);
376 }
377
378 sub parse_date_to_object {
379   my $self           = shift;
380   my ($yy, $mm, $dd) = $self->parse_date(@_);
381
382   return $yy && $mm && $dd ? DateTime->new(year => $yy, month => $mm, day => $dd) : undef;
383 }
384
385 sub reformat_date {
386   $main::lxdebug->enter_sub();
387
388   my ($self, $myconfig, $date, $output_format, $longformat) = @_;
389
390   $main::lxdebug->leave_sub() and return "" unless ($date);
391
392   my ($yy, $mm, $dd) = $self->parse_date($myconfig, $date);
393
394   $output_format =~ /d+/;
395   substr($output_format, $-[0], $+[0] - $-[0]) =
396     sprintf("%0" . (length($&)) . "d", $dd);
397
398   $output_format =~ /m+/;
399   substr($output_format, $-[0], $+[0] - $-[0]) =
400     sprintf("%0" . (length($&)) . "d", $mm);
401
402   $output_format =~ /y+/;
403   substr($output_format, $-[0], $+[0] - $-[0]) = $yy;
404
405   $main::lxdebug->leave_sub();
406
407   return $output_format;
408 }
409
410 sub format_date {
411   $main::lxdebug->enter_sub();
412
413   my $self     = shift;
414   my $myconfig = shift;
415   my $yy       = shift;
416   my $mm       = shift;
417   my $dd       = shift;
418   my $yy_len   = shift || 4;
419
420   ($yy, $mm, $dd) = ($yy->year, $yy->month, $yy->day) if ref $yy eq 'DateTime';
421
422   $main::lxdebug->leave_sub() and return "" unless $yy && $mm && $dd;
423
424   $yy = $yy % 100 if 2 == $yy_len;
425
426   my $format = ref $myconfig eq '' ? "$myconfig" : $myconfig->{dateformat};
427   $format =~ s{ d+ }{ sprintf("%0" . (length($&)) . "d", $dd) }gex;
428   $format =~ s{ m+ }{ sprintf("%0" . (length($&)) . "d", $mm) }gex;
429   $format =~ s{ y+ }{ sprintf("%0${yy_len}d",            $yy) }gex;
430
431   $main::lxdebug->leave_sub();
432
433   return $format;
434 }
435
436 sub quote_special_chars {
437   my $self   = shift;
438   my $format = lc shift;
439   my $string = shift;
440
441   if ($self->{special_chars_map} && $self->{special_chars_map}->{$format} && $self->{special_chars_map}->{$format}->{order}) {
442     my $scmap = $self->{special_chars_map}->{$format};
443
444     map { $string =~ s/\Q${_}\E/$scmap->{$_}/g } @{ $scmap->{order} };
445   }
446
447   return $string;
448 }
449
450 sub unquote_special_chars {
451   my $self    = shift;
452   my $format  = shift;
453
454   return $self->quote_special_chars("${format}-reverse", shift);
455 }
456
457 sub remap_special_chars {
458   my $self       = shift;
459   my $src_format = shift;
460   my $dst_format = shift;
461
462   return $self->quote_special_chars($dst_format, $self->quote_special_chars("${src_format}-reverse", shift));
463 }
464
465 sub raw_io_active {
466   my $self = shift;
467
468   return !!$self->{raw_io_active};
469 }
470
471 sub with_raw_io {
472   my $self = shift;
473   my $fh   = shift;
474   my $code = shift;
475
476   $self->{raw_io_active} = 1;
477   binmode $fh, ":raw";
478   $code->();
479   binmode $fh, ":utf8" if $self->is_utf8;
480   $self->{raw_io_active} = 0;
481 }
482
483 sub set_numberformat_wo_thousands_separator {
484   my $self     = shift;
485   my $myconfig = shift || \%::myconfig;
486
487   $self->{saved_numberformat} = $myconfig->{numberformat};
488   $myconfig->{numberformat}   =~ s/^1[,\.]/1/;
489 }
490
491 sub restore_numberformat {
492   my $self     = shift;
493   my $myconfig = shift || \%::myconfig;
494
495   $myconfig->{numberformat} = $self->{saved_numberformat} if $self->{saved_numberformat};
496 }
497
498 1;