summaryrefslogtreecommitdiff
path: root/LedgerSMB/Locale.pm
blob: 6871044539f325bc56568f95c4e942ec32745f2a (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. ],
  35. _auto => 1,
  36. _decode => 1,
  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 = ("January", "February", "March", "April", "May ", "June",
  50. "July", "August", "September", "October", "November",
  51. "December") if $longformat;
  52. my $longdate = '';
  53. return '' if not $date;
  54. my $spc = '';
  55. my $yy = '';
  56. my $mm = '';
  57. my $dd = '';
  58. # get separator
  59. $spc = $myconfig->{dateformat};
  60. $spc =~ s/\w//g;
  61. $spc = substr($spc, 0, 1);
  62. if (!$longformat && $date =~ /^\d{4}\D/){ # reparsing date at this point
  63. # causes problems!
  64. return $date;
  65. }
  66. if ($date =~ /\D/) {
  67. if ($myconfig->{dateformat} =~ /^yy/) {
  68. ($yy, $mm, $dd) = split /\D/, $date;
  69. }
  70. if ($myconfig->{dateformat} =~ /^mm/) {
  71. ($mm, $dd, $yy) = split /\D/, $date;
  72. }
  73. if ($myconfig->{dateformat} =~ /^dd/) {
  74. ($dd, $mm, $yy) = split /\D/, $date;
  75. }
  76. } else {
  77. $date = substr($date, 2);
  78. ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
  79. }
  80. $dd *= 1;
  81. $mm--;
  82. $yy += 2000 if length $yy == 2;
  83. if ($myconfig->{dateformat} =~ /^dd/) {
  84. $mm++;
  85. $dd = substr("0$dd", -2);
  86. $mm = substr("0$mm", -2);
  87. $longdate = "$dd$spc$mm$spc$yy";
  88. if (defined $longformat) {
  89. $longdate = "$dd";
  90. $longdate .= ($spc eq '.') ? ". " : " ";
  91. $longdate .= &text($self, $longmonth[--$mm])." $yy";
  92. }
  93. } elsif ($myconfig->{dateformat} =~ /^yy/) {
  94. $mm++;
  95. $dd = substr("0$dd", -2);
  96. $mm = substr("0$mm", -2);
  97. $longdate = "$yy$spc$mm$spc$dd";
  98. } else {
  99. $mm++;
  100. $dd = substr("0$dd", -2);
  101. $mm = substr("0$mm", -2);
  102. $longdate = "$mm$spc$dd$spc$yy";
  103. }
  104. if (defined $longformat) {
  105. $longdate = &text($self, $longmonth[--$mm])." $dd $yy";
  106. }
  107. $longdate;
  108. }
  109. 1;