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