Report-Generator: Spaltenüberschriften in HTML- und PDF-Ausgaben nicht erneut HTML...
[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 Text::Iconv;
40
41 use SL::LXDebug;
42 use SL::Common;
43
44 sub new {
45   $main::lxdebug->enter_sub();
46
47   my ($type, $country, $NLS_file) = @_;
48   my $self = {};
49
50   $country  =~ s|.*/||;
51   $country  =~ s|\.||g;
52   $NLS_file =~ s|.*/||;
53
54   if ($country && -d "locale/$country") {
55     local *IN;
56     $self->{countrycode} = $country;
57     if (open(IN, "<", "locale/$country/$NLS_file")) {
58       my $code = join("", <IN>);
59       eval($code);
60       close(IN);
61     }
62
63     if (open IN, "<", "locale/$country/charset") {
64       $self->{charset} = <IN>;
65       close IN;
66
67       chomp $self->{charset};
68
69     } else {
70       $self->{charset} = Common::DEFAULT_CHARSET;
71     }
72
73     my $db_charset         = $main::dbcharset || Common::DEFAULT_CHARSET;
74
75     $self->{iconv}         = Text::Iconv->new($self->{charset}, $db_charset);
76     $self->{iconv_english} = Text::Iconv->new('ASCII',          $db_charset);
77     $self->{iconv_iso8859} = Text::Iconv->new('ISO-8859-15',    $db_charset);
78   }
79
80   $self->{NLS_file} = $NLS_file;
81
82   push @{ $self->{LONG_MONTH} },
83     ("January",   "February", "March",    "April",
84      "May ",      "June",     "July",     "August",
85      "September", "October",  "November", "December");
86   push @{ $self->{SHORT_MONTH} },
87     (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
88
89   $main::lxdebug->leave_sub();
90
91   bless $self, $type;
92 }
93
94 sub text {
95   my $self = shift;
96   my $text = shift;
97
98   if (exists $self->{texts}->{$text}) {
99     $text = $self->{iconv}->convert($self->{texts}->{$text});
100   } else {
101     $text = $self->{iconv_english}->convert($text);
102   }
103
104   if (@_) {
105     $text = Form->format_string($text, @_);
106   }
107
108   return $text;
109 }
110
111 sub findsub {
112   $main::lxdebug->enter_sub();
113
114   my ($self, $text) = @_;
115
116   if (exists $self->{subs}{$text}) {
117     $text = $self->{subs}{$text};
118   } else {
119     if ($self->{countrycode} && $self->{NLS_file}) {
120       Form->error(
121          "$text not defined in locale/$self->{countrycode}/$self->{NLS_file}");
122     }
123   }
124
125   $main::lxdebug->leave_sub();
126
127   return $text;
128 }
129
130 sub date {
131   $main::lxdebug->enter_sub();
132
133   my ($self, $myconfig, $date, $longformat) = @_;
134
135   my $longdate  = "";
136   my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
137
138   if ($date) {
139
140     # get separator
141     $spc = $myconfig->{dateformat};
142     $spc =~ s/\w//g;
143     $spc = substr($spc, 1, 1);
144
145     if ($date =~ /\D/) {
146       if ($myconfig->{dateformat} =~ /^yy/) {
147         ($yy, $mm, $dd) = split /\D/, $date;
148       }
149       if ($myconfig->{dateformat} =~ /^mm/) {
150         ($mm, $dd, $yy) = split /\D/, $date;
151       }
152       if ($myconfig->{dateformat} =~ /^dd/) {
153         ($dd, $mm, $yy) = split /\D/, $date;
154       }
155     } else {
156       $date = substr($date, 2);
157       ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
158     }
159
160     $dd *= 1;
161     $mm--;
162     $yy = ($yy < 70) ? $yy + 2000 : $yy;
163     $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
164
165     if ($myconfig->{dateformat} =~ /^dd/) {
166       if (defined $longformat && $longformat == 0) {
167         $mm++;
168         $dd = "0$dd" if ($dd < 10);
169         $mm = "0$mm" if ($mm < 10);
170         $longdate = "$dd$spc$mm$spc$yy";
171       } else {
172         $longdate = "$dd";
173         $longdate .= ($spc eq '.') ? ". " : " ";
174         $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
175       }
176     } elsif ($myconfig->{dateformat} eq "yyyy-mm-dd") {
177
178       # Use German syntax with the ISO date style "yyyy-mm-dd" because
179       # Lx-Office is mainly used in Germany or German speaking countries.
180       if (defined $longformat && $longformat == 0) {
181         $mm++;
182         $dd = "0$dd" if ($dd < 10);
183         $mm = "0$mm" if ($mm < 10);
184         $longdate = "$yy-$mm-$dd";
185       } else {
186         $longdate = "$dd. ";
187         $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
188       }
189     } else {
190       if (defined $longformat && $longformat == 0) {
191         $mm++;
192         $dd = "0$dd" if ($dd < 10);
193         $mm = "0$mm" if ($mm < 10);
194         $longdate = "$mm$spc$dd$spc$yy";
195       } else {
196         $longdate = &text($self, $self->{$longmonth}[$mm]) . " $dd, $yy";
197       }
198     }
199
200   }
201
202   $main::lxdebug->leave_sub();
203
204   return $longdate;
205 }
206
207 sub parse_date {
208   $main::lxdebug->enter_sub();
209
210   my ($self, $myconfig, $date, $longformat) = @_;
211
212   unless ($date) {
213     $main::lxdebug->leave_sub();
214     return ();
215   }
216
217   # get separator
218   $spc = $myconfig->{dateformat};
219   $spc =~ s/\w//g;
220   $spc = substr($spc, 1, 1);
221
222   if ($date =~ /\D/) {
223     if ($myconfig->{dateformat} =~ /^yy/) {
224       ($yy, $mm, $dd) = split /\D/, $date;
225     } elsif ($myconfig->{dateformat} =~ /^mm/) {
226       ($mm, $dd, $yy) = split /\D/, $date;
227     } elsif ($myconfig->{dateformat} =~ /^dd/) {
228       ($dd, $mm, $yy) = split /\D/, $date;
229     }
230   } else {
231     $date = substr($date, 2);
232     ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
233   }
234
235   $dd *= 1;
236   $mm *= 1;
237   $yy = ($yy < 70) ? $yy + 2000 : $yy;
238   $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
239
240   $main::lxdebug->leave_sub();
241   return ($yy, $mm, $dd);
242 }
243
244 sub reformat_date {
245   $main::lxdebug->enter_sub();
246
247   my ($self, $myconfig, $date, $output_format, $longformat) = @_;
248
249   $main::lxdebug->leave_sub() and return "" unless ($date);
250
251   my ($yy, $mm, $dd) = $self->parse_date($myconfig, $date);
252
253   $output_format =~ /d+/;
254   substr($output_format, $-[0], $+[0] - $-[0]) =
255     sprintf("%0" . (length($&)) . "d", $dd);
256
257   $output_format =~ /m+/;
258   substr($output_format, $-[0], $+[0] - $-[0]) =
259     sprintf("%0" . (length($&)) . "d", $mm);
260
261   $output_format =~ /y+/;
262   if (length($&) == 2) {
263     $yy -= $yy >= 2000 ? 2000 : 1900;
264   }
265   substr($output_format, $-[0], $+[0] - $-[0]) =
266     sprintf("%0" . (length($&)) . "d", $yy);
267
268   $main::lxdebug->leave_sub();
269
270   return $output_format;
271 }
272
273 1;