File Coverage

blib/lib/File/Path/Localize.pm
Criterion Covered Total %
statement 43 47 91.4
branch 13 24 54.1
condition n/a
subroutine 6 6 100.0
pod 2 2 100.0
total 64 79 81.0


line stmt bran cond sub pod time code
1             # -*- perl -*-
2             #
3             # IO::File::Cached by Daniel Berrange
4             #
5             # Copyright (C) 20004 Daniel P. Berrange
6             #
7             # This program is free software; you can redistribute it and/or modify
8             # it under the terms of the GNU General Public License as published by
9             # the Free Software Foundation; either version 2 of the License, or
10             # (at your option) any later version.
11             #
12             # This program is distributed in the hope that it will be useful,
13             # but WITHOUT ANY WARRANTY; without even the implied warranty of
14             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15             # GNU General Public License for more details.
16             #
17             # You should have received a copy of the GNU General Public License
18             # along with this program; if not, write to the Free Software
19             # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20             #
21             # $Id: Localize.pm,v 1.2 2004/03/31 18:05:36 dan Exp $
22              
23             =pod
24              
25             =head1 NAME
26              
27             File::Path::Localize - locale and path aware file name resolution
28              
29             =head1 SYNOPSIS
30              
31             use File::Path::Localize;
32              
33             my @filenames = File::Path::Localize::expand(filename => $filename,
34             locales => \@locales);
35              
36             my $filepath = File::Path::Localize::locate(filename => $filename,
37             path => \@path,
38             locales => \@locales);
39              
40             =head1 DESCRIPTION
41              
42             The File::Path::Localize module provides a method to turn a relative
43             filename into an absolute filename using a listed of paths.
44             It can also localize the file path based on a list of locales.
45              
46             =head1 METHODS
47              
48             =over 4
49              
50             =cut
51            
52             package File::Path::Localize;
53              
54 1     1   1312 use strict;
  1         2  
  1         42  
55 1     1   6 use Carp qw(confess);
  1         2  
  1         69  
56 1     1   5 use File::Spec;
  1         5  
  1         37  
57              
58 1     1   5 use vars qw($VERSION);
  1         1  
  1         1108  
59              
60             $VERSION = "1.0.1";
61              
62             =item my $filename = locate(filename => $filename, locales => \@locales, path => \@path);
63              
64             Finds the best matching localized file in a set of paths.
65              
66             =cut
67              
68             sub locate {
69 2     2 1 1417 my %params = @_;
70            
71 2 50       9 my $filename = exists $params{filename} ? $params{filename} : confess "filename parameter is required";
72 2 50       6 my $locales = exists $params{locales} ? $params{locales} : undef;
73 2 50       16 my $path = exists $params{path} ? $params{path} : confess "path parameter is required";
74            
75 2 50       8 return $filename if $filename eq "-";
76            
77 2         7 foreach my $filename (expand(filename => $filename, locales => $locales)) {
78 9         37 my $rootdir = File::Spec->rootdir;
79 9 50       51 if ($filename =~ /^$rootdir/) {
80 0 0       0 return $filename if -e $filename;
81             } else {
82 9         11 foreach my $path (@{$path}) {
  9         16  
83 17         152 my $file = File::Spec->catfile($path, $filename);
84 17 100       411 return $file if -e $file;
85             }
86             }
87             }
88            
89 0         0 return undef;
90             }
91              
92              
93             =item my \@filenames = expand(filename => $filename, locales => \@locales)
94              
95             Expands a filename with a set of locales. For example given a filename
96             foo.txt and an array of locales [ en_GB.UTF-8, en_US, fr_FR] it will
97             return the set [ foo.txt.en_GB.UTF-8, foo.txt.en_GB, foo.txt.en,
98             foo.txt.en_US, foo.txt.en, foo.txt.fr, foo.txt.fr]
99              
100             =cut
101              
102             sub expand {
103 3     3 1 487 my %params = @_;
104              
105 3 50       13 my $filename = exists $params{filename} ? $params{filename} : confess "filename parameter is required";
106 3 50       10 my $locales = exists $params{locales} ? $params{locales} : [];
107              
108 3         4 my @files;
109 3         4 foreach my $locale (@{$locales}) {
  3         8  
110 9         12 my $language;
111             my $country;
112 0         0 my $charset;
113              
114 9 50       48 if ($locale =~ /^(\w\w)(?:_(\w\w)(?:\.([[:print:]]+))?)?$/) {
115 9         20 $language = $1;
116 9         19 $country = $2;
117 9         13 $charset = $3;
118             } else {
119 0         0 confess "cannot grok locale $locale\n";
120             }
121              
122 9         11 my @variations;
123 9 100       27 push @variations, join("", $language, "_", $country, ".", $charset)
124             if defined $charset;
125 9 50       28 push @variations, join("", $language, "_", $country)
126             if defined $country;
127 9         12 push @variations, $language;
128            
129 9         13 push @files, map { $filename . "." . $_ } @variations;
  21         62  
130             }
131 3         7 push @files, $filename;
132              
133 3         16 return @files;
134             }
135              
136             1 # So that the require or use succeeds.
137              
138             __END__