1 #====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (C) 1998-2002
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
15 # Contributors: Thomas Bayen <bayen@gmx.de>
16 # Antti Kaihola <akaihola@siba.fi>
17 # Moritz Bunkus (tex code)
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.
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 #======================================================================
33 # Translations and number/date formatting
35 #======================================================================
46 $main::lxdebug->enter_sub();
48 my ($type, $country, $NLS_file) = @_;
57 $self->_init($country, $NLS_file);
59 $main::lxdebug->leave_sub();
69 if ($country && -d "locale/$country") {
71 $self->{countrycode} = $country;
72 if (open(IN, "<", "locale/$country/$NLS_file")) {
73 my $code = join("", <IN>);
78 if (open IN, "<", "locale/$country/charset") {
79 $self->{charset} = <IN>;
82 chomp $self->{charset};
85 $self->{charset} = Common::DEFAULT_CHARSET;
88 my $db_charset = $main::dbcharset || Common::DEFAULT_CHARSET;
90 $self->{iconv} = Text::Iconv->new($self->{charset}, $db_charset);
91 $self->{iconv_reverse} = Text::Iconv->new($db_charset, $self->{charset});
92 $self->{iconv_english} = Text::Iconv->new('ASCII', $db_charset);
93 $self->{iconv_iso8859} = Text::Iconv->new('ISO-8859-15', $db_charset);
94 $self->{iconv_to_iso8859} = Text::Iconv->new($db_charset, 'ISO-8859-15');
96 $self->_read_special_chars_file($country);
99 $self->{NLS_file} = $NLS_file;
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));
116 for (my $i = 0; $i < length $str; $i++) {
117 my $char = substr $str, $i, 1;
123 } elsif ($char eq 'r') {
126 } elsif ($char eq 's') {
129 } elsif ($char eq 'x') {
130 $new_str .= chr(hex(substr($str, $i + 1, 2)));
139 } elsif ($char eq '\\') {
150 sub _read_special_chars_file {
154 if (! -f "locale/$country/special_chars") {
155 $self->{special_chars_map} = {};
159 $self->{special_chars_map} = Inifile->new("locale/$country/special_chars", 'verbatim' => 1);
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'));
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;
170 my $scmap = $self->{special_chars_map}->{$format};
171 my $order = $self->{iconv}->convert($scmap->{order});
172 delete $scmap->{order};
174 foreach my $key (keys %{ $scmap }) {
175 $scmap->{$key} = $self->_handle_markup($self->{iconv}->convert($scmap->{$key}));
177 my $new_key = $self->_handle_markup($self->{iconv}->convert($key));
179 if ($key ne $new_key) {
180 $scmap->{$new_key} = $scmap->{$key};
181 delete $scmap->{$key};
185 $self->{special_chars_map}->{"${format}-reverse"} = { reverse %{ $scmap } };
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} } ];
196 if (exists $self->{texts}->{$text}) {
197 $text = $self->{iconv}->convert($self->{texts}->{$text});
199 $text = $self->{iconv_english}->convert($text);
203 $text = Form->format_string($text, @_);
210 $main::lxdebug->enter_sub();
212 my ($self, $text) = @_;
213 my $text_rev = $self->{iconv_reverse}->convert($text);
215 if (exists $self->{subs}{$text_rev}) {
216 $text = $self->{subs}{$text_rev};
217 } elsif ($self->{countrycode} && $self->{NLS_file}) {
218 Form->error("$text not defined in locale/$self->{countrycode}/$self->{NLS_file}");
221 $main::lxdebug->leave_sub();
227 $main::lxdebug->enter_sub();
229 my ($self, $myconfig, $date, $longformat) = @_;
232 my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
237 $spc = $myconfig->{dateformat};
239 $spc = substr($spc, 1, 1);
242 if ($myconfig->{dateformat} =~ /^yy/) {
243 ($yy, $mm, $dd) = split /\D/, $date;
245 if ($myconfig->{dateformat} =~ /^mm/) {
246 ($mm, $dd, $yy) = split /\D/, $date;
248 if ($myconfig->{dateformat} =~ /^dd/) {
249 ($dd, $mm, $yy) = split /\D/, $date;
252 $date = substr($date, 2);
253 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
258 $yy = ($yy < 70) ? $yy + 2000 : $yy;
259 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
261 if ($myconfig->{dateformat} =~ /^dd/) {
262 if (defined $longformat && $longformat == 0) {
264 $dd = "0$dd" if ($dd < 10);
265 $mm = "0$mm" if ($mm < 10);
266 $longdate = "$dd$spc$mm$spc$yy";
269 $longdate .= ($spc eq '.') ? ". " : " ";
270 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
272 } elsif ($myconfig->{dateformat} eq "yyyy-mm-dd") {
274 # Use German syntax with the ISO date style "yyyy-mm-dd" because
275 # Lx-Office is mainly used in Germany or German speaking countries.
276 if (defined $longformat && $longformat == 0) {
278 $dd = "0$dd" if ($dd < 10);
279 $mm = "0$mm" if ($mm < 10);
280 $longdate = "$yy-$mm-$dd";
283 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
286 if (defined $longformat && $longformat == 0) {
288 $dd = "0$dd" if ($dd < 10);
289 $mm = "0$mm" if ($mm < 10);
290 $longdate = "$mm$spc$dd$spc$yy";
292 $longdate = &text($self, $self->{$longmonth}[$mm]) . " $dd, $yy";
298 $main::lxdebug->leave_sub();
304 $main::lxdebug->enter_sub();
306 my ($self, $myconfig, $date, $longformat) = @_;
309 $main::lxdebug->leave_sub();
314 $spc = $myconfig->{dateformat};
316 $spc = substr($spc, 1, 1);
319 if ($myconfig->{dateformat} =~ /^yy/) {
320 ($yy, $mm, $dd) = split /\D/, $date;
321 } elsif ($myconfig->{dateformat} =~ /^mm/) {
322 ($mm, $dd, $yy) = split /\D/, $date;
323 } elsif ($myconfig->{dateformat} =~ /^dd/) {
324 ($dd, $mm, $yy) = split /\D/, $date;
327 $date = substr($date, 2);
328 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
333 $yy = ($yy < 70) ? $yy + 2000 : $yy;
334 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
336 $main::lxdebug->leave_sub();
337 return ($yy, $mm, $dd);
341 $main::lxdebug->enter_sub();
343 my ($self, $myconfig, $date, $output_format, $longformat) = @_;
345 $main::lxdebug->leave_sub() and return "" unless ($date);
347 my ($yy, $mm, $dd) = $self->parse_date($myconfig, $date);
349 $output_format =~ /d+/;
350 substr($output_format, $-[0], $+[0] - $-[0]) =
351 sprintf("%0" . (length($&)) . "d", $dd);
353 $output_format =~ /m+/;
354 substr($output_format, $-[0], $+[0] - $-[0]) =
355 sprintf("%0" . (length($&)) . "d", $mm);
357 $output_format =~ /y+/;
358 if (length($&) == 2) {
359 $yy -= $yy >= 2000 ? 2000 : 1900;
361 substr($output_format, $-[0], $+[0] - $-[0]) =
362 sprintf("%0" . (length($&)) . "d", $yy);
364 $main::lxdebug->leave_sub();
366 return $output_format;
369 sub quote_special_chars {
371 my $format = lc shift;
374 if ($self->{special_chars_map} && $self->{special_chars_map}->{$format} && $self->{special_chars_map}->{$format}->{order}) {
375 my $scmap = $self->{special_chars_map}->{$format};
377 map { $string =~ s/\Q${_}\E/$scmap->{$_}/g } @{ $scmap->{order} };
383 sub unquote_special_chars {
387 return $self->quote_special_chars("${format}-reverse", shift);
390 sub remap_special_chars {
392 my $src_format = shift;
393 my $dst_format = shift;
395 return $self->quote_special_chars($dst_format, $self->quote_special_chars("${src_format}-reverse", shift));