Funktionsnamensauflösung gefixt
[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 use List::Util qw(first);
41
42 use SL::LXDebug;
43 use SL::Common;
44 use SL::Inifile;
45
46 use strict;
47
48 sub new {
49   $main::lxdebug->enter_sub();
50
51   my ($type, $country) = @_;
52
53   $country ||= $::language;
54   $country   =~ s|.*/||;
55   $country   =~ s|\.||g;
56
57   my $self = {};
58   bless $self, $type;
59
60   $self->_init($country);
61
62   $main::lxdebug->leave_sub();
63
64   return $self;
65 }
66
67 sub _init {
68   my $self     = shift;
69   my $country  = shift;
70
71   $self->{charset}     = Common::DEFAULT_CHARSET;
72   $self->{countrycode} = $country;
73
74   if ($country && -d "locale/$country") {
75     local *IN;
76     if (open(IN, "<", "locale/$country/all")) {
77       my $code = join("", <IN>);
78       eval($code);
79       close(IN);
80     }
81
82     if (open IN, "<", "locale/$country/charset") {
83       $self->{charset} = <IN>;
84       close IN;
85
86       chomp $self->{charset};
87     }
88   }
89
90   my $db_charset            = $main::dbcharset || Common::DEFAULT_CHARSET;
91
92   $self->{iconv}            = Text::Iconv->new($self->{charset}, $db_charset);
93   $self->{iconv_reverse}    = Text::Iconv->new($db_charset,      $self->{charset});
94   $self->{iconv_english}    = Text::Iconv->new('ASCII',          $db_charset);
95   $self->{iconv_iso8859}    = Text::Iconv->new('ISO-8859-15',    $db_charset);
96   $self->{iconv_to_iso8859} = Text::Iconv->new($db_charset,      'ISO-8859-15');
97
98   $self->_read_special_chars_file($country);
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      = lc $self->{iconv_reverse}->convert($text);
213
214   if (!$self->{texts_reverse}) {
215     $self->{texts_reverse} = { };
216     while (my ($original, $translation) = each %{ $self->{texts} }) {
217       $original    =  lc $original;
218       $original    =~ s/[^a-z0-9]/_/g;
219       $original    =~ s/_+/_/g;
220
221       $translation =  lc $translation;
222
223       $self->{texts_reverse}->{$translation} ||= [ ];
224       push @{ $self->{texts_reverse}->{$translation} }, $original;
225     }
226   }
227
228   my $sub_name   = first { defined &{ "::$_" } } @{ $self->{texts_reverse}->{$text_rev} } if $self->{texts_reverse}->{$text_rev};
229   $sub_name    ||= $text_rev if ($text_rev =~ m/^[a-z][a-z0-9_]+$/) && defined &{ "::$text_rev" };
230
231   $main::form->error("$text not defined in locale/$self->{countrycode}/all") if !$sub_name;
232
233   $main::lxdebug->leave_sub();
234
235   return $sub_name;
236 }
237
238 sub date {
239   $main::lxdebug->enter_sub();
240
241   my ($self, $myconfig, $date, $longformat) = @_;
242
243   my $longdate  = "";
244   my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
245
246   my ($spc, $yy, $mm, $dd);
247
248   if ($date) {
249
250     # get separator
251     $spc = $myconfig->{dateformat};
252     $spc =~ s/\w//g;
253     $spc = substr($spc, 1, 1);
254
255     if ($date =~ /\D/) {
256       if ($myconfig->{dateformat} =~ /^yy/) {
257         ($yy, $mm, $dd) = split /\D/, $date;
258       }
259       if ($myconfig->{dateformat} =~ /^mm/) {
260         ($mm, $dd, $yy) = split /\D/, $date;
261       }
262       if ($myconfig->{dateformat} =~ /^dd/) {
263         ($dd, $mm, $yy) = split /\D/, $date;
264       }
265     } else {
266       $date = substr($date, 2);
267       ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
268     }
269
270     $dd *= 1;
271     $mm--;
272     $yy = ($yy < 70) ? $yy + 2000 : $yy;
273     $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
274
275     if ($myconfig->{dateformat} =~ /^dd/) {
276       if (defined $longformat && $longformat == 0) {
277         $mm++;
278         $dd = "0$dd" if ($dd < 10);
279         $mm = "0$mm" if ($mm < 10);
280         $longdate = "$dd$spc$mm$spc$yy";
281       } else {
282         $longdate = "$dd";
283         $longdate .= ($spc eq '.') ? ". " : " ";
284         $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
285       }
286     } elsif ($myconfig->{dateformat} eq "yyyy-mm-dd") {
287
288       # Use German syntax with the ISO date style "yyyy-mm-dd" because
289       # Lx-Office is mainly used in Germany or German speaking countries.
290       if (defined $longformat && $longformat == 0) {
291         $mm++;
292         $dd = "0$dd" if ($dd < 10);
293         $mm = "0$mm" if ($mm < 10);
294         $longdate = "$yy-$mm-$dd";
295       } else {
296         $longdate = "$dd. ";
297         $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
298       }
299     } else {
300       if (defined $longformat && $longformat == 0) {
301         $mm++;
302         $dd = "0$dd" if ($dd < 10);
303         $mm = "0$mm" if ($mm < 10);
304         $longdate = "$mm$spc$dd$spc$yy";
305       } else {
306         $longdate = &text($self, $self->{$longmonth}[$mm]) . " $dd, $yy";
307       }
308     }
309
310   }
311
312   $main::lxdebug->leave_sub();
313
314   return $longdate;
315 }
316
317 sub parse_date {
318   $main::lxdebug->enter_sub();
319
320   my ($self, $myconfig, $date, $longformat) = @_;
321   my ($spc, $yy, $mm, $dd);
322
323   unless ($date) {
324     $main::lxdebug->leave_sub();
325     return ();
326   }
327
328   # get separator
329   $spc = $myconfig->{dateformat};
330   $spc =~ s/\w//g;
331   $spc = substr($spc, 1, 1);
332
333   if ($date =~ /\D/) {
334     if ($myconfig->{dateformat} =~ /^yy/) {
335       ($yy, $mm, $dd) = split /\D/, $date;
336     } elsif ($myconfig->{dateformat} =~ /^mm/) {
337       ($mm, $dd, $yy) = split /\D/, $date;
338     } elsif ($myconfig->{dateformat} =~ /^dd/) {
339       ($dd, $mm, $yy) = split /\D/, $date;
340     }
341   } else {
342     $date = substr($date, 2);
343     ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
344   }
345
346   $dd *= 1;
347   $mm *= 1;
348   $yy = ($yy < 70) ? $yy + 2000 : $yy;
349   $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
350
351   $main::lxdebug->leave_sub();
352   return ($yy, $mm, $dd);
353 }
354
355 sub reformat_date {
356   $main::lxdebug->enter_sub();
357
358   my ($self, $myconfig, $date, $output_format, $longformat) = @_;
359
360   $main::lxdebug->leave_sub() and return "" unless ($date);
361
362   my ($yy, $mm, $dd) = $self->parse_date($myconfig, $date);
363
364   $output_format =~ /d+/;
365   substr($output_format, $-[0], $+[0] - $-[0]) =
366     sprintf("%0" . (length($&)) . "d", $dd);
367
368   $output_format =~ /m+/;
369   substr($output_format, $-[0], $+[0] - $-[0]) =
370     sprintf("%0" . (length($&)) . "d", $mm);
371
372   $output_format =~ /y+/;
373   substr($output_format, $-[0], $+[0] - $-[0]) = $yy;
374
375   $main::lxdebug->leave_sub();
376
377   return $output_format;
378 }
379
380 sub quote_special_chars {
381   my $self   = shift;
382   my $format = lc shift;
383   my $string = shift;
384
385   if ($self->{special_chars_map} && $self->{special_chars_map}->{$format} && $self->{special_chars_map}->{$format}->{order}) {
386     my $scmap = $self->{special_chars_map}->{$format};
387
388     map { $string =~ s/\Q${_}\E/$scmap->{$_}/g } @{ $scmap->{order} };
389   }
390
391   return $string;
392 }
393
394 sub unquote_special_chars {
395   my $self    = shift;
396   my $format  = shift;
397
398   return $self->quote_special_chars("${format}-reverse", shift);
399 }
400
401 sub remap_special_chars {
402   my $self       = shift;
403   my $src_format = shift;
404   my $dst_format = shift;
405
406   return $self->quote_special_chars($dst_format, $self->quote_special_chars("${src_format}-reverse", shift));
407 }
408
409 1;