[Catalyst] get the path to the home

Mark Zealey mark at itsolve.co.uk
Fri Sep 28 23:07:52 GMT 2007


The __FILE__ compile-time constant may be of some use.. You should be able to 
get the directory of the current module by doing something like:

package MyApp::I18N::foo;
use strict;

sub get_my_dir {
  my ($dir) = __FILE__ =~ m!^ (.*) / [^/]+ !x;
  return $dir;
}

Of course, if you wanted to do that from a subclass or so you'd probably have 
to get the package of the object by ref($self) || $self, turn that into a 
file name and then look it up in %INC to find the location of the object.

Mark

On Friday 28 September 2007 3:46 pm, Octavian Rasnita wrote:
> From: "Chisel Wright" <chisel at herlpacker.co.uk>
>
> On Fri, Sep 28, 2007 at 03:30:23PM +0300, Octavian Rasnita wrote:
> > Hi,
> >
> > I have a I18N module in my Catalyst application and I want to get the
> > path to the home directory of the application. Is it possible to get it
> > without hard codding it in that module?
> >
> > The module is:
> >
> > package MyApp::I18N::ro;
> >
> > Assuming you can access to $c, I think this is what you want:
> >
> >   $c->path_to(’’)
> >
> > It's described in 'perldoc Catalyst' and mentioned in 'perldoc
> > Catalyst::Plugin::ConfigLoader'
> >
> > --
> > Chisel Wright
>
> Thank you but I know that. However, I have just the following module which
> is not a controller or a model or a view:
>
> # Copy/paste from Catalyst::Plugin::I18N:
>
>        #MyApp/I18N/de.pm
>        package MyApp::I18N::de;
>        use base 'MyApp::I18N';
>        our %Lexicon = ( 'Hello Catalyst' => 'Hallo Katalysator' );
>        1;
>
> So I don't have access to $c. Does this mean that I will need to hard code
> the path to the home directory?
>
> Here is what I want to do: I want to create a site in more languages, and
> it is simple to use a .po file for translating short strings, however I
> also need to display a few pages with pretty large descriptions, and I
> think I can avoid using a database for this.
>
> Instead of using .po files I want to use perl modules like ro.pm, de.pm and
> so on, and in those files I want to define the translations as:
>
> our %lexicon = (
> 'term' => 'translation',
> 'another term' => get_file('another_term',
> );
>
> sub get_file {
> my $file = shift;
> # read that file and return its content
> }
>
> I will keep those files in directories named with the language names like
> de, ro, en, fr, and they will be placed in MyApp/lib/MyApp/I18N directory
> (where the .pm or .po files should be also placed.
>
> And I want to know if there is a way of getting that folder name without
> hard codding it.
>
> Thanks.
>
> Octavian
>
>
>
> _______________________________________________
> List: Catalyst at lists.rawmode.org
> Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst@lists.rawmode.org/
> Dev site: http://dev.catalyst.perl.org/



More information about the Catalyst mailing list