$locale->text() so erweitert, dass weitere Argumente direkt an Form::format_string...
[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 = shift;
95   my $text = shift;
96
97   if (exists $self->{texts}->{$text}) {
98     $text = $self->{iconv}->convert($self->{texts}->{$text});
99   } else {
100     $text = $self->{iconv_english}->convert($text);
101   }
102
103   if (@_) {
104     $text = Form->format_string($text, @_);
105   }
106
107   return $text;
108 }
109
110 sub findsub {
111   $main::lxdebug->enter_sub();
112
113   my ($self, $text) = @_;
114
115   if (exists $self->{subs}{$text}) {
116     $text = $self->{subs}{$text};
117   } else {
118     if ($self->{countrycode} && $self->{NLS_file}) {
119       Form->error(
120          "$text not defined in locale/$self->{countrycode}/$self->{NLS_file}");
121     }
122   }
123
124   $main::lxdebug->leave_sub();
125
126   return $text;
127 }
128
129 sub date {
130   $main::lxdebug->enter_sub();
131
132   my ($self, $myconfig, $date, $longformat) = @_;
133
134   my $longdate  = "";
135   my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
136
137   if ($date) {
138
139     # get separator
140     $spc = $myconfig->{dateformat};
141     $spc =~ s/\w//g;
142     $spc = substr($spc, 1, 1);
143
144     if ($date =~ /\D/) {
145       if ($myconfig->{dateformat} =~ /^yy/) {
146         ($yy, $mm, $dd) = split /\D/, $date;
147       }
148       if ($myconfig->{dateformat} =~ /^mm/) {
149         ($mm, $dd, $yy) = split /\D/, $date;
150       }
151       if ($myconfig->{dateformat} =~ /^dd/) {
152         ($dd, $mm, $yy) = split /\D/, $date;
153       }
154     } else {
155       $date = substr($date, 2);
156       ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
157     }
158
159     $dd *= 1;
160     $mm--;
161     $yy = ($yy < 70) ? $yy + 2000 : $yy;
162     $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
163
164     if ($myconfig->{dateformat} =~ /^dd/) {
165       if (defined $longformat && $longformat == 0) {
166         $mm++;
167         $dd = "0$dd" if ($dd < 10);
168         $mm = "0$mm" if ($mm < 10);
169         $longdate = "$dd$spc$mm$spc$yy";
170       } else {
171         $longdate = "$dd";
172         $longdate .= ($spc eq '.') ? ". " : " ";
173         $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
174       }
175     } elsif ($myconfig->{dateformat} eq "yyyy-mm-dd") {
176
177       # Use German syntax with the ISO date style "yyyy-mm-dd" because
178       # Lx-Office is mainly used in Germany or German speaking countries.
179       if (defined $longformat && $longformat == 0) {
180         $mm++;
181         $dd = "0$dd" if ($dd < 10);
182         $mm = "0$mm" if ($mm < 10);
183         $longdate = "$yy-$mm-$dd";
184       } else {
185         $longdate = "$dd. ";
186         $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
187       }
188     } else {
189       if (defined $longformat && $longformat == 0) {
190         $mm++;
191         $dd = "0$dd" if ($dd < 10);
192         $mm = "0$mm" if ($mm < 10);
193         $longdate = "$mm$spc$dd$spc$yy";
194       } else {
195         $longdate = &text($self, $self->{$longmonth}[$mm]) . " $dd, $yy";
196       }
197     }
198
199   }
200
201   $main::lxdebug->leave_sub();
202
203   return $longdate;
204 }
205
206 sub parse_date {
207   $main::lxdebug->enter_sub();
208
209   my ($self, $myconfig, $date, $longformat) = @_;
210
211   unless ($date) {
212     $main::lxdebug->leave_sub();
213     return ();
214   }
215
216   # get separator
217   $spc = $myconfig->{dateformat};
218   $spc =~ s/\w//g;
219   $spc = substr($spc, 1, 1);
220
221   if ($date =~ /\D/) {
222     if ($myconfig->{dateformat} =~ /^yy/) {
223       ($yy, $mm, $dd) = split /\D/, $date;
224     } elsif ($myconfig->{dateformat} =~ /^mm/) {
225       ($mm, $dd, $yy) = split /\D/, $date;
226     } elsif ($myconfig->{dateformat} =~ /^dd/) {
227       ($dd, $mm, $yy) = split /\D/, $date;
228     }
229   } else {
230     $date = substr($date, 2);
231     ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
232   }
233
234   $dd *= 1;
235   $mm *= 1;
236   $yy = ($yy < 70) ? $yy + 2000 : $yy;
237   $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
238
239   $main::lxdebug->leave_sub();
240   return ($yy, $mm, $dd);
241 }
242
243 sub reformat_date {
244   $main::lxdebug->enter_sub();
245
246   my ($self, $myconfig, $date, $output_format, $longformat) = @_;
247
248   $main::lxdebug->leave_sub() and return "" unless ($date);
249
250   my ($yy, $mm, $dd) = $self->parse_date($myconfig, $date);
251
252   $output_format =~ /d+/;
253   substr($output_format, $-[0], $+[0] - $-[0]) =
254     sprintf("%0" . (length($&)) . "d", $dd);
255
256   $output_format =~ /m+/;
257   substr($output_format, $-[0], $+[0] - $-[0]) =
258     sprintf("%0" . (length($&)) . "d", $mm);
259
260   $output_format =~ /y+/;
261   if (length($&) == 2) {
262     $yy -= $yy >= 2000 ? 2000 : 1900;
263   }
264   substr($output_format, $-[0], $+[0] - $-[0]) =
265     sprintf("%0" . (length($&)) . "d", $yy);
266
267   $main::lxdebug->leave_sub();
268
269   return $output_format;
270 }
271
272 1;