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