summaryrefslogtreecommitdiff
path: root/LedgerSMB/Locale.pm
blob: c89fbef086c9ca72057ace69a16be3fb0e87601a (plain)
  1. #=====================================================================
  2. #
  3. # Locale support module for LedgerSMB
  4. # LedgerSMB::Locale
  5. #
  6. # LedgerSMB
  7. # Small Medium Business Accounting software
  8. # http://www.ledgersmb.org/
  9. #
  10. #
  11. # Copyright (C) 2006
  12. # This work contains copyrighted information from a number of sources all used
  13. # with permission. It is released under the GNU General Public License
  14. # Version 2 or, at your option, any later version. See COPYRIGHT file for
  15. # details.
  16. #
  17. #
  18. #======================================================================
  19. # This package contains locale related functions:
  20. #`
  21. # get_handle - gets a locale handle
  22. # text - outputs HTML escaped translation for input text
  23. # date - formats date for the locale
  24. #
  25. #====================================================================
  26. package LedgerSMB::Locale;
  27. use base 'Locale::Maketext';
  28. use Locale::Maketext::Lexicon;
  29. use HTML::Entities;
  30. use Encode;
  31. Locale::Maketext::Lexicon->import(
  32. {
  33. '*' => [ Gettext => "${LedgerSMB::Sysconfig::localepath}/*.po", ],
  34. _auto => 1,
  35. _decode => 1,
  36. }
  37. );
  38. sub text {
  39. my ( $self, $text, @params ) = @_;
  40. return encode_entities( $self->maketext( $text, @params ) );
  41. }
  42. ##sub date {
  43. ## my ($self, $myconfig, $date, $longformat) = @_;
  44. ## return $date;
  45. ##}
  46. sub date {
  47. my ( $self, $myconfig, $date, $longformat ) = @_;
  48. my @longmonth = (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
  49. @longmonth = (
  50. "January", "February", "March", "April",
  51. "May ", "June", "July", "August",
  52. "September", "October", "November", "December"
  53. ) if $longformat;
  54. my $longdate = '';
  55. return '' if not $date;
  56. my $spc = '';
  57. my $yy = '';
  58. my $mm = '';
  59. my $dd = '';
  60. # get separator
  61. $spc = $myconfig->{dateformat};
  62. $spc =~ s/\w//g;
  63. $spc = substr( $spc, 0, 1 );
  64. if ( !$longformat && $date =~ /^\d{4}\D/ ) { # reparsing date at this point
  65. # causes problems!
  66. return $date;
  67. }
  68. if ( $date =~ /\D/ ) {
  69. if ( $myconfig->{dateformat} =~ /^yy/ ) {
  70. ( $yy, $mm, $dd ) = split /\D/, $date;
  71. }
  72. if ( $myconfig->{dateformat} =~ /^mm/ ) {
  73. ( $mm, $dd, $yy ) = split /\D/, $date;
  74. }
  75. if ( $myconfig->{dateformat} =~ /^dd/ ) {
  76. ( $dd, $mm, $yy ) = split /\D/, $date;
  77. }
  78. }
  79. else {
  80. $date = substr( $date, 2 );
  81. ( $yy, $mm, $dd ) = ( $date =~ /(..)(..)(..)/ );
  82. }
  83. $dd *= 1;
  84. $mm--;
  85. $yy += 2000 if length $yy == 2;
  86. if ( $myconfig->{dateformat} =~ /^dd/ ) {
  87. $mm++;
  88. $dd = substr( "0$dd", -2 );
  89. $mm = substr( "0$mm", -2 );
  90. $longdate = "$dd$spc$mm$spc$yy";
  91. if ( defined $longformat ) {
  92. $longdate = "$dd";
  93. $longdate .= ( $spc eq '.' ) ? ". " : " ";
  94. $longdate .= &text( $self, $longmonth[ --$mm ] ) . " $yy";
  95. }
  96. }
  97. elsif ( $myconfig->{dateformat} =~ /^yy/ ) {
  98. $mm++;
  99. $dd = substr( "0$dd", -2 );
  100. $mm = substr( "0$mm", -2 );
  101. $longdate = "$yy$spc$mm$spc$dd";
  102. }
  103. else {
  104. $mm++;
  105. $dd = substr( "0$dd", -2 );
  106. $mm = substr( "0$mm", -2 );
  107. $longdate = "$mm$spc$dd$spc$yy";
  108. }
  109. if ( defined $longformat ) {
  110. $longdate = &text( $self, $longmonth[ --$mm ] ) . " $dd $yy";
  111. }
  112. $longdate;
  113. }
  114. 1;