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