Merge branch 'master' of ssh://lx-office/~/lx-office-erp
[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, $NLS_file) = @_;
51
52   my $self = {};
53   bless $self, $type;
54
55   $country  =~ s|.*/||;
56   $country  =~ s|\.||g;
57   $NLS_file =~ s|.*/||;
58
59   $self->_init($country, $NLS_file);
60
61   $main::lxdebug->leave_sub();
62
63   return $self;
64 }
65
66 sub _init {
67   my $self     = shift;
68   my $country  = shift;
69   my $NLS_file = shift;
70
71   $self->{charset}     = Common::DEFAULT_CHARSET;
72   $self->{countrycode} = $country;
73   $self->{NLS_file}    = $NLS_file;
74
75   if ($country && -d "locale/$country") {
76     local *IN;
77     if (open(IN, "<", "locale/$country/$NLS_file")) {
78       my $code = join("", <IN>);
79       eval($code);
80       close(IN);
81     }
82
83     if (open IN, "<", "locale/$country/charset") {
84       $self->{charset} = <IN>;
85       close IN;
86
87       chomp $self->{charset};
88     }
89   }
90
91   my $db_charset            = $main::dbcharset || Common::DEFAULT_CHARSET;
92
93   $self->{iconv}            = Text::Iconv->new($self->{charset}, $db_charset);
94   $self->{iconv_reverse}    = Text::Iconv->new($db_charset,      $self->{charset});
95   $self->{iconv_english}    = Text::Iconv->new('ASCII',          $db_charset);
96   $self->{iconv_iso8859}    = Text::Iconv->new('ISO-8859-15',    $db_charset);
97   $self->{iconv_to_iso8859} = Text::Iconv->new($db_charset,      'ISO-8859-15');
98
99   $self->_read_special_chars_file($country);
100
101   push @{ $self->{LONG_MONTH} },
102     ("January",   "February", "March",    "April",
103      "May ",      "June",     "July",     "August",
104      "September", "October",  "November", "December");
105   push @{ $self->{SHORT_MONTH} },
106     (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
107 }
108
109 sub _handle_markup {
110   my $self    = shift;
111   my $str     = shift;
112
113   my $escaped = 0;
114   my $new_str = '';
115
116   for (my $i = 0; $i < length $str; $i++) {
117     my $char = substr $str, $i, 1;
118
119     if ($escaped) {
120       if ($char eq 'n') {
121         $new_str .= "\n";
122
123       } elsif ($char eq 'r') {
124         $new_str .= "\r";
125
126       } elsif ($char eq 's') {
127         $new_str .= ' ';
128
129       } elsif ($char eq 'x') {
130         $new_str .= chr(hex(substr($str, $i + 1, 2)));
131         $i       += 2;
132
133       } else {
134         $new_str .= $char;
135       }
136
137       $escaped  = 0;
138
139     } elsif ($char eq '\\') {
140       $escaped  = 1;
141
142     } else {
143       $new_str .= $char;
144     }
145   }
146
147   return $new_str;
148 }
149
150 sub _read_special_chars_file {
151   my $self    = shift;
152   my $country = shift;
153
154   if (! -f "locale/$country/special_chars") {
155     $self->{special_chars_map} = {};
156     return;
157   }
158
159   $self->{special_chars_map} = Inifile->new("locale/$country/special_chars", 'verbatim' => 1);
160
161   foreach my $format (keys %{ $self->{special_chars_map} }) {
162     next if (($format eq 'FILE') || ($format eq 'ORDER') || (ref $self->{special_chars_map}->{$format} ne 'HASH'));
163
164     if ($format ne lc $format) {
165       $self->{special_chars_map}->{lc $format} = $self->{special_chars_map}->{$format};
166       delete $self->{special_chars_map}->{$format};
167       $format = lc $format;
168     }
169
170     my $scmap = $self->{special_chars_map}->{$format};
171     my $order = $self->{iconv}->convert($scmap->{order});
172     delete $scmap->{order};
173
174     foreach my $key (keys %{ $scmap }) {
175       $scmap->{$key} = $self->_handle_markup($self->{iconv}->convert($scmap->{$key}));
176
177       my $new_key    = $self->_handle_markup($self->{iconv}->convert($key));
178
179       if ($key ne $new_key) {
180         $scmap->{$new_key} = $scmap->{$key};
181         delete $scmap->{$key};
182       }
183     }
184
185     $self->{special_chars_map}->{"${format}-reverse"}          = { reverse %{ $scmap } };
186
187     $scmap->{order}                                            = [ map { $self->_handle_markup($_) } split m/\s+/, $order ];
188     $self->{special_chars_map}->{"${format}-reverse"}->{order} = [ grep { $_ } map { $scmap->{$_} } reverse @{ $scmap->{order} } ];
189   }
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   my $text_rev      = $self->{iconv_reverse}->convert($text);
214
215   if (exists $self->{subs}{$text_rev}) {
216     $text = $self->{subs}{$text_rev};
217   } elsif ($self->{countrycode} && $self->{NLS_file}) {
218     $main::form->error("$text not defined in locale/$self->{countrycode}/$self->{NLS_file}");
219   }
220
221   $main::lxdebug->leave_sub();
222
223   return $text;
224 }
225
226 sub date {
227   $main::lxdebug->enter_sub();
228
229   my ($self, $myconfig, $date, $longformat) = @_;
230
231   my $longdate  = "";
232   my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
233
234   my ($spc, $yy, $mm, $dd);
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   my ($spc, $yy, $mm, $dd);
310
311   unless ($date) {
312     $main::lxdebug->leave_sub();
313     return ();
314   }
315
316   # get separator
317   $spc = $myconfig->{dateformat};
318   $spc =~ s/\w//g;
319   $spc = substr($spc, 1, 1);
320
321   if ($date =~ /\D/) {
322     if ($myconfig->{dateformat} =~ /^yy/) {
323       ($yy, $mm, $dd) = split /\D/, $date;
324     } elsif ($myconfig->{dateformat} =~ /^mm/) {
325       ($mm, $dd, $yy) = split /\D/, $date;
326     } elsif ($myconfig->{dateformat} =~ /^dd/) {
327       ($dd, $mm, $yy) = split /\D/, $date;
328     }
329   } else {
330     $date = substr($date, 2);
331     ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
332   }
333
334   $dd *= 1;
335   $mm *= 1;
336   $yy = ($yy < 70) ? $yy + 2000 : $yy;
337   $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
338
339   $main::lxdebug->leave_sub();
340   return ($yy, $mm, $dd);
341 }
342
343 sub reformat_date {
344   $main::lxdebug->enter_sub();
345
346   my ($self, $myconfig, $date, $output_format, $longformat) = @_;
347
348   $main::lxdebug->leave_sub() and return "" unless ($date);
349
350   my ($yy, $mm, $dd) = $self->parse_date($myconfig, $date);
351
352   $output_format =~ /d+/;
353   substr($output_format, $-[0], $+[0] - $-[0]) =
354     sprintf("%0" . (length($&)) . "d", $dd);
355
356   $output_format =~ /m+/;
357   substr($output_format, $-[0], $+[0] - $-[0]) =
358     sprintf("%0" . (length($&)) . "d", $mm);
359
360   $output_format =~ /y+/;
361   substr($output_format, $-[0], $+[0] - $-[0]) = $yy;
362
363   $main::lxdebug->leave_sub();
364
365   return $output_format;
366 }
367
368 sub quote_special_chars {
369   my $self   = shift;
370   my $format = lc shift;
371   my $string = shift;
372
373   if ($self->{special_chars_map} && $self->{special_chars_map}->{$format} && $self->{special_chars_map}->{$format}->{order}) {
374     my $scmap = $self->{special_chars_map}->{$format};
375
376     map { $string =~ s/\Q${_}\E/$scmap->{$_}/g } @{ $scmap->{order} };
377   }
378
379   return $string;
380 }
381
382 sub unquote_special_chars {
383   my $self    = shift;
384   my $format  = shift;
385
386   return $self->quote_special_chars("${format}-reverse", shift);
387 }
388
389 sub remap_special_chars {
390   my $self       = shift;
391   my $src_format = shift;
392   my $dst_format = shift;
393
394   return $self->quote_special_chars($dst_format, $self->quote_special_chars("${src_format}-reverse", shift));
395 }
396
397 1;