CC: | "David E. Wheeler" <david [...] justatheory.com> |
Subject: | [PATCH] Search File::ShareDir for LocaleData. |
Date: | Wed, 5 Sep 2012 11:48:28 -0700 |
To: | bug-libintl-perl [...] rt.cpan.org |
From: | "David E. Wheeler" <david [...] justatheory.com> |
From: "David E. Wheeler" <david@justatheory.com>
This is the most recent Perl-specific shared file location, with support
built into the standard installer tools. So it makes sense to use it
here.
---
Makefile.PL | 2 +-
lib/Locale/TextDomain.pm | 46 +++++++++++++++++++++++++++++-----------------
2 files changed, 30 insertions(+), 18 deletions(-)
diff --git a/Makefile.PL b/Makefile.PL
index 341b77e..5688839 100644
--- a/Makefile.PL
+++ b/Makefile.PL
@@ -205,7 +205,7 @@ WriteMakefile (
AUTHOR => 'Guido Flohr <guido@imperia.net>',
) : (),
),
- PREREQ_PM => { File::Spec => 0 },
+ PREREQ_PM => { File::Spec => 0, File::ShareDir => 0 },
PL_FILES => {},
DIR => [$result == 0 ? ('gettext_xs') : ()],
clean => { FILES => 'xs_disabled build_xs' }
diff --git a/lib/Locale/TextDomain.pm b/lib/Locale/TextDomain.pm
index f70162d..a962963 100755
--- a/lib/Locale/TextDomain.pm
+++ b/lib/Locale/TextDomain.pm
@@ -300,9 +300,12 @@ sub import
# Remember that we still have to bind that textdomain to
# a directory.
unless (exists $bound_dirs{$textdomain}) {
- @search_dirs = map $_ . '/LocaleData', @INC, @default_dirs
- unless @search_dirs;
- $bound_dirs{$textdomain} = [@search_dirs];
+ require File::ShareDir;
+ $bound_dirs{$textdomain} = [
+ @search_dirs || map { "$_/LocaleData" }
+ (eval { File::ShareDir::dist_dir($textdomain) } || ()),
+ @INC, @default_dirs
+ ];
}
Locale::TextDomain->export_to_level (1, $package, @EXPORT);
@@ -495,18 +498,26 @@ to search other directories prior to the default directories. Specifying
a differnt search directory is called I<binding> a textdomain to a
directory.
-B<Locale::TextDomain> extends the default strategy by a Perl
-specific approach. Unless told otherwise, it will look for a
-directory F<LocaleData> in every component found in the standard
-include path C<@INC> and check for a database containing the message
-for your textdomain there. Example: If the path
-F</usr/lib/perl/5.8.0/site_perl> is in your C<@INC>, you can
-install your translation files in F</usr/lib/perl/5.8.0/site_perl/LocaleData>,
-and they will be found at run-time.
+B<Locale::TextDomain> extends the default strategy by a Perl-specific
+approach. Unless told otherwise, it will first look for a directory named
+F<LocaleData> inin the directory returned by
+C<File::ShareDir::dist_dir($textdomain)>, and check for a database containing
+the message for your textdomain there. This allows you to install your
+database in the Perl-specific shared directory using L<Module::Install>'s
+C<install_share> directive or the Dist::Zilla L<ShareDir
+plugin|Dist::Zilla::Plugin::ShareDir>.
+
+If it fails to find the translation files in the L<File::ShareDir> directory,
+Locale::TextDomain will next look in every directory found in the standard
+include path C<@INC>, and check for a database containing the message for your
+textdomain there. Example: If the path F</usr/lib/perl/5.8.0/site_perl> is in
+your C<@INC>, you can install your translation files in
+F</usr/lib/perl/5.8.0/site_perl/LocaleData>, and they will be found at
+run-time.
=head1 USAGE
-It is crucial to remember that you use Locale::TextDoamin(3) as
+It is crucial to remember that you use Locale::TextDomain(3) as
specified in the section L</SYNOPSIS>, that means you have to
B<use> it, not B<require> it. The module behaves quite differently
compared to other modules.
@@ -519,7 +530,7 @@ as an argument to the use() function. It actually works like this:
The first argument (the first string passed to use()) is the textdomain
of your package, optionally followed by a list of directories to search
I<instead> of the Perl-specific directories (see above: F</LocaleData>
-appended to every part of C<@INC>).
+appended to a F<File::ShareDir> directory and every path in C<@INC>).
If you are the author of a package 'barfoos', you will probably put
the line
@@ -535,10 +546,11 @@ your module has been installed properly, including the message catalogs,
it will then be able to retrieve these translations at run-time.
If you have not installed the translation database in a directory
-F<LocaleData> in the standard include path C<@INC> (or in the system
-directories F</usr/share/locale> resp. F</usr/local/share/locale>), you
-have to explicitely specify a search path by giving the names of
-directories (as strings!) as additional arguments to use():
+F<LocaleData> in the L<File::ShareDir> directory or the standard include
+path C<@INC> (or in the system directories F</usr/share/locale> resp.
+F</usr/local/share/locale>), you have to explicitely specify a search
+path by giving the names of directories (as strings!) as additional
+arguments to use():
use Locale::TextDomain qw (barfoos ./dir1 ./dir2);
--
1.7.10