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