File Coverage

blib/lib/EB/Locale.pm
Criterion Covered Total %
statement 34 51 66.6
branch 10 20 50.0
condition n/a
subroutine 8 9 88.8
pod 0 3 0.0
total 52 83 62.6


line stmt bran cond sub pod time code
1             #! perl
2              
3             # Locale.pm -- EB Locale setup (core version)
4             # Author : Johan Vromans
5             # Created On : Fri Sep 16 20:27:25 2005
6             # Last Modified By: Johan Vromans
7             # Last Modified On: Tue Aug 14 12:28:39 2012
8             # Update Count : 165
9             # Status : Unknown, Use with caution!
10              
11             package EB::Locale;
12              
13             # IMPORTANT:
14             #
15             # This module is used (require-d) by module EB only.
16             # No other modules should try to play localisation tricks.
17             #
18             # Note: Only _T must be defined. The rest is defined in EB::Utils.
19              
20 6     6   19 use strict;
  6         6  
  6         155  
21              
22 6     6   18 use constant COREPACKAGE => "ebcore";
  6         6  
  6         343  
23              
24 6     6   20 use base qw(Exporter);
  6         7  
  6         547  
25              
26             our @EXPORT_OK = qw(_T);
27             our @EXPORT = @EXPORT_OK;
28              
29             # This module supports three different gettext implementations.
30              
31 6     6   878 use POSIX qw(setlocale);
  6         8345  
  6         30  
32              
33             my $core_localiser;
34             our $LOCALISER; # for outside checking
35              
36             sub LC_MESSAGES {
37 12 50   12 0 16 eval { POSIX::LC_MESSAGES() } || 5;
  12         876  
38             }
39              
40             sub __init__ {
41 6 50   6   17 return if $core_localiser;
42              
43             # Since EB is use-ing Locale, we cannot use the EB exported libfile yet.
44 6         17 my $dir = EB::libfile("locale");
45              
46             # Use outer settings.
47 6         12 setlocale(LC_MESSAGES);
48              
49             # Try Locale::gettext
50 6 50       6 eval {
51 6         1201 require Locale::gettext;
52 0         0 $core_localiser = Locale::gettext->domain(COREPACKAGE);
53 0         0 $core_localiser->dir($dir);
54 0         0 eval 'sub _T { $core_localiser->get($_[0]) }';
55 0         0 $LOCALISER = "Locale::gettext";
56             } and return;
57              
58             # Try Locale::Messages (part of libintl-perl).
59 6 50       12 eval {
60 6         938 require Locale::Messages;
61 0         0 Locale::Messages::bindtextdomain( COREPACKAGE, $dir );
62 0         0 Locale::Messages::textdomain(COREPACKAGE);
63 0         0 eval 'sub _T { package Locale::Messages; turn_utf_8_on(gettext($_[0])) }';
64 0         0 $LOCALISER = "Locale::Messages";
65             } and return;
66 6 50       20 return if $core_localiser;
67              
68             # Try Locale::gettext_xs (part of libintl-perl).
69 6 50       7 eval {
70 6         838 require Locale::gettext_xs;
71 0         0 Locale::gettext_xs::bindtextdomain( COREPACKAGE, $dir );
72 0         0 Locale::gettext_xs::textdomain(COREPACKAGE);
73 0         0 eval 'sub _T { Locale::gettext_xs::gettext($_[0]) }';
74 0         0 $LOCALISER = "Locale::gettext_xs";
75             } and return;
76 6 50       19 return if $core_localiser;
77              
78             # Try Locale::gettext_pp (part of libintl-perl).
79 6 50       7 eval {
80 6         2087 require Locale::gettext_pp;
81 0         0 Locale::gettext_pp::bindtextdomain( COREPACKAGE, $dir );
82 0         0 Locale::gettext_pp::textdomain(COREPACKAGE);
83 0         0 eval 'sub _T { Locale::gettext_pp::gettext($_[0]) }';
84 0         0 $LOCALISER = "Locale::gettext_pp";
85             } and return;
86 6 50       19 return if $core_localiser;
87              
88             # Fallback to none.
89 6 50       14 unless ( $core_localiser ) {
90 6         9 $core_localiser = "";
91 6     61   186 eval 'sub _T { $_[0] };';
  61         210  
92 6         17 $LOCALISER = "";
93             }
94             }
95              
96             sub get_language {
97 0     0 0 0 $ENV{LANG};
98             }
99              
100             sub set_language {
101             # Set/change language.
102 6     6 0 12 setlocale( LC_MESSAGES, $ENV{LANG} = $_[1] );
103             }
104              
105             __init__();
106              
107             1;