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