summaryrefslogtreecommitdiff
path: root/LedgerSMB/Locale.pm
blob: 0a38961ea1cd7dc4d07c9185111ce5ea7f1b2a30 (plain)
  1. =head1 NAME
  2. LedgerSMB::Locale Locale handling class for LedgerSMB
  3. =head1 SYNOPSIS
  4. Locale support module for LedgerSMB. Uses Locale::Maketext::Lexicon as a base.
  5. =head1 METHODS
  6. =over
  7. =item get_handle ($language_code)
  8. Returns a locale handle for accessing the other methods. Inherited from
  9. Locale::Maketext.
  10. =item text ($string, @params)
  11. Returns the translation for the given string. This is a legacy wrapper that
  12. merely calls $self->maketext.
  13. =item date ($myconfig, $date, $longformat)
  14. Returns the given date after formatting it. $longformat is a ternary flag that
  15. determines how the date is formatted. If $longformat is true, the date will be
  16. given in the form of "_('September') 23 2007". If $longformat is false but
  17. defined, the date will be in the form of "_('Sep') 23 2007" unless the date is
  18. given in the form 'yyyy.mm.dd', in which case it is returned as-is. If
  19. $longformat is not defined, the date will be output in the format specified by
  20. $myconfig->{dateformat}.
  21. =back
  22. =head1 Copyright (C) 2006, The LedgerSMB core team.
  23. #====================================================================
  24. #
  25. # Locale support module for LedgerSMB
  26. # LedgerSMB::Locale
  27. #
  28. # LedgerSMB
  29. # Small Medium Business Accounting software
  30. # http://www.ledgersmb.org/
  31. #
  32. #
  33. # Copyright (C) 2006
  34. # This work contains copyrighted information from a number of sources
  35. # all used with permission. It is released under the GNU General
  36. # Public License Version 2 or, at your option, any later version.
  37. # See COPYRIGHT file for details.
  38. #
  39. # This file contains source code included with or based on SQL-Ledger
  40. # which is Copyright Dieter Simader and DWS Systems Inc. 2000-2005
  41. # and licensed under the GNU General Public License version 2 or, at
  42. # your option, any later version. For a full list including contact
  43. # information of contributors, maintainers, and copyright holders,
  44. # see the CONTRIBUTORS file.
  45. #
  46. # Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
  47. # Copyright (C) 2000
  48. #
  49. # Author: DWS Systems Inc.
  50. # Web: http://www.sql-ledger.org
  51. #
  52. # Contributors: Thomas Bayen <bayen@gmx.de>
  53. # Antti Kaihola <akaihola@siba.fi>
  54. # Moritz Bunkus (tex)
  55. # Jim Rawlings <jim@your-dba.com> (DB2)
  56. #
  57. #====================================================================
  58. =cut
  59. package LedgerSMB::Locale;
  60. use base 'Locale::Maketext';
  61. use Locale::Maketext::Lexicon;
  62. use Encode;
  63. Locale::Maketext::Lexicon->import(
  64. {
  65. '*' => [ Gettext => "${LedgerSMB::Sysconfig::localepath}/*.po", ],
  66. _auto => 1,
  67. _decode => 1,
  68. }
  69. );
  70. sub text {
  71. my ( $self, $text, @params ) = @_;
  72. return $self->maketext( $text, @params );
  73. }
  74. ##sub date {
  75. ## my ($self, $myconfig, $date, $longformat) = @_;
  76. ## return $date;
  77. ##}
  78. sub date {
  79. my ( $self, $myconfig, $date, $longformat ) = @_;
  80. my @longmonth = (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
  81. @longmonth = (
  82. "January", "February", "March", "April",
  83. "May ", "June", "July", "August",
  84. "September", "October", "November", "December"
  85. ) if $longformat;
  86. my $longdate = '';
  87. return '' if not $date;
  88. my $spc = '';
  89. my $yy = '';
  90. my $mm = '';
  91. my $dd = '';
  92. # get separator
  93. $spc = $myconfig->{dateformat};
  94. $spc =~ s/\w//g;
  95. $spc = substr( $spc, 0, 1 );
  96. if ( !$longformat && $date =~ /^\d{4}\D/ ) { # reparsing date at this point
  97. # causes problems!
  98. return $date;
  99. }
  100. if ( $date =~ /\D/ ) {
  101. if ( $myconfig->{dateformat} =~ /^yy/ ) {
  102. ( $yy, $mm, $dd ) = split /\D/, $date;
  103. }
  104. elsif ( $myconfig->{dateformat} =~ /^mm/ ) {
  105. ( $mm, $dd, $yy ) = split /\D/, $date;
  106. }
  107. elsif ( $myconfig->{dateformat} =~ /^dd/ ) {
  108. ( $dd, $mm, $yy ) = split /\D/, $date;
  109. }
  110. }
  111. else {
  112. $date = substr( $date, 2 );
  113. ( $yy, $mm, $dd ) = ( $date =~ /(..)(..)(..)/ );
  114. }
  115. $dd *= 1;
  116. $yy += 2000 if length $yy == 2;
  117. $dd = substr( "0$dd", -2 );
  118. $mm = substr( "0$mm", -2 );
  119. if ( $myconfig->{dateformat} =~ /^dd/ ) {
  120. $longdate = "$dd$spc$mm$spc$yy";
  121. }
  122. elsif ( $myconfig->{dateformat} =~ /^yy/ ) {
  123. $longdate = "$yy$spc$mm$spc$dd";
  124. }
  125. else {
  126. $longdate = "$mm$spc$dd$spc$yy";
  127. }
  128. if ( defined $longformat ) {
  129. $longdate = $self->text( $longmonth[ --$mm ] ) . " $dd $yy";
  130. }
  131. $longdate;
  132. }
  133. 1;