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