Das Quoten/Unquoten von speziellen Zeichen in zentrale Hilfsfunktionen in Locale...
[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 use SL::Inifile;
44
45 sub new {
46   $main::lxdebug->enter_sub();
47
48   my ($type, $country, $NLS_file) = @_;
49
50   my $self = {};
51   bless $self, $type;
52
53   $country  =~ s|.*/||;
54   $country  =~ s|\.||g;
55   $NLS_file =~ s|.*/||;
56
57   $self->_init($country, $NLS_file);
58
59   $main::lxdebug->leave_sub();
60
61   return $self;
62 }
63
64 sub _init {
65   my $self     = shift;
66   my $country  = shift;
67   my $NLS_file = shift;
68
69   if ($country && -d "locale/$country") {
70     local *IN;
71     $self->{countrycode} = $country;
72     if (open(IN, "<", "locale/$country/$NLS_file")) {
73       my $code = join("", <IN>);
74       eval($code);
75       close(IN);
76     }
77
78     if (open IN, "<", "locale/$country/charset") {
79       $self->{charset} = <IN>;
80       close IN;
81
82       chomp $self->{charset};
83
84     } else {
85       $self->{charset} = Common::DEFAULT_CHARSET;
86     }
87
88     $self->_read_special_chars_file($country);
89
90     my $db_charset         = $main::dbcharset || Common::DEFAULT_CHARSET;
91
92     $self->{iconv}         = Text::Iconv->new($self->{charset}, $db_charset);
93     $self->{iconv_english} = Text::Iconv->new('ASCII',          $db_charset);
94     $self->{iconv_iso8859} = Text::Iconv->new('ISO-8859-15',    $db_charset);
95   }
96
97   $self->{NLS_file} = $NLS_file;
98
99   push @{ $self->{LONG_MONTH} },
100     ("January",   "February", "March",    "April",
101      "May ",      "June",     "July",     "August",
102      "September", "October",  "November", "December");
103   push @{ $self->{SHORT_MONTH} },
104     (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
105 }
106
107 sub _handle_markup {
108   my $self = shift;
109   my $str  = shift;
110
111   if ($str eq "\\n") {
112     return "\n";
113   } elsif ($str eq "\\r") {
114     return "\r";
115   }
116
117   $str =~ s/\\x(..)/chr(hex($1))/eg;
118   $str =~ s/\\(.)/$1/g;
119
120   return $str;
121 }
122
123 sub _read_special_chars_file {
124   my $self    = shift;
125   my $country = shift;
126
127   if (! -f "locale/$country/special_chars") {
128     $self->{special_chars_map} = {};
129     return;
130   }
131
132   $self->{special_chars_map} = Inifile->new("locale/$country/special_chars", 'verbatim' => 1);
133
134   foreach my $format (keys %{ $self->{special_chars_map} }) {
135     next if (($format eq 'FILE') || ($format eq 'ORDER') || (ref $self->{special_chars_map}->{$format} ne 'HASH'));
136
137     if ($format ne lc $format) {
138       $self->{special_chars_map}->{lc $format} = $self->{special_chars_map}->{$format};
139       delete $self->{special_chars_map}->{$format};
140       $format = lc $format;
141     }
142
143     my $scmap = $self->{special_chars_map}->{$format};
144     my $order = $scmap->{order};
145     delete $scmap->{order};
146
147     foreach my $key (keys %{ $scmap }) {
148       $scmap->{$key} = $self->_handle_markup($scmap->{$key});
149
150       my $new_key    = $self->_handle_markup($key);
151
152       if ($key ne $new_key) {
153         $scmap->{$new_key} = $scmap->{$key};
154         delete $scmap->{$key};
155       }
156     }
157
158     $self->{special_chars_map}->{"${format}-reverse"}          = { reverse %{ $scmap } };
159
160     $scmap->{order}                                            = [ map { $self->_handle_markup($_) } split m/\s+/, $order ];
161     $self->{special_chars_map}->{"${format}-reverse"}->{order} = [ grep { $_ } map { $scmap->{$_} } reverse @{ $scmap->{order} } ];
162   }
163 }
164
165 sub text {
166   my $self = shift;
167   my $text = shift;
168
169   if (exists $self->{texts}->{$text}) {
170     $text = $self->{iconv}->convert($self->{texts}->{$text});
171   } else {
172     $text = $self->{iconv_english}->convert($text);
173   }
174
175   if (@_) {
176     $text = Form->format_string($text, @_);
177   }
178
179   return $text;
180 }
181
182 sub findsub {
183   $main::lxdebug->enter_sub();
184
185   my ($self, $text) = @_;
186
187   if (exists $self->{subs}{$text}) {
188     $text = $self->{subs}{$text};
189   } else {
190     if ($self->{countrycode} && $self->{NLS_file}) {
191       Form->error(
192          "$text not defined in locale/$self->{countrycode}/$self->{NLS_file}");
193     }
194   }
195
196   $main::lxdebug->leave_sub();
197
198   return $text;
199 }
200
201 sub date {
202   $main::lxdebug->enter_sub();
203
204   my ($self, $myconfig, $date, $longformat) = @_;
205
206   my $longdate  = "";
207   my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
208
209   if ($date) {
210
211     # get separator
212     $spc = $myconfig->{dateformat};
213     $spc =~ s/\w//g;
214     $spc = substr($spc, 1, 1);
215
216     if ($date =~ /\D/) {
217       if ($myconfig->{dateformat} =~ /^yy/) {
218         ($yy, $mm, $dd) = split /\D/, $date;
219       }
220       if ($myconfig->{dateformat} =~ /^mm/) {
221         ($mm, $dd, $yy) = split /\D/, $date;
222       }
223       if ($myconfig->{dateformat} =~ /^dd/) {
224         ($dd, $mm, $yy) = split /\D/, $date;
225       }
226     } else {
227       $date = substr($date, 2);
228       ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
229     }
230
231     $dd *= 1;
232     $mm--;
233     $yy = ($yy < 70) ? $yy + 2000 : $yy;
234     $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
235
236     if ($myconfig->{dateformat} =~ /^dd/) {
237       if (defined $longformat && $longformat == 0) {
238         $mm++;
239         $dd = "0$dd" if ($dd < 10);
240         $mm = "0$mm" if ($mm < 10);
241         $longdate = "$dd$spc$mm$spc$yy";
242       } else {
243         $longdate = "$dd";
244         $longdate .= ($spc eq '.') ? ". " : " ";
245         $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
246       }
247     } elsif ($myconfig->{dateformat} eq "yyyy-mm-dd") {
248
249       # Use German syntax with the ISO date style "yyyy-mm-dd" because
250       # Lx-Office is mainly used in Germany or German speaking countries.
251       if (defined $longformat && $longformat == 0) {
252         $mm++;
253         $dd = "0$dd" if ($dd < 10);
254         $mm = "0$mm" if ($mm < 10);
255         $longdate = "$yy-$mm-$dd";
256       } else {
257         $longdate = "$dd. ";
258         $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
259       }
260     } else {
261       if (defined $longformat && $longformat == 0) {
262         $mm++;
263         $dd = "0$dd" if ($dd < 10);
264         $mm = "0$mm" if ($mm < 10);
265         $longdate = "$mm$spc$dd$spc$yy";
266       } else {
267         $longdate = &text($self, $self->{$longmonth}[$mm]) . " $dd, $yy";
268       }
269     }
270
271   }
272
273   $main::lxdebug->leave_sub();
274
275   return $longdate;
276 }
277
278 sub parse_date {
279   $main::lxdebug->enter_sub();
280
281   my ($self, $myconfig, $date, $longformat) = @_;
282
283   unless ($date) {
284     $main::lxdebug->leave_sub();
285     return ();
286   }
287
288   # get separator
289   $spc = $myconfig->{dateformat};
290   $spc =~ s/\w//g;
291   $spc = substr($spc, 1, 1);
292
293   if ($date =~ /\D/) {
294     if ($myconfig->{dateformat} =~ /^yy/) {
295       ($yy, $mm, $dd) = split /\D/, $date;
296     } elsif ($myconfig->{dateformat} =~ /^mm/) {
297       ($mm, $dd, $yy) = split /\D/, $date;
298     } elsif ($myconfig->{dateformat} =~ /^dd/) {
299       ($dd, $mm, $yy) = split /\D/, $date;
300     }
301   } else {
302     $date = substr($date, 2);
303     ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
304   }
305
306   $dd *= 1;
307   $mm *= 1;
308   $yy = ($yy < 70) ? $yy + 2000 : $yy;
309   $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
310
311   $main::lxdebug->leave_sub();
312   return ($yy, $mm, $dd);
313 }
314
315 sub reformat_date {
316   $main::lxdebug->enter_sub();
317
318   my ($self, $myconfig, $date, $output_format, $longformat) = @_;
319
320   $main::lxdebug->leave_sub() and return "" unless ($date);
321
322   my ($yy, $mm, $dd) = $self->parse_date($myconfig, $date);
323
324   $output_format =~ /d+/;
325   substr($output_format, $-[0], $+[0] - $-[0]) =
326     sprintf("%0" . (length($&)) . "d", $dd);
327
328   $output_format =~ /m+/;
329   substr($output_format, $-[0], $+[0] - $-[0]) =
330     sprintf("%0" . (length($&)) . "d", $mm);
331
332   $output_format =~ /y+/;
333   if (length($&) == 2) {
334     $yy -= $yy >= 2000 ? 2000 : 1900;
335   }
336   substr($output_format, $-[0], $+[0] - $-[0]) =
337     sprintf("%0" . (length($&)) . "d", $yy);
338
339   $main::lxdebug->leave_sub();
340
341   return $output_format;
342 }
343
344 sub quote_special_chars {
345   my $self   = shift;
346   my $format = lc shift;
347   my $string = shift;
348
349   if ($self->{special_chars_map} && $self->{special_chars_map}->{$format} && $self->{special_chars_map}->{$format}->{order}) {
350     my $scmap = $self->{special_chars_map}->{$format};
351
352     map { $string =~ s/\Q${_}\E/$scmap->{$_}/g } @{ $scmap->{order} };
353   }
354
355   return $string;
356 }
357
358 sub unquote_special_chars {
359   my $self    = shift;
360   my $format  = shift;
361
362   return $self->quote_special_chars("${format}-reverse", shift);
363 }
364
365 sub remap_special_chars {
366   my $self       = shift;
367   my $src_format = shift;
368   my $dst_format = shift;
369
370   return $self->quote_special_chars($dst_format, $self->quote_special_chars("${src_format}-reverse", shift));
371 }
372
373 1;