Giving CGI::Application internationalization (I18N) part3

| No Comments | No TrackBacks
Giving CGI::Application internationalization (I18N)

Part 3


Lets look at the remaining functions of Catalyst::Plugin::I18N and what they are doing.


sub languages {
    my ( $c, $languages ) = @_; ### Getting the catalyst object and languages to set to it.
    if ($languages) { $c->{languages} = $languages } ### Setting the objects languages property if languages passed in
    else {
        $c->{languages} ||= [
            I18N::LangTags::implicate_supers(
                I18N::LangTags::Detect->http_accept_langs(
                    $c->request->header('Accept-Language')
                )
            ),
            'i-default'
        ];
    } ### Otherwise detects users language tags using I18N::LangTags::Detect and make sure all superordinates are included using I18N::LangTags::implicate_supers (if the objects languages property is empty)
    no strict 'refs';
    &{ ref($c) . '::_loc_lang' }( @{ $c->{languages} } ); ### Call Locale::Maketext::Simple's loc_lang function from the calling objects namespace setting the languages
    return $c->{languages};
}
sub language {
    my $c = shift;
    my $class = ref $c || $c; ### Getting the objects package name

    my $lang = ref "$class\::I18N"->get_handle( @{ $c->languages } ); ### Bipassing Locale::Maketext::Simple and calling Locale::Maketext's get_handle routine directly, returning a language handle object.
    $lang =~ s/.*:://; ### Which is then stripped to give you the name of the lexicon that is actually being used.

    return $lang;
}

sub language_tag {
    my $c = shift;
    my $class = ref $c || $c;

    return "$class\::I18N"->get_handle( @{ $c->languages } )->language_tag; ### Similar to above, but returning the language tag name as opposed to the
}


At first these two functions appeared to be needlessly creating a new language handle to get the required info. I thought Locale::Maketext::Simple must already have a language handle it's using, why not just access that. But when I looked deeper at the Locale::Maketext::Simple code, it seemed that handle wouldn't be easy to access at all, if even possible...


*loc = \&localize; ### Make the glob of loc a reference to the subroutine localize

sub localize {
    my $c = shift; ### Get catalyst object
    $c->languages; ### Sets the language on each call??? Seems a bit much...
    no strict 'refs';
    return &{ ref($c) . '::_loc' }( $_[0], @{ $_[1] } )
      if ( ref $_[1] eq 'ARRAY' ); ### Returns the output of the _loc routine (Locale::Maketext::Simple version of maketext), automatically dereferences array references passed as the second arguement
    return &{ ref($c) . '::_loc' }(@_); ### Returns the output of the _loc routine (Locale::Maketext::Simple version of maketext)
}



Here is the bit that actually displays the text. It links up the glob so that loc can be used as well as localize (that's why they've renamed Locale::Maketext::Simple's loc export to _loc).

Again none of these things are exported as they would need to be in CGI::App so I'm assuming Catalyst does this by default. Here are my routines:-


sub localtext_langs {
    my ($self, $langs) = @_;
    if ($langs){
        $self->{__I18N_LANGS} = $langs;
    }#if
    else {
        ### Get CGI query object
        my $q = $self->query();
        $self->{__I18N_LANGS} = [
            I18N::LangTags::implicate_supers(
                I18N::LangTags::Detect->http_accept_langs(
                    $q->http('Accept-Language')
                )
            ),
            'i-default'
        ] unless $self->{__I18N_LANGS};
    }#else
    no strict 'refs';
    &{ ref($self) . '::_maketext_lang' }( @{ $self->{__I18N_LANGS} } );
    return $self->{__I18N_LANGS};
}#sub


sub localtext_lang {
    my $self = shift;
    my $class = ref $self || $self;

    my $lang = ref "$class\::I18N"->get_handle( @{ $self->localtext_langs } );
    $lang =~ s/.*:://;

    return $lang;
}#sub


sub localtext_lang_tag {
    my $self = shift;
    my $class = ref $self || $self;

    return "$class\::I18N"->get_handle( @{ $self->localtext_langs } )->localtext_lang_tag;
}#sub


sub localtext {
    my $self = shift;
    $self->localtext_langs unless $self->{__I18N_LANGS};
    no strict 'refs';
    return &{ ref($self) . '::_maketext' }( $_[0], @{ $_[1] } ) if ( ref $_[1] eq 'ARRAY' );
    return &{ ref($self) . '::_maketext' }(@_);
}#sub


I'll export all these by default so the CGI::App object can have them as methods.

Again I have to stress that all this is completely untested. My next post should have some tests, and most likely fixes...


Lyle

No TrackBacks

TrackBack URL: http://perl.bristolbath.org/cgi-bin/mt/mt-tb.cgi/6

Leave a comment

About this Entry

This page contains a single entry by Lyle published on December 30, 2008 11:43 PM.

Giving CGI::Application internationalization (I18N) part2 was the previous entry in this blog.

Giving CGI::Application internationalization (I18N) part4 is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.