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