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