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 #======================================================================
40 use List::Util qw(first);
41 use List::MoreUtils qw(any);
51 $main::lxdebug->enter_sub();
53 my ($type, $country) = @_;
55 $country ||= $::language;
62 $self->_init($country);
64 $main::lxdebug->leave_sub();
73 $self->{charset} = Common::DEFAULT_CHARSET;
74 $self->{countrycode} = $country;
76 if ($country && -d "locale/$country") {
78 if (open(IN, "<", "locale/$country/all")) {
79 my $code = join("", <IN>);
84 if (open IN, "<", "locale/$country/charset") {
85 $self->{charset} = <IN>;
88 chomp $self->{charset};
92 my $db_charset = $main::dbcharset || Common::DEFAULT_CHARSET;
93 $self->{is_utf8} = (any { lc($::dbcharset || '') eq $_ } qw(utf8 utf-8 unicode)) ? 1 : 0;
95 if ($self->{is_utf8}) {
96 binmode STDOUT, ":utf8";
97 binmode STDERR, ":utf8";
100 $self->{iconv} = SL::Iconv->new($self->{charset}, $db_charset);
101 $self->{iconv_reverse} = SL::Iconv->new($db_charset, $self->{charset});
102 $self->{iconv_english} = SL::Iconv->new('ASCII', $db_charset);
103 $self->{iconv_iso8859} = SL::Iconv->new('ISO-8859-15', $db_charset);
104 $self->{iconv_to_iso8859} = SL::Iconv->new($db_charset, 'ISO-8859-15');
106 $self->_read_special_chars_file($country);
108 push @{ $self->{LONG_MONTH} },
109 ("January", "February", "March", "April",
110 "May ", "June", "July", "August",
111 "September", "October", "November", "December");
112 push @{ $self->{SHORT_MONTH} },
113 (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
119 return $self->{is_utf8} && (!$handle || $handle->is_utf8);
129 for (my $i = 0; $i < length $str; $i++) {
130 my $char = substr $str, $i, 1;
136 } elsif ($char eq 'r') {
139 } elsif ($char eq 's') {
142 } elsif ($char eq 'x') {
143 $new_str .= chr(hex(substr($str, $i + 1, 2)));
152 } elsif ($char eq '\\') {
163 sub _read_special_chars_file {
167 if (! -f "locale/$country/special_chars") {
168 $self->{special_chars_map} = {};
172 $self->{special_chars_map} = Inifile->new("locale/$country/special_chars", 'verbatim' => 1);
174 foreach my $format (keys %{ $self->{special_chars_map} }) {
175 next if (($format eq 'FILE') || ($format eq 'ORDER') || (ref $self->{special_chars_map}->{$format} ne 'HASH'));
177 if ($format ne lc $format) {
178 $self->{special_chars_map}->{lc $format} = $self->{special_chars_map}->{$format};
179 delete $self->{special_chars_map}->{$format};
180 $format = lc $format;
183 my $scmap = $self->{special_chars_map}->{$format};
184 my $order = $self->{iconv}->convert($scmap->{order});
185 delete $scmap->{order};
187 foreach my $key (keys %{ $scmap }) {
188 $scmap->{$key} = $self->_handle_markup($self->{iconv}->convert($scmap->{$key}));
190 my $new_key = $self->_handle_markup($self->{iconv}->convert($key));
192 if ($key ne $new_key) {
193 $scmap->{$new_key} = $scmap->{$key};
194 delete $scmap->{$key};
198 $self->{special_chars_map}->{"${format}-reverse"} = { reverse %{ $scmap } };
200 $scmap->{order} = [ map { $self->_handle_markup($_) } split m/\s+/, $order ];
201 $self->{special_chars_map}->{"${format}-reverse"}->{order} = [ grep { $_ } map { $scmap->{$_} } reverse @{ $scmap->{order} } ];
209 if (exists $self->{texts}->{$text}) {
210 $text = $self->{iconv}->convert($self->{texts}->{$text});
212 $text = $self->{iconv_english}->convert($text);
216 $text = Form->format_string($text, @_);
223 $main::lxdebug->enter_sub();
225 my ($self, $text) = @_;
226 my $text_rev = lc $self->{iconv_reverse}->convert($text);
228 if (!$self->{texts_reverse}) {
229 $self->{texts_reverse} = { };
230 while (my ($original, $translation) = each %{ $self->{texts} }) {
231 $original = lc $original;
232 $original =~ s/[^a-z0-9]/_/g;
233 $original =~ s/_+/_/g;
235 $translation = lc $translation;
237 $self->{texts_reverse}->{$translation} ||= [ ];
238 push @{ $self->{texts_reverse}->{$translation} }, $original;
242 my $sub_name = first { defined &{ "::$_" } } @{ $self->{texts_reverse}->{$text_rev} } if $self->{texts_reverse}->{$text_rev};
243 $sub_name ||= $text_rev if ($text_rev =~ m/^[a-z][a-z0-9_]+$/) && defined &{ "::$text_rev" };
245 $main::form->error("$text not defined in locale/$self->{countrycode}/all") if !$sub_name;
247 $main::lxdebug->leave_sub();
253 $main::lxdebug->enter_sub();
255 my ($self, $myconfig, $date, $longformat) = @_;
258 my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
260 my ($spc, $yy, $mm, $dd);
265 $spc = $myconfig->{dateformat};
267 $spc = substr($spc, 1, 1);
270 if ($myconfig->{dateformat} =~ /^yy/) {
271 ($yy, $mm, $dd) = split /\D/, $date;
273 if ($myconfig->{dateformat} =~ /^mm/) {
274 ($mm, $dd, $yy) = split /\D/, $date;
276 if ($myconfig->{dateformat} =~ /^dd/) {
277 ($dd, $mm, $yy) = split /\D/, $date;
280 $date = substr($date, 2);
281 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
286 $yy = ($yy < 70) ? $yy + 2000 : $yy;
287 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
289 if ($myconfig->{dateformat} =~ /^dd/) {
290 if (defined $longformat && $longformat == 0) {
292 $dd = "0$dd" if ($dd < 10);
293 $mm = "0$mm" if ($mm < 10);
294 $longdate = "$dd$spc$mm$spc$yy";
297 $longdate .= ($spc eq '.') ? ". " : " ";
298 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
300 } elsif ($myconfig->{dateformat} eq "yyyy-mm-dd") {
302 # Use German syntax with the ISO date style "yyyy-mm-dd" because
303 # Lx-Office is mainly used in Germany or German speaking countries.
304 if (defined $longformat && $longformat == 0) {
306 $dd = "0$dd" if ($dd < 10);
307 $mm = "0$mm" if ($mm < 10);
308 $longdate = "$yy-$mm-$dd";
311 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
314 if (defined $longformat && $longformat == 0) {
316 $dd = "0$dd" if ($dd < 10);
317 $mm = "0$mm" if ($mm < 10);
318 $longdate = "$mm$spc$dd$spc$yy";
320 $longdate = &text($self, $self->{$longmonth}[$mm]) . " $dd, $yy";
326 $main::lxdebug->leave_sub();
332 $main::lxdebug->enter_sub();
334 my ($self, $myconfig, $date, $longformat) = @_;
335 my ($spc, $yy, $mm, $dd);
338 $main::lxdebug->leave_sub();
343 $spc = $myconfig->{dateformat};
345 $spc = substr($spc, 1, 1);
348 if ($myconfig->{dateformat} =~ /^yy/) {
349 ($yy, $mm, $dd) = split /\D/, $date;
350 } elsif ($myconfig->{dateformat} =~ /^mm/) {
351 ($mm, $dd, $yy) = split /\D/, $date;
352 } elsif ($myconfig->{dateformat} =~ /^dd/) {
353 ($dd, $mm, $yy) = split /\D/, $date;
356 $date = substr($date, 2);
357 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
362 $yy = ($yy < 70) ? $yy + 2000 : $yy;
363 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
365 $main::lxdebug->leave_sub();
366 return ($yy, $mm, $dd);
370 $main::lxdebug->enter_sub();
372 my ($self, $myconfig, $date, $output_format, $longformat) = @_;
374 $main::lxdebug->leave_sub() and return "" unless ($date);
376 my ($yy, $mm, $dd) = $self->parse_date($myconfig, $date);
378 $output_format =~ /d+/;
379 substr($output_format, $-[0], $+[0] - $-[0]) =
380 sprintf("%0" . (length($&)) . "d", $dd);
382 $output_format =~ /m+/;
383 substr($output_format, $-[0], $+[0] - $-[0]) =
384 sprintf("%0" . (length($&)) . "d", $mm);
386 $output_format =~ /y+/;
387 substr($output_format, $-[0], $+[0] - $-[0]) = $yy;
389 $main::lxdebug->leave_sub();
391 return $output_format;
394 sub quote_special_chars {
396 my $format = lc shift;
399 if ($self->{special_chars_map} && $self->{special_chars_map}->{$format} && $self->{special_chars_map}->{$format}->{order}) {
400 my $scmap = $self->{special_chars_map}->{$format};
402 map { $string =~ s/\Q${_}\E/$scmap->{$_}/g } @{ $scmap->{order} };
408 sub unquote_special_chars {
412 return $self->quote_special_chars("${format}-reverse", shift);
415 sub remap_special_chars {
417 my $src_format = shift;
418 my $dst_format = shift;
420 return $self->quote_special_chars($dst_format, $self->quote_special_chars("${src_format}-reverse", shift));
430 binmode $fh, ":utf8" if $self->is_utf8;