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);
49 $main::lxdebug->enter_sub();
51 my ($type, $country) = @_;
53 $country ||= $::language;
60 $self->_init($country);
62 $main::lxdebug->leave_sub();
71 $self->{charset} = Common::DEFAULT_CHARSET;
72 $self->{countrycode} = $country;
74 if ($country && -d "locale/$country") {
76 if (open(IN, "<", "locale/$country/all")) {
77 my $code = join("", <IN>);
82 if (open IN, "<", "locale/$country/charset") {
83 $self->{charset} = <IN>;
86 chomp $self->{charset};
90 my $db_charset = $main::dbcharset || Common::DEFAULT_CHARSET;
92 $self->{iconv} = Text::Iconv->new($self->{charset}, $db_charset);
93 $self->{iconv_reverse} = Text::Iconv->new($db_charset, $self->{charset});
94 $self->{iconv_english} = Text::Iconv->new('ASCII', $db_charset);
95 $self->{iconv_iso8859} = Text::Iconv->new('ISO-8859-15', $db_charset);
96 $self->{iconv_to_iso8859} = Text::Iconv->new($db_charset, 'ISO-8859-15');
98 $self->_read_special_chars_file($country);
100 push @{ $self->{LONG_MONTH} },
101 ("January", "February", "March", "April",
102 "May ", "June", "July", "August",
103 "September", "October", "November", "December");
104 push @{ $self->{SHORT_MONTH} },
105 (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
115 for (my $i = 0; $i < length $str; $i++) {
116 my $char = substr $str, $i, 1;
122 } elsif ($char eq 'r') {
125 } elsif ($char eq 's') {
128 } elsif ($char eq 'x') {
129 $new_str .= chr(hex(substr($str, $i + 1, 2)));
138 } elsif ($char eq '\\') {
149 sub _read_special_chars_file {
153 if (! -f "locale/$country/special_chars") {
154 $self->{special_chars_map} = {};
158 $self->{special_chars_map} = Inifile->new("locale/$country/special_chars", 'verbatim' => 1);
160 foreach my $format (keys %{ $self->{special_chars_map} }) {
161 next if (($format eq 'FILE') || ($format eq 'ORDER') || (ref $self->{special_chars_map}->{$format} ne 'HASH'));
163 if ($format ne lc $format) {
164 $self->{special_chars_map}->{lc $format} = $self->{special_chars_map}->{$format};
165 delete $self->{special_chars_map}->{$format};
166 $format = lc $format;
169 my $scmap = $self->{special_chars_map}->{$format};
170 my $order = $self->{iconv}->convert($scmap->{order});
171 delete $scmap->{order};
173 foreach my $key (keys %{ $scmap }) {
174 $scmap->{$key} = $self->_handle_markup($self->{iconv}->convert($scmap->{$key}));
176 my $new_key = $self->_handle_markup($self->{iconv}->convert($key));
178 if ($key ne $new_key) {
179 $scmap->{$new_key} = $scmap->{$key};
180 delete $scmap->{$key};
184 $self->{special_chars_map}->{"${format}-reverse"} = { reverse %{ $scmap } };
186 $scmap->{order} = [ map { $self->_handle_markup($_) } split m/\s+/, $order ];
187 $self->{special_chars_map}->{"${format}-reverse"}->{order} = [ grep { $_ } map { $scmap->{$_} } reverse @{ $scmap->{order} } ];
195 if (exists $self->{texts}->{$text}) {
196 $text = $self->{iconv}->convert($self->{texts}->{$text});
198 $text = $self->{iconv_english}->convert($text);
202 $text = Form->format_string($text, @_);
209 $main::lxdebug->enter_sub();
211 my ($self, $text) = @_;
212 my $text_rev = lc $self->{iconv_reverse}->convert($text);
214 if (!$self->{texts_reverse}) {
215 $self->{texts_reverse} = { };
216 while (my ($original, $translation) = each %{ $self->{texts} }) {
217 $original = lc $original;
218 $original =~ s/[^a-z0-9]/_/g;
219 $original =~ s/_+/_/g;
221 $translation = lc $translation;
223 $self->{texts_reverse}->{$translation} ||= [ ];
224 push @{ $self->{texts_reverse}->{$translation} }, $original;
228 my $sub_name = first { defined &{ "::$_" } } @{ $self->{texts_reverse}->{$text_rev} } if $self->{texts_reverse}->{$text_rev};
229 $sub_name ||= $text_rev if ($text_rev =~ m/^[a-z][a-z0-9_]+$/) && defined &{ "::$text_rev" };
231 $main::form->error("$text not defined in locale/$self->{countrycode}/all") if !$sub_name;
233 $main::lxdebug->leave_sub();
239 $main::lxdebug->enter_sub();
241 my ($self, $myconfig, $date, $longformat) = @_;
244 my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
246 my ($spc, $yy, $mm, $dd);
251 $spc = $myconfig->{dateformat};
253 $spc = substr($spc, 1, 1);
256 if ($myconfig->{dateformat} =~ /^yy/) {
257 ($yy, $mm, $dd) = split /\D/, $date;
259 if ($myconfig->{dateformat} =~ /^mm/) {
260 ($mm, $dd, $yy) = split /\D/, $date;
262 if ($myconfig->{dateformat} =~ /^dd/) {
263 ($dd, $mm, $yy) = split /\D/, $date;
266 $date = substr($date, 2);
267 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
272 $yy = ($yy < 70) ? $yy + 2000 : $yy;
273 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
275 if ($myconfig->{dateformat} =~ /^dd/) {
276 if (defined $longformat && $longformat == 0) {
278 $dd = "0$dd" if ($dd < 10);
279 $mm = "0$mm" if ($mm < 10);
280 $longdate = "$dd$spc$mm$spc$yy";
283 $longdate .= ($spc eq '.') ? ". " : " ";
284 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
286 } elsif ($myconfig->{dateformat} eq "yyyy-mm-dd") {
288 # Use German syntax with the ISO date style "yyyy-mm-dd" because
289 # Lx-Office is mainly used in Germany or German speaking countries.
290 if (defined $longformat && $longformat == 0) {
292 $dd = "0$dd" if ($dd < 10);
293 $mm = "0$mm" if ($mm < 10);
294 $longdate = "$yy-$mm-$dd";
297 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
300 if (defined $longformat && $longformat == 0) {
302 $dd = "0$dd" if ($dd < 10);
303 $mm = "0$mm" if ($mm < 10);
304 $longdate = "$mm$spc$dd$spc$yy";
306 $longdate = &text($self, $self->{$longmonth}[$mm]) . " $dd, $yy";
312 $main::lxdebug->leave_sub();
318 $main::lxdebug->enter_sub();
320 my ($self, $myconfig, $date, $longformat) = @_;
321 my ($spc, $yy, $mm, $dd);
324 $main::lxdebug->leave_sub();
329 $spc = $myconfig->{dateformat};
331 $spc = substr($spc, 1, 1);
334 if ($myconfig->{dateformat} =~ /^yy/) {
335 ($yy, $mm, $dd) = split /\D/, $date;
336 } elsif ($myconfig->{dateformat} =~ /^mm/) {
337 ($mm, $dd, $yy) = split /\D/, $date;
338 } elsif ($myconfig->{dateformat} =~ /^dd/) {
339 ($dd, $mm, $yy) = split /\D/, $date;
342 $date = substr($date, 2);
343 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
348 $yy = ($yy < 70) ? $yy + 2000 : $yy;
349 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
351 $main::lxdebug->leave_sub();
352 return ($yy, $mm, $dd);
356 $main::lxdebug->enter_sub();
358 my ($self, $myconfig, $date, $output_format, $longformat) = @_;
360 $main::lxdebug->leave_sub() and return "" unless ($date);
362 my ($yy, $mm, $dd) = $self->parse_date($myconfig, $date);
364 $output_format =~ /d+/;
365 substr($output_format, $-[0], $+[0] - $-[0]) =
366 sprintf("%0" . (length($&)) . "d", $dd);
368 $output_format =~ /m+/;
369 substr($output_format, $-[0], $+[0] - $-[0]) =
370 sprintf("%0" . (length($&)) . "d", $mm);
372 $output_format =~ /y+/;
373 substr($output_format, $-[0], $+[0] - $-[0]) = $yy;
375 $main::lxdebug->leave_sub();
377 return $output_format;
380 sub quote_special_chars {
382 my $format = lc shift;
385 if ($self->{special_chars_map} && $self->{special_chars_map}->{$format} && $self->{special_chars_map}->{$format}->{order}) {
386 my $scmap = $self->{special_chars_map}->{$format};
388 map { $string =~ s/\Q${_}\E/$scmap->{$_}/g } @{ $scmap->{order} };
394 sub unquote_special_chars {
398 return $self->quote_special_chars("${format}-reverse", shift);
401 sub remap_special_chars {
403 my $src_format = shift;
404 my $dst_format = shift;
406 return $self->quote_special_chars($dst_format, $self->quote_special_chars("${src_format}-reverse", shift));