Apache/iNcom/Localizer.pm untested

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 117 CC 12 SCAR 30.0
CC
12
Cov
0%
find_localized_file
56.0
new
6.0
default_lang
6.0
CRAP 20.2
1 #
2 # Localizer.pm - iNcom module responsible for localization.
3 #
4 # This file is part of Apache::iNcom.
5 #
6 # Author: Francis J. Lacoste <francis.lacoste@iNsu.COM>
7 #
8 # Copyright (C) 1999 Francis J. Lacoste, iNsu Innovations
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #
24 package Apache::iNcom::Localizer;
25  
26 require 5.005;
27  
28 use strict;
29  
30 use Symbol;
31 use File::Basename;
32 use I18N::LangTags 0.12;
33  
34 =pod
35  
36 =head1 NAME
37  
38 Apache::iNcom::Localizer - Object responsible for the localization of the
39 generated request.
40  
41 =head1 SYNOPSIS
42  
43 my $file = $Localizer->find_localized_file( $filename );
44 my $bundle = $Localizer->get_handle( "Site_L10N" );
45  
46 =head1 DESCRIPTION
47  
48 This module is used for localization in the Apache::iNcom framework.
49 It should be used for operation which are locale sensitive. (Messages
50 display, currency and date formatting, etc.)
51  
52 =head1 INITIALIZATION
53  
54 An object is automatically initialized on each request by the
55 Apache::iNcom framework. The list of preferred languages is determined
56 by using the Accept-Language HHTP header. The default language is set
57 to the one specified in the configuration. It is accessible through
58 the $Localizer gobal variable in the Apache::iNcom pages.
59  
60 =cut
61  
62 sub new {
63 my $proto = shift;
64 my $class = ref $proto || $proto;
65  
66 my $default = shift;
67 $default = "en" unless I18N::LangTags::is_language_tag( $default );
68 bless {
69 default => $default,
70 preferred => [ grep { I18N::LangTags::is_language_tag( $_ ) } @_ ],
71 }, $class;
72 }
73  
74 =pod
75  
76 =head1 METHODS
77  
78 =head2 default_lang
79  
80 Get or set the default language.
81  
82 =cut
83  
84 sub default_lang {
85 my ( $self, $default ) = @_;
86  
87 if ( $default ) {
88 $self->{default} = $default;
89 }
90 $self->{default};
91 }
92  
93 =pod
94  
95 =head2 preferred_lang
96  
97 Get or set the user's preferred language.
98  
99 =cut
100  
101 sub preferred_lang {
102 my ( $self ) = shift;
103  
104 $self->{preferred}[0] = [ $_[0] ] if ( $_[0] );
105  
106 # Use default if there is no preferred
107 $self->{preferred}[0] || $self->{default};
108 }
109  
110 =pod
111  
112 =head2 preferred_langs
113  
114 Get or set the user's preferred languages in order of preferences.
115  
116 =cut
117  
118 sub preferred_langs {
119 my ( $self) = shift;
120  
121 $self->{preferred} = [ grep { I18N::LangTags::is_language_tag( $_ ) } @_ ]
122 if ( @_ );
123  
124 return @{$self->{preferred}};
125 }
126  
127 =pod
128  
129 =head2 get_handle ( $base_package )
130  
131 This method will initialize a Locale::Maketext object of the
132 $base_package using the user's list of preferred languages. This
133 object should be used for localization of messages and such. See that
134 module documentation for details (or the article about it The Perl
135 Journal #8).
136  
137 =cut
138  
139 sub get_handle {
140 my ($self, $base_package) = @_;
141  
142 eval "use $base_package";
143 die $@ if $@;
144 {
145 no strict 'refs';
146 $base_package->get_handle( @{ $self->{preferred} },
147 $self->{default} );
148 }
149  
150 }
151  
152 =pod
153  
154 =head2 find_localized_file ( $filename )
155  
156 This method will try to find a properly localized version of the
157 $filename. Localized version of the file name should have the same
158 name with the language extension appended. If the file without
159 extension exists, it is assumed to be in the default language.
160  
161 Ex: file.txt -> English
162 file.txt.fr -> French
163 file.txt.es -> Spanish
164 file.txt.fr-CA -> French Canadian
165  
166 =cut
167  
168 sub find_localized_file {
169 my ($self, $file) = @_;
170  
171 my $default = $self->{default};
172 my %representation = (
173 I18N::LangTags::encode_language_tag( $default ) =>
174 $file
175 );
176 my ($name,$dir) = File::Basename::fileparse( $file );
177 return $file unless -d $dir;
178  
179 my $dh = gensym;
180 return $file unless opendir $dh, $dir;
181  
182 my $rx = qr/^\Q$name\E\.([-\w]+)$/;
183 my $var;
184 while ( $var = readdir $dh) {
185 next unless $var =~ /$rx/;
186 my $key = I18N::LangTags::encode_language_tag( $1 );
187 next unless $key;
188 $representation{ $key } = $dir . "/" . $var;
189 }
190 closedir $dh;
191  
192 # Find best match
193 foreach my $lang ( @{ $self->{preferred} } ) {
194 my $key = I18N::LangTags::encode_language_tag( $lang );
195 next unless $key;
196 return $representation{$key} if exists $representation{$key};
197 }
198  
199 # Well that will have to do
200 return $file;
201 }
202  
203  
204 1;
205  
206 __END__