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 #======================================================================
41 use List::Util qw(first);
42 use List::MoreUtils qw(any);
51 my %locales_by_country;
54 $main::lxdebug->enter_sub();
56 my ($type, $country) = @_;
58 $country ||= $::language;
62 if (!$locales_by_country{$country}) {
66 $self->_init($country);
68 $locales_by_country{$country} = $self;
71 $main::lxdebug->leave_sub();
73 return $locales_by_country{$country}
80 $self->{charset} = Common::DEFAULT_CHARSET;
81 $self->{countrycode} = $country;
83 if ($country && -d "locale/$country") {
85 if (open(IN, "<", "locale/$country/all")) {
86 my $code = join("", <IN>);
91 if (open IN, "<", "locale/$country/charset") {
92 $self->{charset} = <IN>;
95 chomp $self->{charset};
99 my $db_charset = $main::dbcharset || Common::DEFAULT_CHARSET;
100 $self->{is_utf8} = (any { lc($::dbcharset || '') eq $_ } qw(utf8 utf-8 unicode)) ? 1 : 0;
102 if ($self->{is_utf8}) {
103 binmode STDOUT, ":utf8";
104 binmode STDERR, ":utf8";
107 $self->{iconv} = SL::Iconv->new($self->{charset}, $db_charset);
108 $self->{iconv_reverse} = SL::Iconv->new($db_charset, $self->{charset});
109 $self->{iconv_english} = SL::Iconv->new('ASCII', $db_charset);
110 $self->{iconv_iso8859} = SL::Iconv->new('ISO-8859-15', $db_charset);
111 $self->{iconv_to_iso8859} = SL::Iconv->new($db_charset, 'ISO-8859-15');
113 $self->_read_special_chars_file($country);
115 push @{ $self->{LONG_MONTH} },
116 ("January", "February", "March", "April",
117 "May ", "June", "July", "August",
118 "September", "October", "November", "December");
119 push @{ $self->{SHORT_MONTH} },
120 (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
126 return $self->{is_utf8} && (!$handle || $handle->is_utf8);
136 for (my $i = 0; $i < length $str; $i++) {
137 my $char = substr $str, $i, 1;
143 } elsif ($char eq 'r') {
146 } elsif ($char eq 's') {
149 } elsif ($char eq 'x') {
150 $new_str .= chr(hex(substr($str, $i + 1, 2)));
159 } elsif ($char eq '\\') {
170 sub _read_special_chars_file {
174 if (! -f "locale/$country/special_chars") {
175 $self->{special_chars_map} = {};
179 $self->{special_chars_map} = Inifile->new("locale/$country/special_chars", 'verbatim' => 1);
181 foreach my $format (keys %{ $self->{special_chars_map} }) {
182 next if (($format eq 'FILE') || ($format eq 'ORDER') || (ref $self->{special_chars_map}->{$format} ne 'HASH'));
184 if ($format ne lc $format) {
185 $self->{special_chars_map}->{lc $format} = $self->{special_chars_map}->{$format};
186 delete $self->{special_chars_map}->{$format};
187 $format = lc $format;
190 my $scmap = $self->{special_chars_map}->{$format};
191 my $order = $self->{iconv}->convert($scmap->{order});
192 delete $scmap->{order};
194 foreach my $key (keys %{ $scmap }) {
195 $scmap->{$key} = $self->_handle_markup($self->{iconv}->convert($scmap->{$key}));
197 my $new_key = $self->_handle_markup($self->{iconv}->convert($key));
199 if ($key ne $new_key) {
200 $scmap->{$new_key} = $scmap->{$key};
201 delete $scmap->{$key};
205 $self->{special_chars_map}->{"${format}-reverse"} = { reverse %{ $scmap } };
207 $scmap->{order} = [ map { $self->_handle_markup($_) } split m/\s+/, $order ];
208 $self->{special_chars_map}->{"${format}-reverse"}->{order} = [ grep { $_ } map { $scmap->{$_} } reverse @{ $scmap->{order} } ];
216 if (exists $self->{texts}->{$text}) {
217 $text = $self->{iconv}->convert($self->{texts}->{$text});
219 $text = $self->{iconv_english}->convert($text);
223 $text = Form->format_string($text, @_);
230 $main::lxdebug->enter_sub();
232 my ($self, $text) = @_;
233 my $text_rev = lc $self->{iconv_reverse}->convert($text);
235 if (!$self->{texts_reverse}) {
236 $self->{texts_reverse} = { };
237 while (my ($original, $translation) = each %{ $self->{texts} }) {
238 $original = lc $original;
239 $original =~ s/[^a-z0-9]/_/g;
240 $original =~ s/_+/_/g;
242 $translation = lc $translation;
244 $self->{texts_reverse}->{$translation} ||= [ ];
245 push @{ $self->{texts_reverse}->{$translation} }, $original;
250 $sub_name = first { defined(&{ "::${_}" }) } @{ $self->{texts_reverse}->{$text_rev} } if $self->{texts_reverse}->{$text_rev};
251 $sub_name ||= $text_rev if ($text_rev =~ m/^[a-z][a-z0-9_]+$/) && defined &{ "::${text_rev}" };
253 $main::form->error("$text not defined in locale/$self->{countrycode}/all") if !$sub_name;
255 $main::lxdebug->leave_sub();
261 $main::lxdebug->enter_sub();
263 my ($self, $myconfig, $date, $longformat) = @_;
266 my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
268 my ($spc, $yy, $mm, $dd);
273 $spc = $myconfig->{dateformat};
275 $spc = substr($spc, 1, 1);
278 if ($myconfig->{dateformat} =~ /^yy/) {
279 ($yy, $mm, $dd) = split /\D/, $date;
281 if ($myconfig->{dateformat} =~ /^mm/) {
282 ($mm, $dd, $yy) = split /\D/, $date;
284 if ($myconfig->{dateformat} =~ /^dd/) {
285 ($dd, $mm, $yy) = split /\D/, $date;
288 $date = substr($date, 2);
289 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
294 $yy = ($yy < 70) ? $yy + 2000 : $yy;
295 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
297 if ($myconfig->{dateformat} =~ /^dd/) {
298 if (defined $longformat && $longformat == 0) {
300 $dd = "0$dd" if ($dd < 10);
301 $mm = "0$mm" if ($mm < 10);
302 $longdate = "$dd$spc$mm$spc$yy";
305 $longdate .= ($spc eq '.') ? ". " : " ";
306 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
308 } elsif ($myconfig->{dateformat} eq "yyyy-mm-dd") {
310 # Use German syntax with the ISO date style "yyyy-mm-dd" because
311 # Lx-Office is mainly used in Germany or German speaking countries.
312 if (defined $longformat && $longformat == 0) {
314 $dd = "0$dd" if ($dd < 10);
315 $mm = "0$mm" if ($mm < 10);
316 $longdate = "$yy-$mm-$dd";
319 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
322 if (defined $longformat && $longformat == 0) {
324 $dd = "0$dd" if ($dd < 10);
325 $mm = "0$mm" if ($mm < 10);
326 $longdate = "$mm$spc$dd$spc$yy";
328 $longdate = &text($self, $self->{$longmonth}[$mm]) . " $dd, $yy";
334 $main::lxdebug->leave_sub();
340 $main::lxdebug->enter_sub();
342 my ($self, $myconfig, $date, $longformat) = @_;
343 my ($spc, $yy, $mm, $dd);
346 $main::lxdebug->leave_sub();
351 $spc = $myconfig->{dateformat};
353 $spc = substr($spc, 1, 1);
356 if ($myconfig->{dateformat} =~ /^yy/) {
357 ($yy, $mm, $dd) = split /\D/, $date;
358 } elsif ($myconfig->{dateformat} =~ /^mm/) {
359 ($mm, $dd, $yy) = split /\D/, $date;
360 } elsif ($myconfig->{dateformat} =~ /^dd/) {
361 ($dd, $mm, $yy) = split /\D/, $date;
364 $date = substr($date, 2);
365 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
370 $yy = ($yy < 70) ? $yy + 2000 : $yy;
371 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
373 $main::lxdebug->leave_sub();
374 return ($yy, $mm, $dd);
377 sub parse_date_to_object {
379 my ($yy, $mm, $dd) = $self->parse_date(@_);
381 return $yy && $mm && $dd ? DateTime->new(year => $yy, month => $mm, day => $dd) : undef;
385 $main::lxdebug->enter_sub();
387 my ($self, $myconfig, $date, $output_format, $longformat) = @_;
389 $main::lxdebug->leave_sub() and return "" unless ($date);
391 my ($yy, $mm, $dd) = $self->parse_date($myconfig, $date);
393 $output_format =~ /d+/;
394 substr($output_format, $-[0], $+[0] - $-[0]) =
395 sprintf("%0" . (length($&)) . "d", $dd);
397 $output_format =~ /m+/;
398 substr($output_format, $-[0], $+[0] - $-[0]) =
399 sprintf("%0" . (length($&)) . "d", $mm);
401 $output_format =~ /y+/;
402 substr($output_format, $-[0], $+[0] - $-[0]) = $yy;
404 $main::lxdebug->leave_sub();
406 return $output_format;
410 $main::lxdebug->enter_sub();
413 my $myconfig = shift;
417 my $yy_len = shift || 4;
419 $main::lxdebug->leave_sub() and return "" unless $yy && $mm && $dd;
421 $yy = $yy % 100 if 2 == $yy_len;
423 my $format = ref $myconfig eq '' ? "$myconfig" : $myconfig->{dateformat};
424 $format =~ s{ d+ }{ sprintf("%0" . (length($&)) . "d", $dd) }gex;
425 $format =~ s{ m+ }{ sprintf("%0" . (length($&)) . "d", $mm) }gex;
426 $format =~ s{ y+ }{ sprintf("%0${yy_len}d", $yy) }gex;
428 $main::lxdebug->leave_sub();
433 sub quote_special_chars {
435 my $format = lc shift;
438 if ($self->{special_chars_map} && $self->{special_chars_map}->{$format} && $self->{special_chars_map}->{$format}->{order}) {
439 my $scmap = $self->{special_chars_map}->{$format};
441 map { $string =~ s/\Q${_}\E/$scmap->{$_}/g } @{ $scmap->{order} };
447 sub unquote_special_chars {
451 return $self->quote_special_chars("${format}-reverse", shift);
454 sub remap_special_chars {
456 my $src_format = shift;
457 my $dst_format = shift;
459 return $self->quote_special_chars($dst_format, $self->quote_special_chars("${src_format}-reverse", shift));
469 binmode $fh, ":utf8" if $self->is_utf8;