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 $self->{charset} = Common::DEFAULT_CHARSET;
70 $self->{countrycode} = $country;
71 $self->{NLS_file} = $NLS_file;
73 if ($country && -d "locale/$country") {
75 if (open(IN, "<", "locale/$country/$NLS_file")) {
76 my $code = join("", <IN>);
81 if (open IN, "<", "locale/$country/charset") {
82 $self->{charset} = <IN>;
85 chomp $self->{charset};
89 my $db_charset = $main::dbcharset || Common::DEFAULT_CHARSET;
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');
97 $self->_read_special_chars_file($country);
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));
114 for (my $i = 0; $i < length $str; $i++) {
115 my $char = substr $str, $i, 1;
121 } elsif ($char eq 'r') {
124 } elsif ($char eq 's') {
127 } elsif ($char eq 'x') {
128 $new_str .= chr(hex(substr($str, $i + 1, 2)));
137 } elsif ($char eq '\\') {
148 sub _read_special_chars_file {
152 if (! -f "locale/$country/special_chars") {
153 $self->{special_chars_map} = {};
157 $self->{special_chars_map} = Inifile->new("locale/$country/special_chars", 'verbatim' => 1);
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'));
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;
168 my $scmap = $self->{special_chars_map}->{$format};
169 my $order = $self->{iconv}->convert($scmap->{order});
170 delete $scmap->{order};
172 foreach my $key (keys %{ $scmap }) {
173 $scmap->{$key} = $self->_handle_markup($self->{iconv}->convert($scmap->{$key}));
175 my $new_key = $self->_handle_markup($self->{iconv}->convert($key));
177 if ($key ne $new_key) {
178 $scmap->{$new_key} = $scmap->{$key};
179 delete $scmap->{$key};
183 $self->{special_chars_map}->{"${format}-reverse"} = { reverse %{ $scmap } };
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} } ];
194 if (exists $self->{texts}->{$text}) {
195 $text = $self->{iconv}->convert($self->{texts}->{$text});
197 $text = $self->{iconv_english}->convert($text);
201 $text = Form->format_string($text, @_);
208 $main::lxdebug->enter_sub();
210 my ($self, $text) = @_;
211 my $text_rev = $self->{iconv_reverse}->convert($text);
213 if (exists $self->{subs}{$text_rev}) {
214 $text = $self->{subs}{$text_rev};
215 } elsif ($self->{countrycode} && $self->{NLS_file}) {
216 Form->error("$text not defined in locale/$self->{countrycode}/$self->{NLS_file}");
219 $main::lxdebug->leave_sub();
225 $main::lxdebug->enter_sub();
227 my ($self, $myconfig, $date, $longformat) = @_;
230 my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
235 $spc = $myconfig->{dateformat};
237 $spc = substr($spc, 1, 1);
240 if ($myconfig->{dateformat} =~ /^yy/) {
241 ($yy, $mm, $dd) = split /\D/, $date;
243 if ($myconfig->{dateformat} =~ /^mm/) {
244 ($mm, $dd, $yy) = split /\D/, $date;
246 if ($myconfig->{dateformat} =~ /^dd/) {
247 ($dd, $mm, $yy) = split /\D/, $date;
250 $date = substr($date, 2);
251 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
256 $yy = ($yy < 70) ? $yy + 2000 : $yy;
257 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
259 if ($myconfig->{dateformat} =~ /^dd/) {
260 if (defined $longformat && $longformat == 0) {
262 $dd = "0$dd" if ($dd < 10);
263 $mm = "0$mm" if ($mm < 10);
264 $longdate = "$dd$spc$mm$spc$yy";
267 $longdate .= ($spc eq '.') ? ". " : " ";
268 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
270 } elsif ($myconfig->{dateformat} eq "yyyy-mm-dd") {
272 # Use German syntax with the ISO date style "yyyy-mm-dd" because
273 # Lx-Office is mainly used in Germany or German speaking countries.
274 if (defined $longformat && $longformat == 0) {
276 $dd = "0$dd" if ($dd < 10);
277 $mm = "0$mm" if ($mm < 10);
278 $longdate = "$yy-$mm-$dd";
281 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
284 if (defined $longformat && $longformat == 0) {
286 $dd = "0$dd" if ($dd < 10);
287 $mm = "0$mm" if ($mm < 10);
288 $longdate = "$mm$spc$dd$spc$yy";
290 $longdate = &text($self, $self->{$longmonth}[$mm]) . " $dd, $yy";
296 $main::lxdebug->leave_sub();
302 $main::lxdebug->enter_sub();
304 my ($self, $myconfig, $date, $longformat) = @_;
307 $main::lxdebug->leave_sub();
312 $spc = $myconfig->{dateformat};
314 $spc = substr($spc, 1, 1);
317 if ($myconfig->{dateformat} =~ /^yy/) {
318 ($yy, $mm, $dd) = split /\D/, $date;
319 } elsif ($myconfig->{dateformat} =~ /^mm/) {
320 ($mm, $dd, $yy) = split /\D/, $date;
321 } elsif ($myconfig->{dateformat} =~ /^dd/) {
322 ($dd, $mm, $yy) = split /\D/, $date;
325 $date = substr($date, 2);
326 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
331 $yy = ($yy < 70) ? $yy + 2000 : $yy;
332 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
334 $main::lxdebug->leave_sub();
335 return ($yy, $mm, $dd);
339 $main::lxdebug->enter_sub();
341 my ($self, $myconfig, $date, $output_format, $longformat) = @_;
343 $main::lxdebug->leave_sub() and return "" unless ($date);
345 my ($yy, $mm, $dd) = $self->parse_date($myconfig, $date);
347 $output_format =~ /d+/;
348 substr($output_format, $-[0], $+[0] - $-[0]) =
349 sprintf("%0" . (length($&)) . "d", $dd);
351 $output_format =~ /m+/;
352 substr($output_format, $-[0], $+[0] - $-[0]) =
353 sprintf("%0" . (length($&)) . "d", $mm);
355 $output_format =~ /y+/;
356 if (length($&) == 2) {
357 $yy -= $yy >= 2000 ? 2000 : 1900;
359 substr($output_format, $-[0], $+[0] - $-[0]) =
360 sprintf("%0" . (length($&)) . "d", $yy);
362 $main::lxdebug->leave_sub();
364 return $output_format;
367 sub quote_special_chars {
369 my $format = lc shift;
372 if ($self->{special_chars_map} && $self->{special_chars_map}->{$format} && $self->{special_chars_map}->{$format}->{order}) {
373 my $scmap = $self->{special_chars_map}->{$format};
375 map { $string =~ s/\Q${_}\E/$scmap->{$_}/g } @{ $scmap->{order} };
381 sub unquote_special_chars {
385 return $self->quote_special_chars("${format}-reverse", shift);
388 sub remap_special_chars {
390 my $src_format = shift;
391 my $dst_format = shift;
393 return $self->quote_special_chars($dst_format, $self->quote_special_chars("${src_format}-reverse", shift));