CatalystX-I18N

 view release on metacpan or  search on metacpan

lib/CatalystX/I18N/TraitFor/ViewTT.pm  view on Meta::CPAN

    if ($app->can('maketext')) {
        $config->{FILTERS}{maketext} ||= [ \&_i18n_maketext_factory, 1 ];
    }

    if ($app->can('localize')) {
        $config->{FILTERS}{localize} ||= [ \&_i18n_localize_factory, 1 ];
    }

    # Call original BUILDARGS
    return $self->$orig($app,$config);
};

sub _i18n_numberformat_factory {
    my ( $context, $format, @options ) = @_;

    my $stash = $context->stash;
    my $catalyst_var = $context->{CONFIG}{CATALYST_VAR};
    my $c = $stash->{$catalyst_var};
    weaken $c;

    my $number_format = $c->i18n_numberformat;
    if (defined $format) {
        undef $format
            unless (grep { $format eq $_ } qw(number negative bytes price picture));
    }

    return sub {
        my $value = shift;

        my $local_format = 'format_'.($format || 'number');


        return $c->maketext('n/a')
            unless defined $value;

        return $number_format->$local_format($value,@options);
    }
}

sub _i18n_maketext_factory {
    my ( $context,@params ) = @_;

    return _i18n_factory_helper('maketext', $context,@params)
}

sub _i18n_localize_factory {
    my ( $context,@params ) = @_;

    return _i18n_factory_helper('localize', $context,@params)
}

sub _i18n_factory_helper {
    my ( $method, $context, @params ) = @_;

    my $stash = $context->stash;
    my $catalyst_var = $context->{CONFIG}{CATALYST_VAR};
    my $c = $stash->{$catalyst_var};
    weaken $c;

    return sub {
        my ($msgid) = @_;
        if (scalar @params == 1
            && ref($params[0]) eq 'ARRAY') {
            @params = @{$params[0]};
        }

        return $c->$method($msgid,@params);
    }
}

no Moose::Role;
1;

=encoding utf8

=head1 NAME

CatalystX::I18N::TraitFor::ViewTT - Adds I18N filters and VMethods to a TT view

=head1 SYNOPSIS

 # In your view
 package MyApp::View::TT;
 use Moose;
 extends qw(Catalyst::View::TT);
 with qw(CatalystX::I18N::TraitFor::ViewTT);
 
 # In your TT template
 # Localised number format
 [% 22 | number('number') %]
 [% 22 | number %]
 
 # Localised collation
 [% mylist.lsort().join(', ') %]
 
 # Maketext
 [% 'Hello %1!' | maketext(name) %]

=head1 DESCRIPTION

=head2 Filters

=head3 number

Formats a number with the current locale settings. You need to have
the L<CatalystX::I18N::Role::NumberFormat> role loaded in Catalyst.

The following formats are available

=over

=item * price

=item * number (default)

=item * bytes

=item * negative

=item * picture

=back

=head3 maketext

Returns the translation for the given string.



( run in 0.987 second using v1.01-cache-2.11-cpan-97f6503c9c8 )